File: search_queries_multi_match.go

package info (click to toggle)
golang-gopkg-olivere-elastic.v2 2.0.12-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 1,964 kB
  • sloc: makefile: 17
file content (253 lines) | stat: -rw-r--r-- 6,121 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright 2012-2015 Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.

package elastic

import (
	"fmt"
	"strings"
)

// The multi_match query builds further on top of the match query by allowing multiple fields to be specified.
// For more details, see:
// http://www.elasticsearch.org/guide/reference/query-dsl/multi-match-query.html
type MultiMatchQuery struct {
	Query
	text               interface{}
	fields             []string
	fieldBoosts        map[string]*float32
	matchQueryType     string // best_fields, most_fields, cross_fields, phrase, phrase_prefix
	operator           string // and / or
	analyzer           string
	boost              *float32
	slop               *int
	fuzziness          string
	prefixLength       *int
	maxExpansions      *int
	minimumShouldMatch string
	rewrite            string
	fuzzyRewrite       string
	useDisMax          *bool
	tieBreaker         *float32
	lenient            *bool
	cutoffFrequency    *float32
	zeroTermsQuery     string
	queryName          string
}

func NewMultiMatchQuery(text interface{}, fields ...string) MultiMatchQuery {
	q := MultiMatchQuery{
		text:        text,
		fields:      make([]string, 0),
		fieldBoosts: make(map[string]*float32),
	}
	q.fields = append(q.fields, fields...)
	return q
}

func (q MultiMatchQuery) Field(field string) MultiMatchQuery {
	q.fields = append(q.fields, field)
	return q
}

func (q MultiMatchQuery) FieldWithBoost(field string, boost float32) MultiMatchQuery {
	q.fields = append(q.fields, field)
	q.fieldBoosts[field] = &boost
	return q
}

// Type can be: "best_fields", "boolean", "most_fields", "cross_fields",
// "phrase", or "phrase_prefix".
func (q MultiMatchQuery) Type(matchQueryType string) MultiMatchQuery {
	zero := float32(0.0)
	one := float32(1.0)

	switch strings.ToLower(matchQueryType) {
	default: // best_fields / boolean
		q.matchQueryType = "best_fields"
		q.tieBreaker = &zero
	case "most_fields":
		q.matchQueryType = "most_fields"
		q.tieBreaker = &one
	case "cross_fields":
		q.matchQueryType = "cross_fields"
		q.tieBreaker = &zero
	case "phrase":
		q.matchQueryType = "phrase"
		q.tieBreaker = &zero
	case "phrase_prefix":
		q.matchQueryType = "phrase_prefix"
		q.tieBreaker = &zero
	}
	return q
}

func (q MultiMatchQuery) Operator(operator string) MultiMatchQuery {
	q.operator = operator
	return q
}

func (q MultiMatchQuery) Analyzer(analyzer string) MultiMatchQuery {
	q.analyzer = analyzer
	return q
}

func (q MultiMatchQuery) Boost(boost float32) MultiMatchQuery {
	q.boost = &boost
	return q
}

func (q MultiMatchQuery) Slop(slop int) MultiMatchQuery {
	q.slop = &slop
	return q
}

func (q MultiMatchQuery) Fuzziness(fuzziness string) MultiMatchQuery {
	q.fuzziness = fuzziness
	return q
}

func (q MultiMatchQuery) PrefixLength(prefixLength int) MultiMatchQuery {
	q.prefixLength = &prefixLength
	return q
}

func (q MultiMatchQuery) MaxExpansions(maxExpansions int) MultiMatchQuery {
	q.maxExpansions = &maxExpansions
	return q
}

func (q MultiMatchQuery) MinimumShouldMatch(minimumShouldMatch string) MultiMatchQuery {
	q.minimumShouldMatch = minimumShouldMatch
	return q
}

func (q MultiMatchQuery) Rewrite(rewrite string) MultiMatchQuery {
	q.rewrite = rewrite
	return q
}

func (q MultiMatchQuery) FuzzyRewrite(fuzzyRewrite string) MultiMatchQuery {
	q.fuzzyRewrite = fuzzyRewrite
	return q
}

// Deprecated.
func (q MultiMatchQuery) UseDisMax(useDisMax bool) MultiMatchQuery {
	q.useDisMax = &useDisMax
	return q
}

func (q MultiMatchQuery) TieBreaker(tieBreaker float32) MultiMatchQuery {
	q.tieBreaker = &tieBreaker
	return q
}

func (q MultiMatchQuery) Lenient(lenient bool) MultiMatchQuery {
	q.lenient = &lenient
	return q
}

func (q MultiMatchQuery) CutoffFrequency(cutoff float32) MultiMatchQuery {
	q.cutoffFrequency = &cutoff
	return q
}

// ZeroTermsQuery can be "all" or "none".
func (q MultiMatchQuery) ZeroTermsQuery(zeroTermsQuery string) MultiMatchQuery {
	q.zeroTermsQuery = zeroTermsQuery
	return q
}

func (q MultiMatchQuery) QueryName(queryName string) MultiMatchQuery {
	q.queryName = queryName
	return q
}

func (q MultiMatchQuery) Source() interface{} {
	//
	// {
	//   "multi_match" : {
	//     "query" : "this is a test",
	//     "fields" : [ "subject", "message" ]
	//   }
	// }

	source := make(map[string]interface{})

	multiMatch := make(map[string]interface{})
	source["multi_match"] = multiMatch

	multiMatch["query"] = q.text

	if len(q.fields) > 0 {
		fields := make([]string, 0)
		for _, field := range q.fields {
			if boost, found := q.fieldBoosts[field]; found {
				if boost != nil {
					fields = append(fields, fmt.Sprintf("%s^%f", field, *boost))
				} else {
					fields = append(fields, field)
				}
			} else {
				fields = append(fields, field)
			}
		}
		multiMatch["fields"] = fields
	}

	if q.matchQueryType != "" {
		multiMatch["type"] = q.matchQueryType
	}

	if q.operator != "" {
		multiMatch["operator"] = q.operator
	}
	if q.analyzer != "" {
		multiMatch["analyzer"] = q.analyzer
	}
	if q.boost != nil {
		multiMatch["boost"] = *q.boost
	}
	if q.slop != nil {
		multiMatch["slop"] = *q.slop
	}
	if q.fuzziness != "" {
		multiMatch["fuzziness"] = q.fuzziness
	}
	if q.prefixLength != nil {
		multiMatch["prefix_length"] = *q.prefixLength
	}
	if q.maxExpansions != nil {
		multiMatch["max_expansions"] = *q.maxExpansions
	}
	if q.minimumShouldMatch != "" {
		multiMatch["minimum_should_match"] = q.minimumShouldMatch
	}
	if q.rewrite != "" {
		multiMatch["rewrite"] = q.rewrite
	}
	if q.fuzzyRewrite != "" {
		multiMatch["fuzzy_rewrite"] = q.fuzzyRewrite
	}
	if q.useDisMax != nil {
		multiMatch["use_dis_max"] = *q.useDisMax
	}
	if q.tieBreaker != nil {
		multiMatch["tie_breaker"] = *q.tieBreaker
	}
	if q.lenient != nil {
		multiMatch["lenient"] = *q.lenient
	}
	if q.cutoffFrequency != nil {
		multiMatch["cutoff_frequency"] = *q.cutoffFrequency
	}
	if q.zeroTermsQuery != "" {
		multiMatch["zero_terms_query"] = q.zeroTermsQuery
	}
	if q.queryName != "" {
		multiMatch["_name"] = q.queryName
	}
	return source
}