| 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 |
import ( |
| 8 |
"fmt" |
| 9 |
"strings" |
| 10 |
) |
| 11 |
|
| 12 |
// MultiMatchQuery builds on the MatchQuery to allow multi-field queries. |
| 13 |
// |
| 14 |
// For more details, see |
| 15 |
// https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-multi-match-query.html |
| 16 |
type MultiMatchQuery struct { |
| 17 |
text interface{} |
| 18 |
fields []string |
| 19 |
fieldBoosts map[string]*float64 |
| 20 |
typ string // best_fields, boolean, most_fields, cross_fields, phrase, phrase_prefix |
| 21 |
operator string // AND or OR |
| 22 |
analyzer string |
| 23 |
boost *float64 |
| 24 |
slop *int |
| 25 |
fuzziness string |
| 26 |
prefixLength *int |
| 27 |
maxExpansions *int |
| 28 |
minimumShouldMatch string |
| 29 |
rewrite string |
| 30 |
fuzzyRewrite string |
| 31 |
tieBreaker *float64 |
| 32 |
lenient *bool |
| 33 |
cutoffFrequency *float64 |
| 34 |
zeroTermsQuery string |
| 35 |
queryName string |
| 36 |
} |
| 37 |
|
| 38 |
// MultiMatchQuery creates and initializes a new MultiMatchQuery. |
| 39 |
func NewMultiMatchQuery(text interface{}, fields ...string) *MultiMatchQuery { |
| 40 |
q := &MultiMatchQuery{ |
| 41 |
text: text, |
| 42 |
fields: make([]string, 0), |
| 43 |
fieldBoosts: make(map[string]*float64), |
| 44 |
} |
| 45 |
q.fields = append(q.fields, fields...) |
| 46 |
return q |
| 47 |
} |
| 48 |
|
| 49 |
// Field adds a field to run the multi match against. |
| 50 |
func (q *MultiMatchQuery) Field(field string) *MultiMatchQuery { |
| 51 |
q.fields = append(q.fields, field) |
| 52 |
return q |
| 53 |
} |
| 54 |
|
| 55 |
// FieldWithBoost adds a field to run the multi match against with a specific boost. |
| 56 |
func (q *MultiMatchQuery) FieldWithBoost(field string, boost float64) *MultiMatchQuery { |
| 57 |
q.fields = append(q.fields, field) |
| 58 |
q.fieldBoosts[field] = &boost |
| 59 |
return q |
| 60 |
} |
| 61 |
|
| 62 |
// Type can be "best_fields", "boolean", "most_fields", "cross_fields", |
| 63 |
// "phrase", or "phrase_prefix". |
| 64 |
func (q *MultiMatchQuery) Type(typ string) *MultiMatchQuery { |
| 65 |
var zero = float64(0.0) |
| 66 |
var one = float64(1.0) |
| 67 |
|
| 68 |
switch strings.ToLower(typ) { |
| 69 |
default: // best_fields / boolean |
| 70 |
q.typ = "best_fields" |
| 71 |
q.tieBreaker = &zero |
| 72 |
case "most_fields": |
| 73 |
q.typ = "most_fields" |
| 74 |
q.tieBreaker = &one |
| 75 |
case "cross_fields": |
| 76 |
q.typ = "cross_fields" |
| 77 |
q.tieBreaker = &zero |
| 78 |
case "phrase": |
| 79 |
q.typ = "phrase" |
| 80 |
q.tieBreaker = &zero |
| 81 |
case "phrase_prefix": |
| 82 |
q.typ = "phrase_prefix" |
| 83 |
q.tieBreaker = &zero |
| 84 |
} |
| 85 |
return q |
| 86 |
} |
| 87 |
|
| 88 |
// Operator sets the operator to use when using boolean query. |
| 89 |
// It can be either AND or OR (default). |
| 90 |
func (q *MultiMatchQuery) Operator(operator string) *MultiMatchQuery { |
| 91 |
q.operator = operator |
| 92 |
return q |
| 93 |
} |
| 94 |
|
| 95 |
// Analyzer sets the analyzer to use explicitly. It defaults to use explicit |
| 96 |
// mapping config for the field, or, if not set, the default search analyzer. |
| 97 |
func (q *MultiMatchQuery) Analyzer(analyzer string) *MultiMatchQuery { |
| 98 |
q.analyzer = analyzer |
| 99 |
return q |
| 100 |
} |
| 101 |
|
| 102 |
// Boost sets the boost for this query. |
| 103 |
func (q *MultiMatchQuery) Boost(boost float64) *MultiMatchQuery { |
| 104 |
q.boost = &boost |
| 105 |
return q |
| 106 |
} |
| 107 |
|
| 108 |
// Slop sets the phrase slop if evaluated to a phrase query type. |
| 109 |
func (q *MultiMatchQuery) Slop(slop int) *MultiMatchQuery { |
| 110 |
q.slop = &slop |
| 111 |
return q |
| 112 |
} |
| 113 |
|
| 114 |
// Fuzziness sets the fuzziness used when evaluated to a fuzzy query type. |
| 115 |
// It defaults to "AUTO". |
| 116 |
func (q *MultiMatchQuery) Fuzziness(fuzziness string) *MultiMatchQuery { |
| 117 |
q.fuzziness = fuzziness |
| 118 |
return q |
| 119 |
} |
| 120 |
|
| 121 |
// PrefixLength for the fuzzy process. |
| 122 |
func (q *MultiMatchQuery) PrefixLength(prefixLength int) *MultiMatchQuery { |
| 123 |
q.prefixLength = &prefixLength |
| 124 |
return q |
| 125 |
} |
| 126 |
|
| 127 |
// MaxExpansions is the number of term expansions to use when using fuzzy |
| 128 |
// or prefix type query. It defaults to unbounded so it's recommended |
| 129 |
// to set it to a reasonable value for faster execution. |
| 130 |
func (q *MultiMatchQuery) MaxExpansions(maxExpansions int) *MultiMatchQuery { |
| 131 |
q.maxExpansions = &maxExpansions |
| 132 |
return q |
| 133 |
} |
| 134 |
|
| 135 |
// MinimumShouldMatch represents the minimum number of optional should clauses |
| 136 |
// to match. |
| 137 |
func (q *MultiMatchQuery) MinimumShouldMatch(minimumShouldMatch string) *MultiMatchQuery { |
| 138 |
q.minimumShouldMatch = minimumShouldMatch |
| 139 |
return q |
| 140 |
} |
| 141 |
|
| 142 |
func (q *MultiMatchQuery) Rewrite(rewrite string) *MultiMatchQuery { |
| 143 |
q.rewrite = rewrite |
| 144 |
return q |
| 145 |
} |
| 146 |
|
| 147 |
func (q *MultiMatchQuery) FuzzyRewrite(fuzzyRewrite string) *MultiMatchQuery { |
| 148 |
q.fuzzyRewrite = fuzzyRewrite |
| 149 |
return q |
| 150 |
} |
| 151 |
|
| 152 |
// TieBreaker for "best-match" disjunction queries (OR queries). |
| 153 |
// The tie breaker capability allows documents that match more than one |
| 154 |
// query clause (in this case on more than one field) to be scored better |
| 155 |
// than documents that match only the best of the fields, without confusing |
| 156 |
// this with the better case of two distinct matches in the multiple fields. |
| 157 |
// |
| 158 |
// A tie-breaker value of 1.0 is interpreted as a signal to score queries as |
| 159 |
// "most-match" queries where all matching query clauses are considered for scoring. |
| 160 |
func (q *MultiMatchQuery) TieBreaker(tieBreaker float64) *MultiMatchQuery { |
| 161 |
q.tieBreaker = &tieBreaker |
| 162 |
return q |
| 163 |
} |
| 164 |
|
| 165 |
// Lenient indicates whether format based failures will be ignored. |
| 166 |
func (q *MultiMatchQuery) Lenient(lenient bool) *MultiMatchQuery { |
| 167 |
q.lenient = &lenient |
| 168 |
return q |
| 169 |
} |
| 170 |
|
| 171 |
// CutoffFrequency sets a cutoff value in [0..1] (or absolute number >=1) |
| 172 |
// representing the maximum threshold of a terms document frequency to be |
| 173 |
// considered a low frequency term. |
| 174 |
func (q *MultiMatchQuery) CutoffFrequency(cutoff float64) *MultiMatchQuery { |
| 175 |
q.cutoffFrequency = &cutoff |
| 176 |
return q |
| 177 |
} |
| 178 |
|
| 179 |
// ZeroTermsQuery can be "all" or "none". |
| 180 |
func (q *MultiMatchQuery) ZeroTermsQuery(zeroTermsQuery string) *MultiMatchQuery { |
| 181 |
q.zeroTermsQuery = zeroTermsQuery |
| 182 |
return q |
| 183 |
} |
| 184 |
|
| 185 |
// QueryName sets the query name for the filter that can be used when |
| 186 |
// searching for matched filters per hit. |
| 187 |
func (q *MultiMatchQuery) QueryName(queryName string) *MultiMatchQuery { |
| 188 |
q.queryName = queryName |
| 189 |
return q |
| 190 |
} |
| 191 |
|
| 192 |
// Source returns JSON for the query. |
| 193 |
func (q *MultiMatchQuery) Source() (interface{}, error) { |
| 194 |
// |
| 195 |
// { |
| 196 |
// "multi_match" : { |
| 197 |
// "query" : "this is a test", |
| 198 |
// "fields" : [ "subject", "message" ] |
| 199 |
// } |
| 200 |
// } |
| 201 |
|
| 202 |
source := make(map[string]interface{}) |
| 203 |
|
| 204 |
multiMatch := make(map[string]interface{}) |
| 205 |
source["multi_match"] = multiMatch |
| 206 |
|
| 207 |
multiMatch["query"] = q.text |
| 208 |
|
| 209 |
if len(q.fields) > 0 { |
| 210 |
var fields []string |
| 211 |
for _, field := range q.fields { |
| 212 |
if boost, found := q.fieldBoosts[field]; found { |
| 213 |
if boost != nil { |
| 214 |
fields = append(fields, fmt.Sprintf("%s^%f", field, *boost)) |
| 215 |
} else { |
| 216 |
fields = append(fields, field) |
| 217 |
} |
| 218 |
} else { |
| 219 |
fields = append(fields, field) |
| 220 |
} |
| 221 |
} |
| 222 |
multiMatch["fields"] = fields |
| 223 |
} |
| 224 |
|
| 225 |
if q.typ != "" { |
| 226 |
multiMatch["type"] = q.typ |
| 227 |
} |
| 228 |
|
| 229 |
if q.operator != "" { |
| 230 |
multiMatch["operator"] = q.operator |
| 231 |
} |
| 232 |
if q.analyzer != "" { |
| 233 |
multiMatch["analyzer"] = q.analyzer |
| 234 |
} |
| 235 |
if q.boost != nil { |
| 236 |
multiMatch["boost"] = *q.boost |
| 237 |
} |
| 238 |
if q.slop != nil { |
| 239 |
multiMatch["slop"] = *q.slop |
| 240 |
} |
| 241 |
if q.fuzziness != "" { |
| 242 |
multiMatch["fuzziness"] = q.fuzziness |
| 243 |
} |
| 244 |
if q.prefixLength != nil { |
| 245 |
multiMatch["prefix_length"] = *q.prefixLength |
| 246 |
} |
| 247 |
if q.maxExpansions != nil { |
| 248 |
multiMatch["max_expansions"] = *q.maxExpansions |
| 249 |
} |
| 250 |
if q.minimumShouldMatch != "" { |
| 251 |
multiMatch["minimum_should_match"] = q.minimumShouldMatch |
| 252 |
} |
| 253 |
if q.rewrite != "" { |
| 254 |
multiMatch["rewrite"] = q.rewrite |
| 255 |
} |
| 256 |
if q.fuzzyRewrite != "" { |
| 257 |
multiMatch["fuzzy_rewrite"] = q.fuzzyRewrite |
| 258 |
} |
| 259 |
if q.tieBreaker != nil { |
| 260 |
multiMatch["tie_breaker"] = *q.tieBreaker |
| 261 |
} |
| 262 |
if q.lenient != nil { |
| 263 |
multiMatch["lenient"] = *q.lenient |
| 264 |
} |
| 265 |
if q.cutoffFrequency != nil { |
| 266 |
multiMatch["cutoff_frequency"] = *q.cutoffFrequency |
| 267 |
} |
| 268 |
if q.zeroTermsQuery != "" { |
| 269 |
multiMatch["zero_terms_query"] = q.zeroTermsQuery |
| 270 |
} |
| 271 |
if q.queryName != "" { |
| 272 |
multiMatch["_name"] = q.queryName |
| 273 |
} |
| 274 |
return source, nil |
| 275 |
} |