forked from olivere/elastic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_queries_custom_score.go
108 lines (89 loc) · 2.29 KB
/
search_queries_custom_score.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
// 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
// custom_score query allows to wrap another query and customize
// the scoring of it optionally with a computation derived from
// other field values in the doc (numeric ones) using script expression.
//
// For more details, see:
// http://www.elasticsearch.org/guide/reference/query-dsl/custom-score-query/
type CustomScoreQuery struct {
query Query
filter Filter
script string
lang string
boost *float32
params map[string]interface{}
}
// Creates a new custom_score query.
func NewCustomScoreQuery() CustomScoreQuery {
q := CustomScoreQuery{
params: make(map[string]interface{}),
}
return q
}
func (q CustomScoreQuery) Query(query Query) CustomScoreQuery {
q.query = query
return q
}
func (q CustomScoreQuery) Filter(filter Filter) CustomScoreQuery {
q.filter = filter
return q
}
func (q CustomScoreQuery) Script(script string) CustomScoreQuery {
q.script = script
return q
}
func (q CustomScoreQuery) Lang(lang string) CustomScoreQuery {
q.lang = lang
return q
}
func (q CustomScoreQuery) Boost(boost float32) CustomScoreQuery {
q.boost = &boost
return q
}
func (q CustomScoreQuery) Params(params map[string]interface{}) CustomScoreQuery {
q.params = params
return q
}
func (q CustomScoreQuery) Param(name string, value interface{}) CustomScoreQuery {
q.params[name] = value
return q
}
// Creates the query source for the custom_fscore query.
func (q CustomScoreQuery) Source() interface{} {
// "custom_score" : {
// "query" : {
// ....
// },
// "params" : {
// "param1" : 2,
// "param2" : 3.1
// },
// "script" : "_score * doc['my_numeric_field'].value / pow(param1, param2)"
// }
query := make(map[string]interface{})
csq := make(map[string]interface{})
query["custom_score"] = csq
// query
if q.query != nil {
csq["query"] = q.query.Source()
} else if q.filter != nil {
csq["filter"] = q.filter.Source()
}
csq["script"] = q.script
// lang
if q.lang != "" {
csq["lang"] = q.lang
}
// params
if len(q.params) > 0 {
csq["params"] = q.params
}
// boost
if q.boost != nil {
csq["boost"] = *q.boost
}
return query
}