From a1e4a0e14e74dc05730bc4ed765346589d96fff4 Mon Sep 17 00:00:00 2001 From: Abhi Dangeti Date: Wed, 20 Mar 2024 08:46:17 -0600 Subject: [PATCH] MB-61216: Interpret empty `query` (in search request) as a match_none (#2000) --- search/query/query.go | 11 +++++++++++ search/query/query_test.go | 22 ++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/search/query/query.go b/search/query/query.go index f0172b76b..26ab656e7 100644 --- a/search/query/query.go +++ b/search/query/query.go @@ -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"] diff --git a/search/query/query_test.go b/search/query/query_test.go index 228fb65cd..0028c956b 100644 --- a/search/query/query_test.go +++ b/search/query/query_test.go @@ -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) + } +}