Skip to content

Commit

Permalink
fix bug for querystringquery
Browse files Browse the repository at this point in the history
  • Loading branch information
CascadingRadium committed Dec 11, 2024
1 parent 31973e0 commit 201caf6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
30 changes: 23 additions & 7 deletions search/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,10 +430,11 @@ type FieldSet map[string]struct{}
// ExtractFields returns a set of fields referenced by the query.
// The returned set may be nil if the query does not explicitly reference any field
// and the DefaultSearchField is unset in the index mapping.
func ExtractFields(q Query, m mapping.IndexMapping, fs FieldSet) FieldSet {
if q == nil {
return fs
func ExtractFields(q Query, m mapping.IndexMapping, fs FieldSet) (FieldSet, error) {
if q == nil || m == nil {
return fs, nil
}
var err error
switch q := q.(type) {
case FieldableQuery:
f := q.Field()
Expand All @@ -446,18 +447,33 @@ func ExtractFields(q Query, m mapping.IndexMapping, fs FieldSet) FieldSet {
}
fs[f] = struct{}{}
}
case *QueryStringQuery:
var expandedQuery Query
expandedQuery, err = expandQuery(m, q)
if err == nil {
fs, err = ExtractFields(expandedQuery, m, fs)
}
case *BooleanQuery:
for _, subq := range []Query{q.Must, q.Should, q.MustNot} {
fs = ExtractFields(subq, m, fs)
fs, err = ExtractFields(subq, m, fs)
if err != nil {
break
}
}
case *ConjunctionQuery:
for _, subq := range q.Conjuncts {
fs = ExtractFields(subq, m, fs)
fs, err = ExtractFields(subq, m, fs)
if err != nil {
break
}
}
case *DisjunctionQuery:
for _, subq := range q.Disjuncts {
fs = ExtractFields(subq, m, fs)
fs, err = ExtractFields(subq, m, fs)
if err != nil {
break
}
}
}
return fs
return fs, err
}
23 changes: 22 additions & 1 deletion search/query/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,24 @@ func TestExtractFields(t *testing.T) {
}`,
expFields: []string{"date", "number", "date2", "number2", "date3"},
},
{
query: `{
"query" : "hardworking people"
}`,
expFields: []string{"_all"},
},
{
query: `{
"query" : "text:hardworking people"
}`,
expFields: []string{"text", "_all"},
},
{
query: `{
"query" : "text:\"hardworking people\""
}`,
expFields: []string{"text"},
},
}

m := mapping.NewIndexMapping()
Expand All @@ -1007,7 +1025,10 @@ func TestExtractFields(t *testing.T) {
if err != nil {
t.Fatal(err)
}
fields := ExtractFields(q, m, nil)
fields, err := ExtractFields(q, m, nil)
if err != nil {
t.Fatal(err)
}
var fieldsSlice []string
for k := range fields {
fieldsSlice = append(fieldsSlice, k)
Expand Down

0 comments on commit 201caf6

Please sign in to comment.