Debian Codegraph @

search_queries_prefix.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 // PrefixQuery matches documents that have fields containing terms
8 // with a specified prefix (not analyzed).
9 //
10 // For more details, see
11 // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-prefix-query.html
12 type PrefixQuery struct {
13 name string
14 prefix string
15 boost *float64
16 rewrite string
17 queryName string
18 }
19
20 // NewPrefixQuery creates and initializes a new PrefixQuery.
21 func NewPrefixQuery(name string, prefix string) *PrefixQuery {
22 return &PrefixQuery{name: name, prefix: prefix}
23 }
24
25 // Boost sets the boost for this query.
26 func (q *PrefixQuery) Boost(boost float64) *PrefixQuery {
27 q.boost = &boost
28 return q
29 }
30
31 func (q *PrefixQuery) Rewrite(rewrite string) *PrefixQuery {
32 q.rewrite = rewrite
33 return q
34 }
35
36 // QueryName sets the query name for the filter that can be used when
37 // searching for matched_filters per hit.
38 func (q *PrefixQuery) QueryName(queryName string) *PrefixQuery {
39 q.queryName = queryName
40 return q
41 }
42
43 // Source returns JSON for the query.
44 func (q *PrefixQuery) Source() (interface{}, error) {
45 source := make(map[string]interface{})
46 query := make(map[string]interface{})
47 source["prefix"] = query
48
49 if q.boost == nil && q.rewrite == "" && q.queryName == "" {
50 query[q.name] = q.prefix
51 } else {
52 subQuery := make(map[string]interface{})
53 subQuery["value"] = q.prefix
54 if q.boost != nil {
55 subQuery["boost"] = *q.boost
56 }
57 if q.rewrite != "" {
58 subQuery["rewrite"] = q.rewrite
59 }
60 if q.queryName != "" {
61 subQuery["_name"] = q.queryName
62 }
63 query[q.name] = subQuery
64 }
65
66 return source, nil
67 }