-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
4 changed files
with
147 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// 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 | ||
|
||
// TermsLookup encapsulates the parameters needed to fetch terms. | ||
// | ||
// For more details, see | ||
// https://www.elastic.co/guide/en/elasticsearch/reference/5.3/query-dsl-terms-query.html#query-dsl-terms-lookup. | ||
type TermsLookup struct { | ||
index string | ||
typ string | ||
id string | ||
path string | ||
routing string | ||
} | ||
|
||
// NewTermsLookup creates and initializes a new TermsLookup. | ||
func NewTermsLookup() *TermsLookup { | ||
t := &TermsLookup{} | ||
return t | ||
} | ||
|
||
// Index name. | ||
func (t *TermsLookup) Index(index string) *TermsLookup { | ||
t.index = index | ||
return t | ||
} | ||
|
||
// Type name. | ||
func (t *TermsLookup) Type(typ string) *TermsLookup { | ||
t.typ = typ | ||
return t | ||
} | ||
|
||
// Id to look up. | ||
func (t *TermsLookup) Id(id string) *TermsLookup { | ||
t.id = id | ||
return t | ||
} | ||
|
||
// Path to use for lookup. | ||
func (t *TermsLookup) Path(path string) *TermsLookup { | ||
t.path = path | ||
return t | ||
} | ||
|
||
// Routing value. | ||
func (t *TermsLookup) Routing(routing string) *TermsLookup { | ||
t.routing = routing | ||
return t | ||
} | ||
|
||
// Source creates the JSON source of the builder. | ||
func (t *TermsLookup) Source() (interface{}, error) { | ||
src := make(map[string]interface{}) | ||
if t.index != "" { | ||
src["index"] = t.index | ||
} | ||
if t.typ != "" { | ||
src["type"] = t.typ | ||
} | ||
if t.id != "" { | ||
src["id"] = t.id | ||
} | ||
if t.path != "" { | ||
src["path"] = t.path | ||
} | ||
if t.routing != "" { | ||
src["routing"] = t.routing | ||
} | ||
return src, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// 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 TestTermsLookup(t *testing.T) { | ||
tl := NewTermsLookup().Index("users").Type("user").Id("2").Path("followers") | ||
src, err := tl.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 := `{"id":"2","index":"users","path":"followers","type":"user"}` | ||
if got != expected { | ||
t.Errorf("expected\n%s\n,got:\n%s", expected, got) | ||
} | ||
} |