Skip to content

Commit

Permalink
Add to nested query
Browse files Browse the repository at this point in the history
  • Loading branch information
olivere committed May 6, 2017
1 parent 6f76abd commit cec324c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Braden Bassingthwaite [@bbassingthwaite-va](https://github.com/bbassingthwaite-v
Brady Love [@bradylove](https://github.com/bradylove)
Bryan Conklin [@bmconklin](https://github.com/bmconklin)
Bruce Zhou [@brucez-isell](https://github.com/brucez-isell)
cforbes [@cforbes](https://github.com/cforbes)
Chris M [@tebriel](https://github.com/tebriel)
Christophe Courtaut [@kri5](https://github.com/kri5)
Conrad Pankoff [@deoxxa](https://github.com/deoxxa)
Expand Down
23 changes: 17 additions & 6 deletions search_queries_nested.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ package elastic
// For more details, see
// https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-nested-query.html
type NestedQuery struct {
query Query
path string
scoreMode string
boost *float64
queryName string
innerHit *InnerHit
query Query
path string
scoreMode string
boost *float64
queryName string
innerHit *InnerHit
ignoreUnmapped *bool
}

// NewNestedQuery creates and initializes a new NestedQuery.
Expand Down Expand Up @@ -51,6 +52,13 @@ func (q *NestedQuery) InnerHit(innerHit *InnerHit) *NestedQuery {
return q
}

// IgnoreUnmapped sets the ignore_unmapped option for the filter that ignores
// unmapped nested fields
func (q *NestedQuery) IgnoreUnmapped(value bool) *NestedQuery {
q.ignoreUnmapped = &value
return q
}

// Source returns JSON for the query.
func (q *NestedQuery) Source() (interface{}, error) {
query := make(map[string]interface{})
Expand All @@ -74,6 +82,9 @@ func (q *NestedQuery) Source() (interface{}, error) {
if q.queryName != "" {
nq["_name"] = q.queryName
}
if q.ignoreUnmapped != nil {
nq["ignore_unmapped"] = *q.ignoreUnmapped
}
if q.innerHit != nil {
src, err := q.innerHit.Source()
if err != nil {
Expand Down
34 changes: 34 additions & 0 deletions search_queries_nested_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,37 @@ func TestNestedQueryWithInnerHit(t *testing.T) {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}

func TestNestedQueryWithIgnoreUnmapped(t *testing.T) {
var tests = []struct {
query *BoolQuery
expected string
}{
{
NewBoolQuery().Must(NewNestedQuery("path", NewTermQuery("test", "test"))),
`{"bool":{"must":{"nested":{"path":"path","query":{"term":{"test":"test"}}}}}}`,
},
{
NewBoolQuery().Must(NewNestedQuery("path", NewTermQuery("test", "test")).IgnoreUnmapped(true)),
`{"bool":{"must":{"nested":{"ignore_unmapped":true,"path":"path","query":{"term":{"test":"test"}}}}}}`,
},
{
NewBoolQuery().Must(NewNestedQuery("path", NewTermQuery("test", "test")).IgnoreUnmapped(false)),
`{"bool":{"must":{"nested":{"ignore_unmapped":false,"path":"path","query":{"term":{"test":"test"}}}}}}`,
},
}
for _, test := range tests {
src, err := test.query.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)
if got != test.expected {
t.Errorf("expected\n%s\n,got:\n%s", test.expected, got)
}
}
}

0 comments on commit cec324c

Please sign in to comment.