-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathsearch_queries_distance_feature_query.go
119 lines (105 loc) · 3.2 KB
/
search_queries_distance_feature_query.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
// Copyright 2012-present 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 (
"fmt"
)
// DistanceFeatureQuery uses a script to provide a custom score for returned documents.
//
// A DistanceFeatureQuery query is useful if, for example, a scoring function is
// expensive and you only need to calculate the score of a filtered set of documents.
//
// For more details, see
// https://www.elastic.co/guide/en/elasticsearch/reference/7.4/query-dsl-distance-feature-query.html
type DistanceFeatureQuery struct {
field string
pivot string
origin interface{}
boost *float64
queryName string
}
// NewDistanceFeatureQuery creates and initializes a new script_score query.
func NewDistanceFeatureQuery(field string, origin interface{}, pivot string) *DistanceFeatureQuery {
return &DistanceFeatureQuery{
field: field,
origin: origin,
pivot: pivot,
}
}
// Field to be used in the DistanceFeatureQuery.
func (q *DistanceFeatureQuery) Field(name string) *DistanceFeatureQuery {
q.field = name
return q
}
// Origin is the date or point of origin used to calculate distances.
//
// If the field is a date or date_nanos field, the origin value must be a
// date. Date math such as "now-1h" is supported.
//
// If the field is a geo_point field, the origin must be a GeoPoint.
func (q *DistanceFeatureQuery) Origin(origin interface{}) *DistanceFeatureQuery {
q.origin = origin
return q
}
// Pivot is distance from the origin at which relevance scores
// receive half of the boost value.
//
// If field is a date or date_nanos field, the pivot value must be a time
// unit, such as "1h" or "10d".
//
// If field is a geo_point field, the pivot value must be a distance unit,
// such as "1km" or "12m". You can pass a string, or a GeoPoint.
func (q *DistanceFeatureQuery) Pivot(pivot string) *DistanceFeatureQuery {
q.pivot = pivot
return q
}
// Boost sets the boost for this query.
func (q *DistanceFeatureQuery) Boost(boost float64) *DistanceFeatureQuery {
q.boost = &boost
return q
}
// QueryName sets the query name for the filter.
func (q *DistanceFeatureQuery) QueryName(queryName string) *DistanceFeatureQuery {
q.queryName = queryName
return q
}
// Source returns JSON for the function score query.
func (q *DistanceFeatureQuery) Source() (interface{}, error) {
// {
// "distance_feature" : {
// "field" : "production_date",
// "pivot" : "7d",
// "origin" : "now"
// }
// }
// {
// "distance_feature" : {
// "field" : "location",
// "pivot" : "1000m",
// "origin" : [-71.3, 41.15]
// }
// }
source := make(map[string]interface{})
query := make(map[string]interface{})
source["distance_feature"] = query
query["field"] = q.field
query["pivot"] = q.pivot
switch v := q.origin.(type) {
default:
return nil, fmt.Errorf("DistanceFeatureQuery: unable to serialize Origin from type %T", v)
case string:
query["origin"] = v
case *GeoPoint:
query["origin"] = v.Source()
case GeoPoint:
query["origin"] = v.Source()
}
if v := q.boost; v != nil {
query["boost"] = *v
}
if q.queryName != "" {
query["_name"] = q.queryName
}
return source, nil
}