From cc8cbe10257cf795075f0451877aa1ceb1ee98c1 Mon Sep 17 00:00:00 2001 From: vincent1394 Date: Wed, 1 Dec 2021 15:34:32 +0800 Subject: [PATCH 1/3] Create search_queries_span_or.go add span_or support --- search_queries_span_or.go | 78 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 search_queries_span_or.go diff --git a/search_queries_span_or.go b/search_queries_span_or.go new file mode 100644 index 00000000..a1a5b74d --- /dev/null +++ b/search_queries_span_or.go @@ -0,0 +1,78 @@ +package elastic + +import "github.com/olivere/elastic/v7" + +// 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 which are near one another. One can specify slop, +// the maximum number of intervening unmatched positions, as well as whether +// matches are required to be in-order. The span near query maps to Lucene SpanOrQuery. +// +// See https://www.elastic.co/guide/en/elasticsearch/reference/7.7/query-dsl-span-near-query.html +// for details. +type SpanOrQuery struct { + clauses []elastic.Query + boost *float64 + queryName string +} + +// NewSpanOrQuery creates a new NewSpanOrQuery. +func NewSpanOrQuery(clauses ...elastic.Query) *SpanOrQuery { + return &SpanOrQuery{ + clauses: clauses, + } +} + +// Add clauses to use in the query. +func (q *SpanOrQuery) Add(clauses ...elastic.Query) *SpanOrQuery { + q.clauses = append(q.clauses, clauses...) + return q +} + +// Clauses to use in the query. +func (q *SpanOrQuery) Clauses(clauses ...elastic.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 +} From 8ef899cce3b374719c37ca5b2f56ff3efbdd6985 Mon Sep 17 00:00:00 2001 From: vincent1394 Date: Wed, 1 Dec 2021 15:37:15 +0800 Subject: [PATCH 2/3] Update search_queries_span_or.go --- search_queries_span_or.go | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/search_queries_span_or.go b/search_queries_span_or.go index a1a5b74d..9e4507ef 100644 --- a/search_queries_span_or.go +++ b/search_queries_span_or.go @@ -6,33 +6,31 @@ import "github.com/olivere/elastic/v7" // Use of this source code is governed by a MIT-license. // See http://olivere.mit-license.org/license.txt for details. -// SpanOrQuery matches spans which are near one another. One can specify slop, +// SpanOrQuery matches spans or. One can specify slop, // the maximum number of intervening unmatched positions, as well as whether -// matches are required to be in-order. The span near query maps to Lucene SpanOrQuery. -// -// See https://www.elastic.co/guide/en/elasticsearch/reference/7.7/query-dsl-span-near-query.html -// for details. +// The span or query maps to Lucene SpanOrQuery. + type SpanOrQuery struct { - clauses []elastic.Query + clauses []Query boost *float64 queryName string } // NewSpanOrQuery creates a new NewSpanOrQuery. -func NewSpanOrQuery(clauses ...elastic.Query) *SpanOrQuery { +func NewSpanOrQuery(clauses ...Query) *SpanOrQuery { return &SpanOrQuery{ clauses: clauses, } } // Add clauses to use in the query. -func (q *SpanOrQuery) Add(clauses ...elastic.Query) *SpanOrQuery { +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 ...elastic.Query) *SpanOrQuery { +func (q *SpanOrQuery) Clauses(clauses ...Query) *SpanOrQuery { q.clauses = clauses return q } From f9c02b835275086765e6006f13d88f3d3f159b29 Mon Sep 17 00:00:00 2001 From: luantao Date: Wed, 1 Dec 2021 16:46:48 +0800 Subject: [PATCH 3/3] add test --- go.mod | 3 +-- search_queries_span_or.go | 2 -- search_queries_span_or_test.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 search_queries_span_or_test.go 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 index 9e4507ef..33b85b7d 100644 --- a/search_queries_span_or.go +++ b/search_queries_span_or.go @@ -1,7 +1,5 @@ package elastic -import "github.com/olivere/elastic/v7" - // 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. 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) + } +}