Skip to content

Commit 22424da

Browse files
committed
Issue #18: improve with operators
1 parent 2dedfcb commit 22424da

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

pkg/quickwit/response_parser.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,17 @@ 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

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

110121
keyValueMatches := keyValueRegex.FindStringSubmatch(termMatch)
111122
if len(keyValueMatches) <= 1 {
112-
keywords = append(keywords, strings.ReplaceAll(termMatch, "*", ""))
123+
value := strings.ReplaceAll(termMatch, "*", "")
124+
if isLuceneOperator(value) {
125+
continue
126+
}
127+
keywords = append(keywords, value)
113128
continue
114129
}
115130

116131
for _, keyValueMatch := range keyValueMatches[1:] {
117-
keywords = append(keywords, strings.ReplaceAll(keyValueMatch, "*", ""))
132+
value := strings.ReplaceAll(keyValueMatch, "*", "")
133+
if isLuceneOperator(value) {
134+
continue
135+
}
136+
keywords = append(keywords, value)
118137
}
119138
}
120139

pkg/quickwit/response_parser_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3207,12 +3207,20 @@ func TestParseLuceneQuery(t *testing.T) {
32073207
require.Equal(t, "foo", highlights[0])
32083208
})
32093209

3210-
t.Run("LeyValue query", func(t *testing.T) {
3210+
t.Run("KeyValue query", func(t *testing.T) {
32113211
query := "foo:bar*"
32123212
highlights := parseLuceneQuery(query)
32133213
require.Len(t, highlights, 1)
32143214
require.Equal(t, "bar", highlights[0])
32153215
})
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+
})
32163224
}
32173225

32183226
func TestFlatten(t *testing.T) {

0 commit comments

Comments
 (0)