Skip to content

Commit da7a99e

Browse files
committed
Fix prefix query with case insensitive setting
The `PrefixQuery` query wasn't serialized correctly if only name, prefix, and case-sensitivity were set. Close #1546
1 parent 4f45386 commit da7a99e

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

search_queries_prefix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (q *PrefixQuery) Source() (interface{}, error) {
5252
query := make(map[string]interface{})
5353
source["prefix"] = query
5454

55-
if q.boost == nil && q.rewrite == "" && q.queryName == "" {
55+
if q.boost == nil && q.rewrite == "" && q.queryName == "" && q.caseInsensitive == nil {
5656
query[q.name] = q.prefix
5757
} else {
5858
subQuery := make(map[string]interface{})

search_queries_prefix_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@ func TestPrefixQuery(t *testing.T) {
2626
}
2727
}
2828

29+
func TestPrefixQueryWithCaseInsensitive(t *testing.T) {
30+
q := NewPrefixQuery("user", "ki").CaseInsensitive(true)
31+
src, err := q.Source()
32+
if err != nil {
33+
t.Fatal(err)
34+
}
35+
data, err := json.Marshal(src)
36+
if err != nil {
37+
t.Fatalf("marshaling to JSON failed: %v", err)
38+
}
39+
got := string(data)
40+
expected := `{"prefix":{"user":{"case_insensitive":true,"value":"ki"}}}`
41+
if got != expected {
42+
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
43+
}
44+
}
45+
2946
func TestPrefixQueryWithOptions(t *testing.T) {
3047
q := NewPrefixQuery("user", "ki").CaseInsensitive(true)
3148
q = q.QueryName("my_query_name")

0 commit comments

Comments
 (0)