Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add search_queries_span_or #1552

Open
wants to merge 3 commits into
base: release-branch.v7
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions go.mod
Expand Up @@ -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
Expand Down
74 changes: 74 additions & 0 deletions 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
}
31 changes: 31 additions & 0 deletions 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)
}
}