-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathsearch_queries_percolator.go
131 lines (113 loc) · 3.35 KB
/
search_queries_percolator.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
// 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 "errors"
// PercolatorQuery can be used to match queries stored in an index.
//
// For more details, see
// https://www.elastic.co/guide/en/elasticsearch/reference/7.0/query-dsl-percolate-query.html
type PercolatorQuery struct {
field string
name string
documentType string // deprecated
documents []interface{}
indexedDocumentIndex string
indexedDocumentType string
indexedDocumentId string
indexedDocumentRouting string
indexedDocumentPreference string
indexedDocumentVersion *int64
}
// NewPercolatorQuery creates and initializes a new Percolator query.
func NewPercolatorQuery() *PercolatorQuery {
return &PercolatorQuery{}
}
func (q *PercolatorQuery) Field(field string) *PercolatorQuery {
q.field = field
return q
}
// Name used for identification purposes in "_percolator_document_slot" response
// field when multiple percolate queries have been specified in the main query.
func (q *PercolatorQuery) Name(name string) *PercolatorQuery {
q.name = name
return q
}
// Deprecated: DocumentType is deprecated as of 6.0.
func (q *PercolatorQuery) DocumentType(typ string) *PercolatorQuery {
q.documentType = typ
return q
}
func (q *PercolatorQuery) Document(docs ...interface{}) *PercolatorQuery {
q.documents = append(q.documents, docs...)
return q
}
func (q *PercolatorQuery) IndexedDocumentIndex(index string) *PercolatorQuery {
q.indexedDocumentIndex = index
return q
}
func (q *PercolatorQuery) IndexedDocumentType(typ string) *PercolatorQuery {
q.indexedDocumentType = typ
return q
}
func (q *PercolatorQuery) IndexedDocumentId(id string) *PercolatorQuery {
q.indexedDocumentId = id
return q
}
func (q *PercolatorQuery) IndexedDocumentRouting(routing string) *PercolatorQuery {
q.indexedDocumentRouting = routing
return q
}
func (q *PercolatorQuery) IndexedDocumentPreference(preference string) *PercolatorQuery {
q.indexedDocumentPreference = preference
return q
}
func (q *PercolatorQuery) IndexedDocumentVersion(version int64) *PercolatorQuery {
q.indexedDocumentVersion = &version
return q
}
// Source returns JSON for the percolate query.
func (q *PercolatorQuery) Source() (interface{}, error) {
if len(q.field) == 0 {
return nil, errors.New("elastic: Field is required in PercolatorQuery")
}
// {
// "percolate" : { ... }
// }
source := make(map[string]interface{})
params := make(map[string]interface{})
source["percolate"] = params
params["field"] = q.field
if q.documentType != "" {
params["document_type"] = q.documentType
}
if q.name != "" {
params["name"] = q.name
}
switch len(q.documents) {
case 0:
case 1:
params["document"] = q.documents[0]
default:
params["documents"] = q.documents
}
if s := q.indexedDocumentIndex; s != "" {
params["index"] = s
}
if s := q.indexedDocumentType; s != "" {
params["type"] = s
}
if s := q.indexedDocumentId; s != "" {
params["id"] = s
}
if s := q.indexedDocumentRouting; s != "" {
params["routing"] = s
}
if s := q.indexedDocumentPreference; s != "" {
params["preference"] = s
}
if v := q.indexedDocumentVersion; v != nil {
params["version"] = *v
}
return source, nil
}