Skip to content

Commit

Permalink
Fix prefix query with case insensitive setting
Browse files Browse the repository at this point in the history
The `PrefixQuery` query wasn't serialized correctly if only name,
prefix, and case-sensitivity were set.

Close #1546
  • Loading branch information
olivere committed Dec 17, 2021
1 parent 4f45386 commit da7a99e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion search_queries_prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (q *PrefixQuery) Source() (interface{}, error) {
query := make(map[string]interface{})
source["prefix"] = query

if q.boost == nil && q.rewrite == "" && q.queryName == "" {
if q.boost == nil && q.rewrite == "" && q.queryName == "" && q.caseInsensitive == nil {
query[q.name] = q.prefix
} else {
subQuery := make(map[string]interface{})
Expand Down
17 changes: 17 additions & 0 deletions search_queries_prefix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ func TestPrefixQuery(t *testing.T) {
}
}

func TestPrefixQueryWithCaseInsensitive(t *testing.T) {
q := NewPrefixQuery("user", "ki").CaseInsensitive(true)
src, err := q.Source()
if err != nil {
t.Fatal(err)
}
data, err := json.Marshal(src)
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"prefix":{"user":{"case_insensitive":true,"value":"ki"}}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}

func TestPrefixQueryWithOptions(t *testing.T) {
q := NewPrefixQuery("user", "ki").CaseInsensitive(true)
q = q.QueryName("my_query_name")
Expand Down

0 comments on commit da7a99e

Please sign in to comment.