Debian Codegraph @

search_queries_percolator.go

raw
1 // Copyright 2012-present Oliver Eilhard. All rights reserved.
2 // Use of this source code is governed by a MIT-license.
3 // See http://olivere.mit-license.org/license.txt for details.
4
5 package elastic
6
7 import "errors"
8
9 // PercolatorQuery can be used to match queries stored in an index.
10 //
11 // For more details, see
12 // https://www.elastic.co/guide/en/elasticsearch/reference/5.x/query-dsl-percolate-query.html
13 type PercolatorQuery struct {
14 field string
15 documentType string
16 document interface{}
17 indexedDocumentIndex string
18 indexedDocumentType string
19 indexedDocumentId string
20 indexedDocumentRouting string
21 indexedDocumentPreference string
22 indexedDocumentVersion *int64
23 }
24
25 // NewPercolatorQuery creates and initializes a new Percolator query.
26 func NewPercolatorQuery() *PercolatorQuery {
27 return &PercolatorQuery{}
28 }
29
30 func (q *PercolatorQuery) Field(field string) *PercolatorQuery {
31 q.field = field
32 return q
33 }
34
35 func (q *PercolatorQuery) DocumentType(typ string) *PercolatorQuery {
36 q.documentType = typ
37 return q
38 }
39
40 func (q *PercolatorQuery) Document(doc interface{}) *PercolatorQuery {
41 q.document = doc
42 return q
43 }
44
45 func (q *PercolatorQuery) IndexedDocumentIndex(index string) *PercolatorQuery {
46 q.indexedDocumentIndex = index
47 return q
48 }
49
50 func (q *PercolatorQuery) IndexedDocumentType(typ string) *PercolatorQuery {
51 q.indexedDocumentType = typ
52 return q
53 }
54
55 func (q *PercolatorQuery) IndexedDocumentId(id string) *PercolatorQuery {
56 q.indexedDocumentId = id
57 return q
58 }
59
60 func (q *PercolatorQuery) IndexedDocumentRouting(routing string) *PercolatorQuery {
61 q.indexedDocumentRouting = routing
62 return q
63 }
64
65 func (q *PercolatorQuery) IndexedDocumentPreference(preference string) *PercolatorQuery {
66 q.indexedDocumentPreference = preference
67 return q
68 }
69
70 func (q *PercolatorQuery) IndexedDocumentVersion(version int64) *PercolatorQuery {
71 q.indexedDocumentVersion = &version
72 return q
73 }
74
75 // Source returns JSON for the percolate query.
76 func (q *PercolatorQuery) Source() (interface{}, error) {
77 if len(q.field) == 0 {
78 return nil, errors.New("elastic: Field is required in PercolatorQuery")
79 }
80 if len(q.documentType) == 0 {
81 return nil, errors.New("elastic: DocumentType is required in PercolatorQuery")
82 }
83 if q.document == nil {
84 return nil, errors.New("elastic: Document is required in PercolatorQuery")
85 }
86
87 // {
88 // "percolate" : { ... }
89 // }
90 source := make(map[string]interface{})
91 params := make(map[string]interface{})
92 source["percolate"] = params
93 params["field"] = q.field
94 params["document_type"] = q.documentType
95 params["document"] = q.document
96 if len(q.indexedDocumentIndex) > 0 {
97 params["index"] = q.indexedDocumentIndex
98 }
99 if len(q.indexedDocumentType) > 0 {
100 params["type"] = q.indexedDocumentType
101 }
102 if len(q.indexedDocumentId) > 0 {
103 params["id"] = q.indexedDocumentId
104 }
105 if len(q.indexedDocumentRouting) > 0 {
106 params["routing"] = q.indexedDocumentRouting
107 }
108 if len(q.indexedDocumentPreference) > 0 {
109 params["preference"] = q.indexedDocumentPreference
110 }
111 if q.indexedDocumentVersion != nil {
112 params["version"] = *q.indexedDocumentVersion
113 }
114 return source, nil
115 }