Skip to content

Commit 2630e2b

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

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

pkg/quickwit/response_parser.go

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,46 @@ func parseResponse(responses []*es.SearchResponse, targets []*Query, configuredF
9595
return &result, nil
9696
}
9797

98+
func isLuceneOperator(value string) bool {
99+
operators := []string{"or", "and"}
100+
for _, op := range operators {
101+
if strings.ToLower(value) == op {
102+
return true
103+
}
104+
}
105+
106+
return false
107+
}
108+
98109
func parseLuceneQuery(query string) []string {
99110
var keywords []string
100111

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

104-
for _, match := range matches {
105-
if match[0] == '"' && match[len(match)-1] == '"' {
106-
match = match[1 : len(match)-1]
116+
for _, termMatch := range termMatches {
117+
if termMatch[0] == '"' && termMatch[len(termMatches)-1] == '"' {
118+
termMatch = termMatch[1 : len(termMatch)-1]
107119
}
108120

109-
keywords = append(keywords, strings.ReplaceAll(match, "*", ""))
121+
keyValueMatches := keyValueRegex.FindStringSubmatch(termMatch)
122+
if len(keyValueMatches) <= 1 {
123+
value := strings.ReplaceAll(termMatch, "*", "")
124+
if isLuceneOperator(value) {
125+
continue
126+
}
127+
keywords = append(keywords, value)
128+
continue
129+
}
130+
131+
for _, keyValueMatch := range keyValueMatches[1:] {
132+
value := strings.ReplaceAll(keyValueMatch, "*", "")
133+
if isLuceneOperator(value) {
134+
continue
135+
}
136+
keywords = append(keywords, value)
137+
}
110138
}
111139

112140
return keywords

pkg/quickwit/response_parser_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3206,6 +3206,21 @@ func TestParseLuceneQuery(t *testing.T) {
32063206
require.Len(t, highlights, 1)
32073207
require.Equal(t, "foo", highlights[0])
32083208
})
3209+
3210+
t.Run("KeyValue 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+
})
3216+
3217+
t.Run("MultiKeyValue query", func(t *testing.T) {
3218+
query := "foo:bar* AND foo2:bar2"
3219+
highlights := parseLuceneQuery(query)
3220+
require.Len(t, highlights, 2)
3221+
require.Equal(t, "bar", highlights[0])
3222+
require.Equal(t, "bar2", highlights[1])
3223+
})
32093224
}
32103225

32113226
func TestFlatten(t *testing.T) {

0 commit comments

Comments
 (0)