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 2dedfcb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
19 changes: 14 additions & 5 deletions pkg/quickwit/response_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,23 @@ 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 {
keywords = append(keywords, strings.ReplaceAll(termMatch, "*", ""))
continue
}

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

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

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

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

0 comments on commit 2dedfcb

Please sign in to comment.