Skip to content

Commit 322da1a

Browse files
committed
1 parent 2b61967 commit 322da1a

3 files changed

+103
-0
lines changed

search_queries_wrapper.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
// WrapperQuery accepts any other query as base64 encoded string.
8+
//
9+
// For details, see
10+
// https://www.elastic.co/guide/en/elasticsearch/reference/6.3/query-dsl-wrapper-query.html.
11+
type WrapperQuery struct {
12+
source string
13+
}
14+
15+
// NewWrapperQuery creates and initializes a new WrapperQuery.
16+
func NewWrapperQuery(source string) *WrapperQuery {
17+
return &WrapperQuery{source: source}
18+
}
19+
20+
// Source returns JSON for the query.
21+
func (q *WrapperQuery) Source() (interface{}, error) {
22+
// {"wrapper":{"query":"..."}}
23+
source := make(map[string]interface{})
24+
tq := make(map[string]interface{})
25+
source["wrapper"] = tq
26+
tq["query"] = q.source
27+
return source, nil
28+
}
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
"context"
9+
"encoding/base64"
10+
"encoding/json"
11+
"testing"
12+
)
13+
14+
func TestWrapperQueryIntegration(t *testing.T) {
15+
client := setupTestClientAndCreateIndexAndAddDocs(t) //, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
16+
17+
tq := NewTermQuery("user", "olivere")
18+
src, err := tq.Source()
19+
if err != nil {
20+
t.Fatal(err)
21+
}
22+
data, err := json.Marshal(src)
23+
if err != nil {
24+
t.Fatalf("marshaling to JSON failed: %v", err)
25+
}
26+
base64string := base64.StdEncoding.EncodeToString(data)
27+
28+
q := NewWrapperQuery(base64string)
29+
30+
// Match all should return all documents
31+
searchResult, err := client.Search().
32+
Index(testIndexName).
33+
Query(q).
34+
Pretty(true).
35+
Do(context.TODO())
36+
if err != nil {
37+
t.Fatal(err)
38+
}
39+
if searchResult.Hits == nil {
40+
t.Errorf("expected SearchResult.Hits != nil; got nil")
41+
}
42+
if got, want := searchResult.Hits.TotalHits, int64(2); got != want {
43+
t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", want, got)
44+
}
45+
if got, want := len(searchResult.Hits.Hits), 2; got != want {
46+
t.Errorf("expected len(SearchResult.Hits.Hits) = %d; got %d", want, got)
47+
}
48+
}

search_queries_wrapper_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 TestWrapperQuery(t *testing.T) {
13+
q := NewWrapperQuery("eyJ0ZXJtIiA6IHsgInVzZXIiIDogIktpbWNoeSIgfX0=")
14+
src, err := q.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 := `{"wrapper":{"query":"eyJ0ZXJtIiA6IHsgInVzZXIiIDogIktpbWNoeSIgfX0="}}`
24+
if got != expected {
25+
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
26+
}
27+
}

0 commit comments

Comments
 (0)