Skip to content

Commit

Permalink
Issue #18: handle key/value queries
Browse files Browse the repository at this point in the history
  • Loading branch information
idrissneumann committed Dec 1, 2023
1 parent b71b27a commit 2630e2b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
38 changes: 33 additions & 5 deletions pkg/quickwit/response_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,46 @@ 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

termRegex := regexp.MustCompile(`("[^"]+"|\S+)`)
matches := termRegex.FindAllString(query, -1)
keyValueRegex := regexp.MustCompile(`[^:]+:([^:]*)`)
termMatches := termRegex.FindAllString(query, -1)

for _, match := range matches {
if match[0] == '"' && match[len(match)-1] == '"' {
match = match[1 : len(match)-1]
for _, termMatch := range termMatches {
if termMatch[0] == '"' && termMatch[len(termMatches)-1] == '"' {
termMatch = termMatch[1 : len(termMatch)-1]
}

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

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

return keywords
Expand Down
15 changes: 15 additions & 0 deletions pkg/quickwit/response_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3206,6 +3206,21 @@ func TestParseLuceneQuery(t *testing.T) {
require.Len(t, highlights, 1)
require.Equal(t, "foo", highlights[0])
})

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 2630e2b

Please sign in to comment.