Skip to content

Commit f698dfe

Browse files
committed
Add TermsLookup to TermsQuery
The TermsQuery was missing the TermsLookup mechanism, as described here: https://www.elastic.co/guide/en/elasticsearch/reference/5.3/query-dsl-terms-query.html#query-dsl-terms-lookup Close #500
1 parent 7169c87 commit f698dfe

4 files changed

+147
-12
lines changed

search_queries_terms.go

+28-12
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,30 @@ package elastic
1010
// For more details, see
1111
// https://www.elastic.co/guide/en/elasticsearch/reference/5.2/query-dsl-terms-query.html
1212
type TermsQuery struct {
13-
name string
14-
values []interface{}
15-
queryName string
16-
boost *float64
13+
name string
14+
values []interface{}
15+
termsLookup *TermsLookup
16+
queryName string
17+
boost *float64
1718
}
1819

1920
// NewTermsQuery creates and initializes a new TermsQuery.
2021
func NewTermsQuery(name string, values ...interface{}) *TermsQuery {
2122
q := &TermsQuery{
22-
name: name,
23-
values: make([]interface{}, 0),
23+
name: name,
2424
}
2525
if len(values) > 0 {
2626
q.values = append(q.values, values...)
2727
}
2828
return q
2929
}
3030

31+
// TermsLookup adds terms lookup details to the query.
32+
func (q *TermsQuery) TermsLookup(lookup *TermsLookup) *TermsQuery {
33+
q.termsLookup = lookup
34+
return q
35+
}
36+
3137
// Boost sets the boost for this query.
3238
func (q *TermsQuery) Boost(boost float64) *TermsQuery {
3339
q.boost = &boost
@@ -47,12 +53,22 @@ func (q *TermsQuery) Source() (interface{}, error) {
4753
source := make(map[string]interface{})
4854
params := make(map[string]interface{})
4955
source["terms"] = params
50-
params[q.name] = q.values
51-
if q.boost != nil {
52-
params["boost"] = *q.boost
53-
}
54-
if q.queryName != "" {
55-
params["_name"] = q.queryName
56+
57+
if q.termsLookup != nil {
58+
src, err := q.termsLookup.Source()
59+
if err != nil {
60+
return nil, err
61+
}
62+
params[q.name] = src
63+
} else {
64+
params[q.name] = q.values
65+
if q.boost != nil {
66+
params["boost"] = *q.boost
67+
}
68+
if q.queryName != "" {
69+
params["_name"] = q.queryName
70+
}
5671
}
72+
5773
return source, nil
5874
}

search_queries_terms_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@ func TestTermsQuery(t *testing.T) {
2626
}
2727
}
2828

29+
func TestTermsQueryWithTermsLookup(t *testing.T) {
30+
q := NewTermsQuery("user").
31+
TermsLookup(NewTermsLookup().Index("users").Type("user").Id("2").Path("followers"))
32+
src, err := q.Source()
33+
if err != nil {
34+
t.Fatal(err)
35+
}
36+
data, err := json.Marshal(src)
37+
if err != nil {
38+
t.Fatalf("marshaling to JSON failed: %v", err)
39+
}
40+
got := string(data)
41+
expected := `{"terms":{"user":{"id":"2","index":"users","path":"followers","type":"user"}}}`
42+
if got != expected {
43+
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
44+
}
45+
}
46+
2947
func TestTermQuerysWithOptions(t *testing.T) {
3048
q := NewTermsQuery("user", "ki", "ko")
3149
q = q.Boost(2.79)

search_terms_lookup.go

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2012-present Oliver Eilhard. All rights reserved.
2+
// Use of this source code is governed by a MIT-license.
3+
// See http://olivere.mit-license.org/license.txt for details.
4+
5+
package elastic
6+
7+
// TermsLookup encapsulates the parameters needed to fetch terms.
8+
//
9+
// For more details, see
10+
// https://www.elastic.co/guide/en/elasticsearch/reference/5.3/query-dsl-terms-query.html#query-dsl-terms-lookup.
11+
type TermsLookup struct {
12+
index string
13+
typ string
14+
id string
15+
path string
16+
routing string
17+
}
18+
19+
// NewTermsLookup creates and initializes a new TermsLookup.
20+
func NewTermsLookup() *TermsLookup {
21+
t := &TermsLookup{}
22+
return t
23+
}
24+
25+
// Index name.
26+
func (t *TermsLookup) Index(index string) *TermsLookup {
27+
t.index = index
28+
return t
29+
}
30+
31+
// Type name.
32+
func (t *TermsLookup) Type(typ string) *TermsLookup {
33+
t.typ = typ
34+
return t
35+
}
36+
37+
// Id to look up.
38+
func (t *TermsLookup) Id(id string) *TermsLookup {
39+
t.id = id
40+
return t
41+
}
42+
43+
// Path to use for lookup.
44+
func (t *TermsLookup) Path(path string) *TermsLookup {
45+
t.path = path
46+
return t
47+
}
48+
49+
// Routing value.
50+
func (t *TermsLookup) Routing(routing string) *TermsLookup {
51+
t.routing = routing
52+
return t
53+
}
54+
55+
// Source creates the JSON source of the builder.
56+
func (t *TermsLookup) Source() (interface{}, error) {
57+
src := make(map[string]interface{})
58+
if t.index != "" {
59+
src["index"] = t.index
60+
}
61+
if t.typ != "" {
62+
src["type"] = t.typ
63+
}
64+
if t.id != "" {
65+
src["id"] = t.id
66+
}
67+
if t.path != "" {
68+
src["path"] = t.path
69+
}
70+
if t.routing != "" {
71+
src["routing"] = t.routing
72+
}
73+
return src, nil
74+
}

search_terms_lookup_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2012-present Oliver Eilhard. All rights reserved.
2+
// Use of this source code is governed by a MIT-license.
3+
// See http://olivere.mit-license.org/license.txt for details.
4+
5+
package elastic
6+
7+
import (
8+
"encoding/json"
9+
"testing"
10+
)
11+
12+
func TestTermsLookup(t *testing.T) {
13+
tl := NewTermsLookup().Index("users").Type("user").Id("2").Path("followers")
14+
src, err := tl.Source()
15+
if err != nil {
16+
t.Fatal(err)
17+
}
18+
data, err := json.Marshal(src)
19+
if err != nil {
20+
t.Fatalf("marshaling to JSON failed: %v", err)
21+
}
22+
got := string(data)
23+
expected := `{"id":"2","index":"users","path":"followers","type":"user"}`
24+
if got != expected {
25+
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
26+
}
27+
}

0 commit comments

Comments
 (0)