Skip to content

Commit

Permalink
CBB-1170: support _name on multi-match
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesmitharoo committed Sep 5, 2024
1 parent 274a03a commit a3d428a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
21 changes: 11 additions & 10 deletions query_multi_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,24 @@ type multiMatchParams struct {
MinMatch string `structs:"minimum_should_match,omitempty"`
ZeroTerms ZeroTerms `structs:"zero_terms_query,string,omitempty"`
Slp uint16 `structs:"slop,omitempty"`
Name string `structs:"_name,omitempty"`
}

// MultiMatch creates a new query of type "multi_match"
func MultiMatch(simpleQuery ...interface{}) *MultiMatchQuery {
return newMultiMatch(simpleQuery...)
func MultiMatch(fieldNames []string, simpleQuery ...interface{}) *MultiMatchQuery {
return newMultiMatch(fieldNames, simpleQuery...)
}

func newMultiMatch(simpleQuery ...interface{}) *MultiMatchQuery {
func newMultiMatch(fieldNames []string, simpleQuery ...interface{}) *MultiMatchQuery {
var qry interface{}
if len(simpleQuery) > 0 {
qry = simpleQuery[len(simpleQuery)-1]
}

return &MultiMatchQuery{
params: multiMatchParams{
Qry: qry,
Fields: fieldNames,
Qry: qry,
},
}
}
Expand All @@ -68,12 +70,6 @@ func (q *MultiMatchQuery) Analyzer(a string) *MultiMatchQuery {
return q
}

// Fields sets the fields used in the query
func (q *MultiMatchQuery) Fields(a ...string) *MultiMatchQuery {
q.params.Fields = append(q.params.Fields, a...)
return q
}

// AutoGenerateSynonymsPhraseQuery sets the "auto_generate_synonyms_phrase_query"
// boolean.
func (q *MultiMatchQuery) AutoGenerateSynonymsPhraseQuery(b bool) *MultiMatchQuery {
Expand Down Expand Up @@ -164,6 +160,11 @@ func (q *MultiMatchQuery) ZeroTermsQuery(s ZeroTerms) *MultiMatchQuery {
return q
}

func (q *MultiMatchQuery) Name(name string) Mappable {
q.params.Name = name
return q
}

// MatchType is an enumeration type representing supported values for a
// multi match query's "type" parameter.
type MultiMatchType uint8
Expand Down
9 changes: 5 additions & 4 deletions query_multi_match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ func TestMultiMatch(t *testing.T) {
runMapTests(t, []mapTest{
{
"simple multi_match",
MultiMatch("value1", "value2").Fields("title"),
MultiMatch([]string{"title"}, "value1", "value2"),
map[string]interface{}{
"multi_match": map[string]interface{}{
"fields": []string{"title"},
Expand All @@ -18,10 +18,9 @@ func TestMultiMatch(t *testing.T) {
},
{
"multi_match all params",
MultiMatch("original").
MultiMatch([]string{"title", "body"}, "original").
Query("test").
Analyzer("stop").
Fields("title", "body").
AutoGenerateSynonymsPhraseQuery(true).
Fuzziness("AUTO").
MaxExpansions(16).
Expand All @@ -35,7 +34,8 @@ func TestMultiMatch(t *testing.T) {
Type(MatchTypePhrase).
MinimumShouldMatch("3<90%").
Slop(2).
ZeroTermsQuery(ZeroTermsAll),
ZeroTermsQuery(ZeroTermsAll).
Name("query_name"),
map[string]interface{}{
"multi_match": map[string]interface{}{
"analyzer": "stop",
Expand All @@ -55,6 +55,7 @@ func TestMultiMatch(t *testing.T) {
"slop": 2,
"query": "test",
"fields": []string{"title", "body"},
"_name": "query_name",
},
},
},
Expand Down

0 comments on commit a3d428a

Please sign in to comment.