| 1 |
// Copyright 2012-present Oliver Eilhard. All rights reserved. |
| 2 |
// Use of this source code is governed by a MIT-license. |
| 3 |
// See http://olivere.mit-license.org/license.txt for details. |
| 4 |
|
| 5 |
package elastic |
| 6 |
|
| 7 |
import ( |
| 8 |
"encoding/json" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
func TestSearchSourceMatchAllQuery(t *testing.T) { |
| 13 |
matchAllQ := NewMatchAllQuery() |
| 14 |
builder := NewSearchSource().Query(matchAllQ) |
| 15 |
src, err := builder.Source() |
| 16 |
if err != nil { |
| 17 |
t.Fatal(err) |
| 18 |
} |
| 19 |
data, err := json.Marshal(src) |
| 20 |
if err != nil { |
| 21 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 22 |
} |
| 23 |
got := string(data) |
| 24 |
expected := `{"query":{"match_all":{}}}` |
| 25 |
if got != expected { |
| 26 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
func TestSearchSourceNoStoredFields(t *testing.T) { |
| 31 |
matchAllQ := NewMatchAllQuery() |
| 32 |
builder := NewSearchSource().Query(matchAllQ).NoStoredFields() |
| 33 |
src, err := builder.Source() |
| 34 |
if err != nil { |
| 35 |
t.Fatal(err) |
| 36 |
} |
| 37 |
data, err := json.Marshal(src) |
| 38 |
if err != nil { |
| 39 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 40 |
} |
| 41 |
got := string(data) |
| 42 |
expected := `{"query":{"match_all":{}}}` |
| 43 |
if got != expected { |
| 44 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
func TestSearchSourceStoredFields(t *testing.T) { |
| 49 |
matchAllQ := NewMatchAllQuery() |
| 50 |
builder := NewSearchSource().Query(matchAllQ).StoredFields("message", "tags") |
| 51 |
src, err := builder.Source() |
| 52 |
if err != nil { |
| 53 |
t.Fatal(err) |
| 54 |
} |
| 55 |
data, err := json.Marshal(src) |
| 56 |
if err != nil { |
| 57 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 58 |
} |
| 59 |
got := string(data) |
| 60 |
expected := `{"query":{"match_all":{}},"stored_fields":["message","tags"]}` |
| 61 |
if got != expected { |
| 62 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func TestSearchSourceFetchSourceDisabled(t *testing.T) { |
| 67 |
matchAllQ := NewMatchAllQuery() |
| 68 |
builder := NewSearchSource().Query(matchAllQ).FetchSource(false) |
| 69 |
src, err := builder.Source() |
| 70 |
if err != nil { |
| 71 |
t.Fatal(err) |
| 72 |
} |
| 73 |
data, err := json.Marshal(src) |
| 74 |
if err != nil { |
| 75 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 76 |
} |
| 77 |
got := string(data) |
| 78 |
expected := `{"_source":false,"query":{"match_all":{}}}` |
| 79 |
if got != expected { |
| 80 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
func TestSearchSourceFetchSourceByWildcards(t *testing.T) { |
| 85 |
matchAllQ := NewMatchAllQuery() |
| 86 |
fsc := NewFetchSourceContext(true).Include("obj1.*", "obj2.*").Exclude("*.description") |
| 87 |
builder := NewSearchSource().Query(matchAllQ).FetchSourceContext(fsc) |
| 88 |
src, err := builder.Source() |
| 89 |
if err != nil { |
| 90 |
t.Fatal(err) |
| 91 |
} |
| 92 |
data, err := json.Marshal(src) |
| 93 |
if err != nil { |
| 94 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 95 |
} |
| 96 |
got := string(data) |
| 97 |
expected := `{"_source":{"excludes":["*.description"],"includes":["obj1.*","obj2.*"]},"query":{"match_all":{}}}` |
| 98 |
if got != expected { |
| 99 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
func TestSearchSourceDocvalueFields(t *testing.T) { |
| 104 |
matchAllQ := NewMatchAllQuery() |
| 105 |
builder := NewSearchSource().Query(matchAllQ).DocvalueFields("test1", "test2") |
| 106 |
src, err := builder.Source() |
| 107 |
if err != nil { |
| 108 |
t.Fatal(err) |
| 109 |
} |
| 110 |
data, err := json.Marshal(src) |
| 111 |
if err != nil { |
| 112 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 113 |
} |
| 114 |
got := string(data) |
| 115 |
expected := `{"docvalue_fields":["test1","test2"],"query":{"match_all":{}}}` |
| 116 |
if got != expected { |
| 117 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
func TestSearchSourceScriptFields(t *testing.T) { |
| 122 |
matchAllQ := NewMatchAllQuery() |
| 123 |
sf1 := NewScriptField("test1", NewScript("doc['my_field_name'].value * 2")) |
| 124 |
sf2 := NewScriptField("test2", NewScript("doc['my_field_name'].value * factor").Param("factor", 3.1415927)) |
| 125 |
builder := NewSearchSource().Query(matchAllQ).ScriptFields(sf1, sf2) |
| 126 |
src, err := builder.Source() |
| 127 |
if err != nil { |
| 128 |
t.Fatal(err) |
| 129 |
} |
| 130 |
data, err := json.Marshal(src) |
| 131 |
if err != nil { |
| 132 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 133 |
} |
| 134 |
got := string(data) |
| 135 |
expected := `{"query":{"match_all":{}},"script_fields":{"test1":{"script":{"inline":"doc['my_field_name'].value * 2"}},"test2":{"script":{"inline":"doc['my_field_name'].value * factor","params":{"factor":3.1415927}}}}}` |
| 136 |
if got != expected { |
| 137 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
func TestSearchSourcePostFilter(t *testing.T) { |
| 142 |
matchAllQ := NewMatchAllQuery() |
| 143 |
pf := NewTermQuery("tag", "important") |
| 144 |
builder := NewSearchSource().Query(matchAllQ).PostFilter(pf) |
| 145 |
src, err := builder.Source() |
| 146 |
if err != nil { |
| 147 |
t.Fatal(err) |
| 148 |
} |
| 149 |
data, err := json.Marshal(src) |
| 150 |
if err != nil { |
| 151 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 152 |
} |
| 153 |
got := string(data) |
| 154 |
expected := `{"post_filter":{"term":{"tag":"important"}},"query":{"match_all":{}}}` |
| 155 |
if got != expected { |
| 156 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
func TestSearchSourceHighlight(t *testing.T) { |
| 161 |
matchAllQ := NewMatchAllQuery() |
| 162 |
hl := NewHighlight().Field("content") |
| 163 |
builder := NewSearchSource().Query(matchAllQ).Highlight(hl) |
| 164 |
src, err := builder.Source() |
| 165 |
if err != nil { |
| 166 |
t.Fatal(err) |
| 167 |
} |
| 168 |
data, err := json.Marshal(src) |
| 169 |
if err != nil { |
| 170 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 171 |
} |
| 172 |
got := string(data) |
| 173 |
expected := `{"highlight":{"fields":{"content":{}}},"query":{"match_all":{}}}` |
| 174 |
if got != expected { |
| 175 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
func TestSearchSourceRescoring(t *testing.T) { |
| 180 |
matchAllQ := NewMatchAllQuery() |
| 181 |
rescorerQuery := NewMatchPhraseQuery("field1", "the quick brown fox").Slop(2) |
| 182 |
rescorer := NewQueryRescorer(rescorerQuery) |
| 183 |
rescorer = rescorer.QueryWeight(0.7) |
| 184 |
rescorer = rescorer.RescoreQueryWeight(1.2) |
| 185 |
rescore := NewRescore().WindowSize(50).Rescorer(rescorer) |
| 186 |
builder := NewSearchSource().Query(matchAllQ).Rescorer(rescore) |
| 187 |
src, err := builder.Source() |
| 188 |
if err != nil { |
| 189 |
t.Fatal(err) |
| 190 |
} |
| 191 |
data, err := json.Marshal(src) |
| 192 |
if err != nil { |
| 193 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 194 |
} |
| 195 |
got := string(data) |
| 196 |
expected := `{"query":{"match_all":{}},"rescore":{"query":{"query_weight":0.7,"rescore_query":{"match_phrase":{"field1":{"query":"the quick brown fox","slop":2}}},"rescore_query_weight":1.2},"window_size":50}}` |
| 197 |
if got != expected { |
| 198 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
func TestSearchSourceIndexBoost(t *testing.T) { |
| 203 |
matchAllQ := NewMatchAllQuery() |
| 204 |
builder := NewSearchSource().Query(matchAllQ).IndexBoost("index1", 1.4).IndexBoost("index2", 1.3) |
| 205 |
src, err := builder.Source() |
| 206 |
if err != nil { |
| 207 |
t.Fatal(err) |
| 208 |
} |
| 209 |
data, err := json.Marshal(src) |
| 210 |
if err != nil { |
| 211 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 212 |
} |
| 213 |
got := string(data) |
| 214 |
expected := `{"indices_boost":{"index1":1.4,"index2":1.3},"query":{"match_all":{}}}` |
| 215 |
if got != expected { |
| 216 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
func TestSearchSourceMixDifferentSorters(t *testing.T) { |
| 221 |
matchAllQ := NewMatchAllQuery() |
| 222 |
builder := NewSearchSource().Query(matchAllQ). |
| 223 |
Sort("a", false). |
| 224 |
SortWithInfo(SortInfo{Field: "b", Ascending: true}). |
| 225 |
SortBy(NewScriptSort(NewScript("doc['field_name'].value * factor").Param("factor", 1.1), "number")) |
| 226 |
src, err := builder.Source() |
| 227 |
if err != nil { |
| 228 |
t.Fatal(err) |
| 229 |
} |
| 230 |
data, err := json.Marshal(src) |
| 231 |
if err != nil { |
| 232 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 233 |
} |
| 234 |
got := string(data) |
| 235 |
expected := `{"query":{"match_all":{}},"sort":[{"a":{"order":"desc"}},{"b":{"order":"asc"}},{"_script":{"order":"asc","script":{"inline":"doc['field_name'].value * factor","params":{"factor":1.1}},"type":"number"}}]}` |
| 236 |
if got != expected { |
| 237 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
func TestSearchSourceInnerHits(t *testing.T) { |
| 242 |
matchAllQ := NewMatchAllQuery() |
| 243 |
builder := NewSearchSource().Query(matchAllQ). |
| 244 |
InnerHit("comments", NewInnerHit().Type("comment").Query(NewMatchQuery("user", "olivere"))). |
| 245 |
InnerHit("views", NewInnerHit().Path("view")) |
| 246 |
src, err := builder.Source() |
| 247 |
if err != nil { |
| 248 |
t.Fatal(err) |
| 249 |
} |
| 250 |
data, err := json.Marshal(src) |
| 251 |
if err != nil { |
| 252 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 253 |
} |
| 254 |
got := string(data) |
| 255 |
expected := `{"inner_hits":{"comments":{"type":{"comment":{"query":{"match":{"user":{"query":"olivere"}}}}}},"views":{"path":{"view":{}}}},"query":{"match_all":{}}}` |
| 256 |
if got != expected { |
| 257 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
func TestSearchSourceSearchAfter(t *testing.T) { |
| 262 |
matchAllQ := NewMatchAllQuery() |
| 263 |
builder := NewSearchSource().Query(matchAllQ).SearchAfter(1463538857, "tweet#654323") |
| 264 |
src, err := builder.Source() |
| 265 |
if err != nil { |
| 266 |
t.Fatal(err) |
| 267 |
} |
| 268 |
data, err := json.Marshal(src) |
| 269 |
if err != nil { |
| 270 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 271 |
} |
| 272 |
got := string(data) |
| 273 |
expected := `{"query":{"match_all":{}},"search_after":[1463538857,"tweet#654323"]}` |
| 274 |
if got != expected { |
| 275 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
func TestSearchSourceProfiledQuery(t *testing.T) { |
| 280 |
matchAllQ := NewMatchAllQuery() |
| 281 |
builder := NewSearchSource().Query(matchAllQ).Profile(true) |
| 282 |
src, err := builder.Source() |
| 283 |
if err != nil { |
| 284 |
t.Fatal(err) |
| 285 |
} |
| 286 |
data, err := json.Marshal(src) |
| 287 |
if err != nil { |
| 288 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 289 |
} |
| 290 |
got := string(data) |
| 291 |
expected := `{"profile":true,"query":{"match_all":{}}}` |
| 292 |
if got != expected { |
| 293 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 294 |
} |
| 295 |
} |