Debian Codegraph @

search_queries_boosting.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 // A boosting query can be used to effectively
8 // demote results that match a given query.
9 // For more details, see:
10 // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-boosting-query.html
11 type BoostingQuery struct {
12 Query
13 positiveClause Query
14 negativeClause Query
15 negativeBoost *float64
16 boost *float64
17 }
18
19 // Creates a new boosting query.
20 func NewBoostingQuery() *BoostingQuery {
21 return &BoostingQuery{}
22 }
23
24 func (q *BoostingQuery) Positive(positive Query) *BoostingQuery {
25 q.positiveClause = positive
26 return q
27 }
28
29 func (q *BoostingQuery) Negative(negative Query) *BoostingQuery {
30 q.negativeClause = negative
31 return q
32 }
33
34 func (q *BoostingQuery) NegativeBoost(negativeBoost float64) *BoostingQuery {
35 q.negativeBoost = &negativeBoost
36 return q
37 }
38
39 func (q *BoostingQuery) Boost(boost float64) *BoostingQuery {
40 q.boost = &boost
41 return q
42 }
43
44 // Creates the query source for the boosting query.
45 func (q *BoostingQuery) Source() (interface{}, error) {
46 // {
47 // "boosting" : {
48 // "positive" : {
49 // "term" : {
50 // "field1" : "value1"
51 // }
52 // },
53 // "negative" : {
54 // "term" : {
55 // "field2" : "value2"
56 // }
57 // },
58 // "negative_boost" : 0.2
59 // }
60 // }
61
62 query := make(map[string]interface{})
63
64 boostingClause := make(map[string]interface{})
65 query["boosting"] = boostingClause
66
67 // Negative and positive clause as well as negative boost
68 // are mandatory in the Java client.
69
70 // positive
71 if q.positiveClause != nil {
72 src, err := q.positiveClause.Source()
73 if err != nil {
74 return nil, err
75 }
76 boostingClause["positive"] = src
77 }
78
79 // negative
80 if q.negativeClause != nil {
81 src, err := q.negativeClause.Source()
82 if err != nil {
83 return nil, err
84 }
85 boostingClause["negative"] = src
86 }
87
88 if q.negativeBoost != nil {
89 boostingClause["negative_boost"] = *q.negativeBoost
90 }
91
92 if q.boost != nil {
93 boostingClause["boost"] = *q.boost
94 }
95
96 return query, nil
97 }