Skip to content

Commit

Permalink
MB-61216: Interpret empty query (in search request) as a match_none (
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinavdangeti committed Mar 20, 2024
1 parent c9edd4c commit a1e4a0e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
11 changes: 11 additions & 0 deletions search/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,22 @@ func ParsePreSearchData(input []byte) (map[string]interface{}, error) {
// ParseQuery deserializes a JSON representation of
// a Query object.
func ParseQuery(input []byte) (Query, error) {
if len(input) == 0 {
// interpret as a match_none query
return NewMatchNoneQuery(), nil
}

var tmp map[string]interface{}
err := util.UnmarshalJSON(input, &tmp)
if err != nil {
return nil, err
}

if len(tmp) == 0 {
// interpret as a match_none query
return NewMatchNoneQuery(), nil
}

_, hasFuzziness := tmp["fuzziness"]
_, isMatchQuery := tmp["match"]
_, isMatchPhraseQuery := tmp["match_phrase"]
Expand Down
22 changes: 22 additions & 0 deletions search/query/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,3 +763,25 @@ func TestGeoShapeQuery(t *testing.T) {
}
}
}

func TestParseEmptyQuery(t *testing.T) {
var qBytes []byte
rv, err := ParseQuery(qBytes)
if err != nil {
t.Fatal(err)
}
expect := NewMatchNoneQuery()
if !reflect.DeepEqual(rv, expect) {
t.Errorf("[1] Expected %#v, got %#v", expect, rv)
}

qBytes = []byte(`{}`)
rv, err = ParseQuery(qBytes)
if err != nil {
t.Fatal(err)
}
expect = NewMatchNoneQuery()
if !reflect.DeepEqual(rv, expect) {
t.Errorf("[2] Expected %#v, got %#v", expect, rv)
}
}

0 comments on commit a1e4a0e

Please sign in to comment.