| 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 |
// DisMaxQuery is a query that generates the union of documents produced by |
| 8 |
// its subqueries, and that scores each document with the maximum score |
| 9 |
// for that document as produced by any subquery, plus a tie breaking |
| 10 |
// increment for any additional matching subqueries. |
| 11 |
// |
| 12 |
// For more details, see: |
| 13 |
// https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-dis-max-query.html |
| 14 |
type DisMaxQuery struct { |
| 15 |
queries []Query |
| 16 |
boost *float64 |
| 17 |
tieBreaker *float64 |
| 18 |
queryName string |
| 19 |
} |
| 20 |
|
| 21 |
// NewDisMaxQuery creates and initializes a new dis max query. |
| 22 |
func NewDisMaxQuery() *DisMaxQuery { |
| 23 |
return &DisMaxQuery{ |
| 24 |
queries: make([]Query, 0), |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
// Query adds one or more queries to the dis max query. |
| 29 |
func (q *DisMaxQuery) Query(queries ...Query) *DisMaxQuery { |
| 30 |
q.queries = append(q.queries, queries...) |
| 31 |
return q |
| 32 |
} |
| 33 |
|
| 34 |
// Boost sets the boost for this query. Documents matching this query will |
| 35 |
// (in addition to the normal weightings) have their score multiplied by |
| 36 |
// the boost provided. |
| 37 |
func (q *DisMaxQuery) Boost(boost float64) *DisMaxQuery { |
| 38 |
q.boost = &boost |
| 39 |
return q |
| 40 |
} |
| 41 |
|
| 42 |
// TieBreaker is the factor by which the score of each non-maximum disjunct |
| 43 |
// for a document is multiplied with and added into the final score. |
| 44 |
// |
| 45 |
// If non-zero, the value should be small, on the order of 0.1, which says |
| 46 |
// that 10 occurrences of word in a lower-scored field that is also in a |
| 47 |
// higher scored field is just as good as a unique word in the lower scored |
| 48 |
// field (i.e., one that is not in any higher scored field). |
| 49 |
func (q *DisMaxQuery) TieBreaker(tieBreaker float64) *DisMaxQuery { |
| 50 |
q.tieBreaker = &tieBreaker |
| 51 |
return q |
| 52 |
} |
| 53 |
|
| 54 |
// QueryName sets the query name for the filter that can be used |
| 55 |
// when searching for matched filters per hit. |
| 56 |
func (q *DisMaxQuery) QueryName(queryName string) *DisMaxQuery { |
| 57 |
q.queryName = queryName |
| 58 |
return q |
| 59 |
} |
| 60 |
|
| 61 |
// Source returns the JSON serializable content for this query. |
| 62 |
func (q *DisMaxQuery) Source() (interface{}, error) { |
| 63 |
// { |
| 64 |
// "dis_max" : { |
| 65 |
// "tie_breaker" : 0.7, |
| 66 |
// "boost" : 1.2, |
| 67 |
// "queries" : { |
| 68 |
// { |
| 69 |
// "term" : { "age" : 34 } |
| 70 |
// }, |
| 71 |
// { |
| 72 |
// "term" : { "age" : 35 } |
| 73 |
// } |
| 74 |
// ] |
| 75 |
// } |
| 76 |
// } |
| 77 |
|
| 78 |
query := make(map[string]interface{}) |
| 79 |
params := make(map[string]interface{}) |
| 80 |
query["dis_max"] = params |
| 81 |
|
| 82 |
if q.tieBreaker != nil { |
| 83 |
params["tie_breaker"] = *q.tieBreaker |
| 84 |
} |
| 85 |
if q.boost != nil { |
| 86 |
params["boost"] = *q.boost |
| 87 |
} |
| 88 |
if q.queryName != "" { |
| 89 |
params["_name"] = q.queryName |
| 90 |
} |
| 91 |
|
| 92 |
// queries |
| 93 |
var clauses []interface{} |
| 94 |
for _, subQuery := range q.queries { |
| 95 |
src, err := subQuery.Source() |
| 96 |
if err != nil { |
| 97 |
return nil, err |
| 98 |
} |
| 99 |
clauses = append(clauses, src) |
| 100 |
} |
| 101 |
params["queries"] = clauses |
| 102 |
|
| 103 |
return query, nil |
| 104 |
} |