-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
search_queries_fsq_test.go
138 lines (129 loc) · 5.29 KB
/
search_queries_fsq_test.go
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
// 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 TestFunctionScoreQuery(t *testing.T) {
q := NewFunctionScoreQuery().
Query(NewTermQuery("name.last", "banon")).
Add(NewTermFilter("name.last", "banon"), NewFactorFunction().BoostFactor(3)).
AddScoreFunc(NewFactorFunction().BoostFactor(3)).
AddScoreFunc(NewFactorFunction().BoostFactor(3)).
Boost(3).
MaxBoost(10).
ScoreMode("avg")
data, err := json.Marshal(q.Source())
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"function_score":{"boost":3,"functions":[{"boost_factor":3,"filter":{"term":{"name.last":"banon"}}},{"boost_factor":3},{"boost_factor":3}],"max_boost":10,"query":{"term":{"name.last":"banon"}},"score_mode":"avg"}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}
func TestFunctionScoreQueryWithNilFilter(t *testing.T) {
q := NewFunctionScoreQuery().
Query(NewTermQuery("tag", "wow")).
AddScoreFunc(NewRandomFunction()).
Boost(2.0).
MaxBoost(12.0).
BoostMode("multiply").
ScoreMode("max")
data, err := json.Marshal(q.Source())
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"function_score":{"boost":2,"boost_mode":"multiply","max_boost":12,"query":{"term":{"tag":"wow"}},"random_score":{},"score_mode":"max"}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}
func TestFieldValueFactor(t *testing.T) {
q := NewFunctionScoreQuery().
Query(NewTermQuery("name.last", "banon")).
AddScoreFunc(NewFieldValueFactorFunction().Modifier("sqrt").Factor(2).Field("income")).
Boost(2.0).
MaxBoost(12.0).
BoostMode("multiply").
ScoreMode("max")
data, err := json.Marshal(q.Source())
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"function_score":{"boost":2,"boost_mode":"multiply","field_value_factor":{"factor":2,"field":"income","modifier":"sqrt"},"max_boost":12,"query":{"term":{"name.last":"banon"}},"score_mode":"max"}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}
func TestFieldValueFactorWithWeight(t *testing.T) {
q := NewFunctionScoreQuery().
Query(NewTermQuery("name.last", "banon")).
AddScoreFunc(NewFieldValueFactorFunction().Modifier("sqrt").Factor(2).Field("income").Weight(2.5)).
Boost(2.0).
MaxBoost(12.0).
BoostMode("multiply").
ScoreMode("max")
data, err := json.Marshal(q.Source())
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"function_score":{"boost":2,"boost_mode":"multiply","field_value_factor":{"factor":2,"field":"income","modifier":"sqrt"},"max_boost":12,"query":{"term":{"name.last":"banon"}},"score_mode":"max","weight":2.5}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}
func TestFieldValueFactorWithMultipleScoreFuncsAndWeights(t *testing.T) {
q := NewFunctionScoreQuery().
Query(NewTermQuery("name.last", "banon")).
AddScoreFunc(NewFieldValueFactorFunction().Modifier("sqrt").Factor(2).Field("income").Weight(2.5)).
AddScoreFunc(NewScriptFunction("_score * doc['my_numeric_field'].value").Weight(1.25)).
AddScoreFunc(NewWeightFactorFunction(0.5)).
Boost(2.0).
MaxBoost(12.0).
BoostMode("multiply").
ScoreMode("max")
data, err := json.Marshal(q.Source())
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"function_score":{"boost":2,"boost_mode":"multiply","functions":[{"field_value_factor":{"factor":2,"field":"income","modifier":"sqrt"},"weight":2.5},{"script_score":{"script":"_score * doc['my_numeric_field'].value"},"weight":1.25},{"weight":0.5}],"max_boost":12,"query":{"term":{"name.last":"banon"}},"score_mode":"max"}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}
func TestFunctionScoreQueryWithGaussScoreFunc(t *testing.T) {
q := NewFunctionScoreQuery().
Query(NewTermQuery("name.last", "banon")).
AddScoreFunc(NewGaussDecayFunction().FieldName("pin.location").Origin("11, 12").Scale("2km").Offset("0km").Decay(0.33))
data, err := json.Marshal(q.Source())
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"function_score":{"gauss":{"pin.location":{"decay":0.33,"offset":"0km","origin":"11, 12","scale":"2km"}},"query":{"term":{"name.last":"banon"}}}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}
func TestFunctionScoreQueryWithGaussScoreFuncAndMultiValueMode(t *testing.T) {
q := NewFunctionScoreQuery().
Query(NewTermQuery("name.last", "banon")).
AddScoreFunc(NewGaussDecayFunction().FieldName("pin.location").Origin("11, 12").Scale("2km").Offset("0km").Decay(0.33).MultiValueMode("avg"))
data, err := json.Marshal(q.Source())
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"function_score":{"gauss":{"multi_value_mode":"avg","pin.location":{"decay":0.33,"offset":"0km","origin":"11, 12","scale":"2km"}},"query":{"term":{"name.last":"banon"}}}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}