| 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 |
// FunctionScoreQuery allows you to modify the score of documents that |
| 8 |
// are retrieved by a query. This can be useful if, for example, |
| 9 |
// a score function is computationally expensive and it is sufficient |
| 10 |
// to compute the score on a filtered set of documents. |
| 11 |
// |
| 12 |
// For more details, see |
| 13 |
// https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-function-score-query.html |
| 14 |
type FunctionScoreQuery struct { |
| 15 |
query Query |
| 16 |
filter Query |
| 17 |
boost *float64 |
| 18 |
maxBoost *float64 |
| 19 |
scoreMode string |
| 20 |
boostMode string |
| 21 |
filters []Query |
| 22 |
scoreFuncs []ScoreFunction |
| 23 |
minScore *float64 |
| 24 |
weight *float64 |
| 25 |
} |
| 26 |
|
| 27 |
// NewFunctionScoreQuery creates and initializes a new function score query. |
| 28 |
func NewFunctionScoreQuery() *FunctionScoreQuery { |
| 29 |
return &FunctionScoreQuery{ |
| 30 |
filters: make([]Query, 0), |
| 31 |
scoreFuncs: make([]ScoreFunction, 0), |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
// Query sets the query for the function score query. |
| 36 |
func (q *FunctionScoreQuery) Query(query Query) *FunctionScoreQuery { |
| 37 |
q.query = query |
| 38 |
return q |
| 39 |
} |
| 40 |
|
| 41 |
// Filter sets the filter for the function score query. |
| 42 |
func (q *FunctionScoreQuery) Filter(filter Query) *FunctionScoreQuery { |
| 43 |
q.filter = filter |
| 44 |
return q |
| 45 |
} |
| 46 |
|
| 47 |
// Add adds a score function that will execute on all the documents |
| 48 |
// matching the filter. |
| 49 |
func (q *FunctionScoreQuery) Add(filter Query, scoreFunc ScoreFunction) *FunctionScoreQuery { |
| 50 |
q.filters = append(q.filters, filter) |
| 51 |
q.scoreFuncs = append(q.scoreFuncs, scoreFunc) |
| 52 |
return q |
| 53 |
} |
| 54 |
|
| 55 |
// AddScoreFunc adds a score function that will execute the function on all documents. |
| 56 |
func (q *FunctionScoreQuery) AddScoreFunc(scoreFunc ScoreFunction) *FunctionScoreQuery { |
| 57 |
q.filters = append(q.filters, nil) |
| 58 |
q.scoreFuncs = append(q.scoreFuncs, scoreFunc) |
| 59 |
return q |
| 60 |
} |
| 61 |
|
| 62 |
// ScoreMode defines how results of individual score functions will be aggregated. |
| 63 |
// Can be first, avg, max, sum, min, or multiply. |
| 64 |
func (q *FunctionScoreQuery) ScoreMode(scoreMode string) *FunctionScoreQuery { |
| 65 |
q.scoreMode = scoreMode |
| 66 |
return q |
| 67 |
} |
| 68 |
|
| 69 |
// BoostMode defines how the combined result of score functions will |
| 70 |
// influence the final score together with the sub query score. |
| 71 |
func (q *FunctionScoreQuery) BoostMode(boostMode string) *FunctionScoreQuery { |
| 72 |
q.boostMode = boostMode |
| 73 |
return q |
| 74 |
} |
| 75 |
|
| 76 |
// MaxBoost is the maximum boost that will be applied by function score. |
| 77 |
func (q *FunctionScoreQuery) MaxBoost(maxBoost float64) *FunctionScoreQuery { |
| 78 |
q.maxBoost = &maxBoost |
| 79 |
return q |
| 80 |
} |
| 81 |
|
| 82 |
// Boost sets the boost for this query. Documents matching this query will |
| 83 |
// (in addition to the normal weightings) have their score multiplied by the |
| 84 |
// boost provided. |
| 85 |
func (q *FunctionScoreQuery) Boost(boost float64) *FunctionScoreQuery { |
| 86 |
q.boost = &boost |
| 87 |
return q |
| 88 |
} |
| 89 |
|
| 90 |
// MinScore sets the minimum score. |
| 91 |
func (q *FunctionScoreQuery) MinScore(minScore float64) *FunctionScoreQuery { |
| 92 |
q.minScore = &minScore |
| 93 |
return q |
| 94 |
} |
| 95 |
|
| 96 |
// Source returns JSON for the function score query. |
| 97 |
func (q *FunctionScoreQuery) Source() (interface{}, error) { |
| 98 |
source := make(map[string]interface{}) |
| 99 |
query := make(map[string]interface{}) |
| 100 |
source["function_score"] = query |
| 101 |
|
| 102 |
if q.query != nil { |
| 103 |
src, err := q.query.Source() |
| 104 |
if err != nil { |
| 105 |
return nil, err |
| 106 |
} |
| 107 |
query["query"] = src |
| 108 |
} |
| 109 |
if q.filter != nil { |
| 110 |
src, err := q.filter.Source() |
| 111 |
if err != nil { |
| 112 |
return nil, err |
| 113 |
} |
| 114 |
query["filter"] = src |
| 115 |
} |
| 116 |
|
| 117 |
if len(q.filters) > 0 { |
| 118 |
funcs := make([]interface{}, len(q.filters)) |
| 119 |
for i, filter := range q.filters { |
| 120 |
hsh := make(map[string]interface{}) |
| 121 |
if filter != nil { |
| 122 |
src, err := filter.Source() |
| 123 |
if err != nil { |
| 124 |
return nil, err |
| 125 |
} |
| 126 |
hsh["filter"] = src |
| 127 |
} |
| 128 |
// Weight needs to be serialized on this level. |
| 129 |
if weight := q.scoreFuncs[i].GetWeight(); weight != nil { |
| 130 |
hsh["weight"] = weight |
| 131 |
} |
| 132 |
// Serialize the score function |
| 133 |
src, err := q.scoreFuncs[i].Source() |
| 134 |
if err != nil { |
| 135 |
return nil, err |
| 136 |
} |
| 137 |
hsh[q.scoreFuncs[i].Name()] = src |
| 138 |
funcs[i] = hsh |
| 139 |
} |
| 140 |
query["functions"] = funcs |
| 141 |
} |
| 142 |
|
| 143 |
if q.scoreMode != "" { |
| 144 |
query["score_mode"] = q.scoreMode |
| 145 |
} |
| 146 |
if q.boostMode != "" { |
| 147 |
query["boost_mode"] = q.boostMode |
| 148 |
} |
| 149 |
if q.maxBoost != nil { |
| 150 |
query["max_boost"] = *q.maxBoost |
| 151 |
} |
| 152 |
if q.boost != nil { |
| 153 |
query["boost"] = *q.boost |
| 154 |
} |
| 155 |
if q.minScore != nil { |
| 156 |
query["min_score"] = *q.minScore |
| 157 |
} |
| 158 |
|
| 159 |
return source, nil |
| 160 |
} |