Skip to content

Commit

Permalink
Issue #18: improve with operators
Browse files Browse the repository at this point in the history
  • Loading branch information
idrissneumann committed Dec 1, 2023
1 parent 2dedfcb commit 22424da
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
23 changes: 21 additions & 2 deletions pkg/quickwit/response_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,17 @@ func parseResponse(responses []*es.SearchResponse, targets []*Query, configuredF
return &result, nil
}

func isLuceneOperator(value string) bool {
operators := []string{"or", "and"}
for _, op := range operators {
if strings.ToLower(value) == op {
return true
}
}

return false
}

func parseLuceneQuery(query string) []string {
var keywords []string

Expand All @@ -109,12 +120,20 @@ func parseLuceneQuery(query string) []string {

keyValueMatches := keyValueRegex.FindStringSubmatch(termMatch)
if len(keyValueMatches) <= 1 {
keywords = append(keywords, strings.ReplaceAll(termMatch, "*", ""))
value := strings.ReplaceAll(termMatch, "*", "")
if isLuceneOperator(value) {
continue
}
keywords = append(keywords, value)
continue
}

for _, keyValueMatch := range keyValueMatches[1:] {
keywords = append(keywords, strings.ReplaceAll(keyValueMatch, "*", ""))
value := strings.ReplaceAll(keyValueMatch, "*", "")
if isLuceneOperator(value) {
continue
}
keywords = append(keywords, value)
}
}

Expand Down
10 changes: 9 additions & 1 deletion pkg/quickwit/response_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3207,12 +3207,20 @@ func TestParseLuceneQuery(t *testing.T) {
require.Equal(t, "foo", highlights[0])
})

t.Run("LeyValue query", func(t *testing.T) {
t.Run("KeyValue query", func(t *testing.T) {
query := "foo:bar*"
highlights := parseLuceneQuery(query)
require.Len(t, highlights, 1)
require.Equal(t, "bar", highlights[0])
})

t.Run("MultiKeyValue query", func(t *testing.T) {
query := "foo:bar* AND foo2:bar2"
highlights := parseLuceneQuery(query)
require.Len(t, highlights, 2)
require.Equal(t, "bar", highlights[0])
require.Equal(t, "bar2", highlights[1])
})
}

func TestFlatten(t *testing.T) {
Expand Down

0 comments on commit 22424da

Please sign in to comment.