Skip to content

Commit 2dedfcb

Browse files
committed
Issue #18: handle key/value queries
1 parent b71b27a commit 2dedfcb

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

pkg/quickwit/response_parser.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,23 @@ func parseLuceneQuery(query string) []string {
9999
var keywords []string
100100

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

104-
for _, match := range matches {
105-
if match[0] == '"' && match[len(match)-1] == '"' {
106-
match = match[1 : len(match)-1]
105+
for _, termMatch := range termMatches {
106+
if termMatch[0] == '"' && termMatch[len(termMatches)-1] == '"' {
107+
termMatch = termMatch[1 : len(termMatch)-1]
107108
}
108109

109-
keywords = append(keywords, strings.ReplaceAll(match, "*", ""))
110+
keyValueMatches := keyValueRegex.FindStringSubmatch(termMatch)
111+
if len(keyValueMatches) <= 1 {
112+
keywords = append(keywords, strings.ReplaceAll(termMatch, "*", ""))
113+
continue
114+
}
115+
116+
for _, keyValueMatch := range keyValueMatches[1:] {
117+
keywords = append(keywords, strings.ReplaceAll(keyValueMatch, "*", ""))
118+
}
110119
}
111120

112121
return keywords

pkg/quickwit/response_parser_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3206,6 +3206,13 @@ func TestParseLuceneQuery(t *testing.T) {
32063206
require.Len(t, highlights, 1)
32073207
require.Equal(t, "foo", highlights[0])
32083208
})
3209+
3210+
t.Run("LeyValue query", func(t *testing.T) {
3211+
query := "foo:bar*"
3212+
highlights := parseLuceneQuery(query)
3213+
require.Len(t, highlights, 1)
3214+
require.Equal(t, "bar", highlights[0])
3215+
})
32093216
}
32103217

32113218
func TestFlatten(t *testing.T) {

0 commit comments

Comments
 (0)