1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
// Copyright 2012-2015 Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package elastic
import (
"encoding/json"
"testing"
)
func TestSearchSourceMatchAllQuery(t *testing.T) {
matchAllQ := NewMatchAllQuery()
builder := NewSearchSource().Query(matchAllQ)
data, err := json.Marshal(builder.Source())
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"query":{"match_all":{}}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}
func TestSearchSourceFromAndSize(t *testing.T) {
matchAllQ := NewMatchAllQuery()
builder := NewSearchSource().Query(matchAllQ).From(21).Size(20)
data, err := json.Marshal(builder.Source())
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"from":21,"query":{"match_all":{}},"size":20}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}
func TestSearchSourceNoFields(t *testing.T) {
matchAllQ := NewMatchAllQuery()
builder := NewSearchSource().Query(matchAllQ).NoFields()
data, err := json.Marshal(builder.Source())
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"fields":[],"query":{"match_all":{}}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}
func TestSearchSourceFields(t *testing.T) {
matchAllQ := NewMatchAllQuery()
builder := NewSearchSource().Query(matchAllQ).Fields("message", "tags")
data, err := json.Marshal(builder.Source())
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"fields":["message","tags"],"query":{"match_all":{}}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}
func TestSearchSourceFetchSourceDisabled(t *testing.T) {
matchAllQ := NewMatchAllQuery()
builder := NewSearchSource().Query(matchAllQ).FetchSource(false)
data, err := json.Marshal(builder.Source())
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"_source":false,"query":{"match_all":{}}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}
func TestSearchSourceFetchSourceByWildcards(t *testing.T) {
matchAllQ := NewMatchAllQuery()
fsc := NewFetchSourceContext(true).Include("obj1.*", "obj2.*").Exclude("*.description")
builder := NewSearchSource().Query(matchAllQ).FetchSourceContext(fsc)
data, err := json.Marshal(builder.Source())
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"_source":{"excludes":["*.description"],"includes":["obj1.*","obj2.*"]},"query":{"match_all":{}}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}
func TestSearchSourceFieldDataFields(t *testing.T) {
matchAllQ := NewMatchAllQuery()
builder := NewSearchSource().Query(matchAllQ).FieldDataFields("test1", "test2")
data, err := json.Marshal(builder.Source())
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"fielddata_fields":["test1","test2"],"query":{"match_all":{}}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}
func TestSearchSourceScriptFields(t *testing.T) {
matchAllQ := NewMatchAllQuery()
sf1 := NewScriptField("test1", "doc['my_field_name'].value * 2", "", nil)
sf2 := NewScriptField("test2", "doc['my_field_name'].value * factor", "", map[string]interface{}{"factor": 3.1415927})
builder := NewSearchSource().Query(matchAllQ).ScriptFields(sf1, sf2)
data, err := json.Marshal(builder.Source())
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"query":{"match_all":{}},"script_fields":{"test1":{"script":"doc['my_field_name'].value * 2"},"test2":{"params":{"factor":3.1415927},"script":"doc['my_field_name'].value * factor"}}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}
func TestSearchSourcePostFilter(t *testing.T) {
matchAllQ := NewMatchAllQuery()
pf := NewTermFilter("tag", "important")
builder := NewSearchSource().Query(matchAllQ).PostFilter(pf)
data, err := json.Marshal(builder.Source())
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"post_filter":{"term":{"tag":"important"}},"query":{"match_all":{}}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}
func TestSearchSourceHighlight(t *testing.T) {
matchAllQ := NewMatchAllQuery()
hl := NewHighlight().Field("content")
builder := NewSearchSource().Query(matchAllQ).Highlight(hl)
data, err := json.Marshal(builder.Source())
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"highlight":{"fields":{"content":{}}},"query":{"match_all":{}}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}
func TestSearchSourceRescoring(t *testing.T) {
matchAllQ := NewMatchAllQuery()
rescorerQuery := NewMatchQuery("field1", "the quick brown fox").Type("phrase").Slop(2)
rescorer := NewQueryRescorer(rescorerQuery)
rescorer = rescorer.QueryWeight(0.7)
rescorer = rescorer.RescoreQueryWeight(1.2)
rescore := NewRescore().WindowSize(50).Rescorer(rescorer)
builder := NewSearchSource().Query(matchAllQ).AddRescore(rescore)
data, err := json.Marshal(builder.Source())
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"query":{"match_all":{}},"rescore":{"query":{"query_weight":0.7,"rescore_query":{"match":{"field1":{"query":"the quick brown fox","slop":2,"type":"phrase"}}},"rescore_query_weight":1.2},"window_size":50}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}
func TestSearchSourceIndexBoost(t *testing.T) {
matchAllQ := NewMatchAllQuery()
builder := NewSearchSource().Query(matchAllQ).IndexBoost("index1", 1.4).IndexBoost("index2", 1.3)
data, err := json.Marshal(builder.Source())
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"indices_boost":{"index1":1.4,"index2":1.3},"query":{"match_all":{}}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}
func TestSearchSourceInnerHits(t *testing.T) {
matchAllQ := NewMatchAllQuery()
builder := NewSearchSource().Query(matchAllQ).
InnerHit("comments", NewInnerHit().Type("comment").Query(NewMatchQuery("user", "olivere"))).
InnerHit("views", NewInnerHit().Path("view"))
data, err := json.Marshal(builder.Source())
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"inner_hits":{"comments":{"type":{"comment":{"query":{"match":{"user":{"query":"olivere"}}}}}},"views":{"path":{"view":{}}}},"query":{"match_all":{}}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}
|