Skip to content

Commit

Permalink
Fix IntervalQueryPrefix
Browse files Browse the repository at this point in the history
This commit fixes the `IntervalQueryPrefix` as described in #1566.

Close #1566
  • Loading branch information
olivere committed Jan 7, 2022
1 parent 67d8121 commit 15cfb75
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
56 changes: 56 additions & 0 deletions search_queries_interval_integration_test.go
Expand Up @@ -14,6 +14,15 @@ func TestIntervalQuery_Integration(t *testing.T) {
// client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
client := setupTestClientAndCreateIndexAndAddDocs(t)

t.Run("Match", func(t *testing.T) {
testIntervalQueryMatch(client, t)
})
t.Run("Prefix", func(t *testing.T) {
testIntervalQueryPrefix(client, t)
})
}

func testIntervalQueryMatch(client *Client, t *testing.T) {
q := NewIntervalQuery(
"message",
NewIntervalQueryRuleAllOf(
Expand Down Expand Up @@ -59,3 +68,50 @@ func TestIntervalQuery_Integration(t *testing.T) {
}
}
}

func testIntervalQueryPrefix(client *Client, t *testing.T) {
q := NewIntervalQuery(
"message",
NewIntervalQueryRuleAllOf(
NewIntervalQueryRuleAnyOf(
NewIntervalQueryRuleMatch("Golang").Ordered(true),
NewIntervalQueryRuleMatch("Cycling").MaxGaps(0).Filter(
NewIntervalQueryFilter().NotContaining(
NewIntervalQueryRulePrefix("Hockey"),
),
),
),
).Ordered(true),
)

// Match all should return all documents
searchResult, err := client.Search().
Index(testIndexName).
Query(q).
Size(10).
Pretty(true).
Do(context.TODO())
if err != nil {
t.Fatal(err)
}
if searchResult.Hits == nil {
t.Errorf("expected SearchResult.Hits != nil; got nil")
}
if got, want := searchResult.TotalHits(), int64(2); got != want {
t.Errorf("expected SearchResult.TotalHits() = %d; got %d", want, got)
}
if got, want := len(searchResult.Hits.Hits), 2; got != want {
t.Errorf("expected len(SearchResult.Hits.Hits) = %d; got %d", want, got)
}

for _, hit := range searchResult.Hits.Hits {
if hit.Index != testIndexName {
t.Errorf("expected SearchResult.Hits.Hit.Index = %q; got %q", testIndexName, hit.Index)
}
item := make(map[string]interface{})
err := json.Unmarshal(hit.Source, &item)
if err != nil {
t.Fatal(err)
}
}
}
2 changes: 1 addition & 1 deletion search_queries_interval_rules_prefix.go
Expand Up @@ -37,7 +37,7 @@ func (r *IntervalQueryRulePrefix) UseField(useField string) *IntervalQueryRulePr
func (r *IntervalQueryRulePrefix) Source() (interface{}, error) {
source := make(map[string]interface{})

source["query"] = r.prefix
source["prefix"] = r.prefix

if r.analyzer != "" {
source["analyzer"] = r.analyzer
Expand Down

0 comments on commit 15cfb75

Please sign in to comment.