Skip to content

Commit

Permalink
Enable support for parsing empty query bodies (#114)
Browse files Browse the repository at this point in the history
When nobody provided when searching, it should be interpreted as `match-all`. This works because later on we search for `query` in the body, and if it's not present, we return `newSimpleQuery(NewSimpleStatement(""), true)`
  • Loading branch information
pivovarit authored May 15, 2024
1 parent 6f206fa commit 87b536e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
12 changes: 7 additions & 5 deletions quesma/queryparser/query_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ func NewCompoundStatementNoFieldName(stmt string) Statement {
func (cw *ClickhouseQueryTranslator) ParseQuery(queryAsJson string) (SimpleQuery, model.SearchQueryInfo, model.Highlighter) {
cw.ClearTokensToHighlight()
queryAsMap := make(QueryMap)
err := json.Unmarshal([]byte(queryAsJson), &queryAsMap)
if err != nil {
logger.ErrorWithCtx(cw.Ctx).Err(err).Msg("error parsing query request's JSON")
return newSimpleQuery(NewSimpleStatement("invalid JSON (ParseQuery)"), false),
model.NewSearchQueryInfoNone(), NewEmptyHighlighter()
if queryAsJson != "" {
err := json.Unmarshal([]byte(queryAsJson), &queryAsMap)
if err != nil {
logger.ErrorWithCtx(cw.Ctx).Err(err).Msg("error parsing query request's JSON")
return newSimpleQuery(NewSimpleStatement("invalid JSON (ParseQuery)"), false),
model.NewSearchQueryInfoNone(), NewEmptyHighlighter()
}
}

// we must parse "highlights" here, because it is stripped from the queryAsMap later
Expand Down
8 changes: 8 additions & 0 deletions quesma/testdata/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -1875,6 +1875,14 @@ var TestsSearch = []SearchTestCase{
[]model.Query{justSimplestWhere(`"message" iLIKE '%User logged out%' AND "message" iLIKE '%User logged out%'`)},
[]string{qToStr(justSimplestWhere(`"message" iLIKE '%User logged out%' AND "message" iLIKE '%User logged out%'`))},
},
{ // [31]
"Match all (empty query)",
``,
[]string{""},
model.Normal,
[]model.Query{newSimplestQuery()},
[]string{qToStr(newSimplestQuery())},
},
}

var TestsSearchNoAttrs = []SearchTestCase{
Expand Down

0 comments on commit 87b536e

Please sign in to comment.