diff --git a/go.mod b/go.mod index c13e8061..7abda555 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,8 @@ module github.com/olivere/elastic/v7 go 1.16 require ( - github.com/aws/aws-sdk-go v1.40.43 + github.com/aws/aws-sdk-go v1.42.16 github.com/fortytw2/leaktest v1.3.0 - github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/google/go-cmp v0.5.6 github.com/mailru/easyjson v0.7.7 github.com/opentracing/opentracing-go v1.2.0 diff --git a/search_queries_span_or.go b/search_queries_span_or.go new file mode 100644 index 00000000..33b85b7d --- /dev/null +++ b/search_queries_span_or.go @@ -0,0 +1,74 @@ +package elastic + +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +// SpanOrQuery matches spans or. One can specify slop, +// the maximum number of intervening unmatched positions, as well as whether +// The span or query maps to Lucene SpanOrQuery. + +type SpanOrQuery struct { + clauses []Query + boost *float64 + queryName string +} + +// NewSpanOrQuery creates a new NewSpanOrQuery. +func NewSpanOrQuery(clauses ...Query) *SpanOrQuery { + return &SpanOrQuery{ + clauses: clauses, + } +} + +// Add clauses to use in the query. +func (q *SpanOrQuery) Add(clauses ...Query) *SpanOrQuery { + q.clauses = append(q.clauses, clauses...) + return q +} + +// Clauses to use in the query. +func (q *SpanOrQuery) Clauses(clauses ...Query) *SpanOrQuery { + q.clauses = clauses + return q +} + +// Boost sets the boost for this query. +func (q *SpanOrQuery) Boost(boost float64) *SpanOrQuery { + q.boost = &boost + return q +} + +// QueryName sets the query name for the filter that can be used when +// searching for matched_filters per hit. +func (q *SpanOrQuery) QueryName(queryName string) *SpanOrQuery { + q.queryName = queryName + return q +} + +// Source returns the JSON body. +func (q *SpanOrQuery) Source() (interface{}, error) { + m := make(map[string]interface{}) + c := make(map[string]interface{}) + + if len(q.clauses) > 0 { + var clauses []interface{} + for _, clause := range q.clauses { + src, err := clause.Source() + if err != nil { + return nil, err + } + clauses = append(clauses, src) + } + c["clauses"] = clauses + } + + if v := q.boost; v != nil { + c["boost"] = *v + } + if v := q.queryName; v != "" { + c["query_name"] = v + } + m["span_or"] = c + return m, nil +} diff --git a/search_queries_span_or_test.go b/search_queries_span_or_test.go new file mode 100644 index 00000000..f285dea1 --- /dev/null +++ b/search_queries_span_or_test.go @@ -0,0 +1,31 @@ +// Copyright 2012-present Oliver Eilhard. All rights reserved. +// Use of this source code is governed by a MIT-license. +// See http://olivere.mit-license.org/license.txt for details. + +package elastic + +import ( + "encoding/json" + "testing" +) + +func TestSpanOrQuery(t *testing.T) { + q := NewSpanOrQuery( + NewSpanTermQuery("field", "value1"), + NewSpanTermQuery("field", "value2"), + NewSpanTermQuery("field", "value3"), + ) + 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 := `{"span_or":{"clauses":[{"span_term":{"field":{"value":"value1"}}},{"span_term":{"field":{"value":"value2"}}},{"span_term":{"field":{"value":"value3"}}}]}}` + if got != expected { + t.Errorf("expected\n%s\n,got:\n%s", expected, got) + } +}