From da7a99ee4f786f6ca211bffd7981f32c3cf8d23d Mon Sep 17 00:00:00 2001 From: Oliver Eilhard Date: Fri, 17 Dec 2021 10:08:44 +0100 Subject: [PATCH] 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 --- search_queries_prefix.go | 2 +- search_queries_prefix_test.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/search_queries_prefix.go b/search_queries_prefix.go index 5e9813e1..7c3f122a 100644 --- a/search_queries_prefix.go +++ b/search_queries_prefix.go @@ -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{}) diff --git a/search_queries_prefix_test.go b/search_queries_prefix_test.go index 2666fbe8..282a65d4 100644 --- a/search_queries_prefix_test.go +++ b/search_queries_prefix_test.go @@ -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")