Debian Codegraph @

search_queries_constant_score.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 // ConstantScoreQuery is a query that wraps a filter and simply returns
8 // a constant score equal to the query boost for every document in the filter.
9 //
10 // For more details, see:
11 // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-constant-score-query.html
12 type ConstantScoreQuery struct {
13 filter Query
14 boost *float64
15 }
16
17 // ConstantScoreQuery creates and initializes a new constant score query.
18 func NewConstantScoreQuery(filter Query) *ConstantScoreQuery {
19 return &ConstantScoreQuery{
20 filter: filter,
21 }
22 }
23
24 // Boost sets the boost for this query. Documents matching this query
25 // will (in addition to the normal weightings) have their score multiplied
26 // by the boost provided.
27 func (q *ConstantScoreQuery) Boost(boost float64) *ConstantScoreQuery {
28 q.boost = &boost
29 return q
30 }
31
32 // Source returns the query source.
33 func (q *ConstantScoreQuery) Source() (interface{}, error) {
34 // "constant_score" : {
35 // "filter" : {
36 // ....
37 // },
38 // "boost" : 1.5
39 // }
40
41 query := make(map[string]interface{})
42
43 params := make(map[string]interface{})
44 query["constant_score"] = params
45
46 // filter
47 src, err := q.filter.Source()
48 if err != nil {
49 return nil, err
50 }
51 params["filter"] = src
52
53 // boost
54 if q.boost != nil {
55 params["boost"] = *q.boost
56 }
57
58 return query, nil
59 }