From cec324cc95fe1924ee3834acbbd7f8830864fd48 Mon Sep 17 00:00:00 2001 From: Oliver Eilhard Date: Sat, 6 May 2017 17:36:30 +0200 Subject: [PATCH] Add to nested query --- CONTRIBUTORS | 1 + search_queries_nested.go | 23 +++++++++++++++++------ search_queries_nested_test.go | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 39a71f90b..9ad11d31c 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -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) diff --git a/search_queries_nested.go b/search_queries_nested.go index 11e3bb1c6..a95cc2b80 100644 --- a/search_queries_nested.go +++ b/search_queries_nested.go @@ -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. @@ -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{}) @@ -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 { diff --git a/search_queries_nested_test.go b/search_queries_nested_test.go index af9740553..c7a5322a6 100644 --- a/search_queries_nested_test.go +++ b/search_queries_nested_test.go @@ -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) + } + } +}