Skip to content

Commit 68349f0

Browse files
committed
Allow to use encoding/json with some Search APIs
This is a first trial of allowing to serialize the DSL objects with `encoding/json`. They could then be used together with the official client.
1 parent 87d344c commit 68349f0

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

search_queries_match_all.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
package elastic
66

7+
import "encoding/json"
8+
79
// MatchAllQuery is the most simple query, which matches all documents,
810
// giving them all a _score of 1.0.
911
//
@@ -34,7 +36,7 @@ func (q *MatchAllQuery) QueryName(name string) *MatchAllQuery {
3436
}
3537

3638
// Source returns JSON for the match all query.
37-
func (q MatchAllQuery) Source() (interface{}, error) {
39+
func (q *MatchAllQuery) Source() (interface{}, error) {
3840
// {
3941
// "match_all" : { ... }
4042
// }
@@ -49,3 +51,15 @@ func (q MatchAllQuery) Source() (interface{}, error) {
4951
}
5052
return source, nil
5153
}
54+
55+
// MarshalJSON enables serializing the type as JSON.
56+
func (q *MatchAllQuery) MarshalJSON() ([]byte, error) {
57+
if q == nil {
58+
return nilByte, nil
59+
}
60+
src, err := q.Source()
61+
if err != nil {
62+
return nil, err
63+
}
64+
return json.Marshal(src)
65+
}

search_queries_match_all_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,16 @@ func TestMatchAllQueryWithQueryName(t *testing.T) {
5959
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
6060
}
6161
}
62+
63+
func TestMatchAllMarshalJSON(t *testing.T) {
64+
in := NewMatchAllQuery().Boost(3.14)
65+
data, err := json.Marshal(in)
66+
if err != nil {
67+
t.Fatal(err)
68+
}
69+
got := string(data)
70+
expected := `{"match_all":{"boost":3.14}}`
71+
if got != expected {
72+
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
73+
}
74+
}

search_source.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package elastic
66

77
import (
8+
"encoding/json"
89
"fmt"
910
)
1011

@@ -632,6 +633,18 @@ func (s *SearchSource) Source() (interface{}, error) {
632633
return source, nil
633634
}
634635

636+
// MarshalJSON enables serializing the type as JSON.
637+
func (q *SearchSource) MarshalJSON() ([]byte, error) {
638+
if q == nil {
639+
return nilByte, nil
640+
}
641+
src, err := q.Source()
642+
if err != nil {
643+
return nil, err
644+
}
645+
return json.Marshal(src)
646+
}
647+
635648
// -- IndexBoosts --
636649

637650
// IndexBoost specifies an index by some boost factor.

search_source_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ func TestSearchSourceMatchAllQuery(t *testing.T) {
2727
}
2828
}
2929

30+
func TestSearchSourceMarshalJSON(t *testing.T) {
31+
matchAllQ := NewMatchAllQuery()
32+
builder := NewSearchSource().Query(matchAllQ)
33+
data, err := builder.MarshalJSON()
34+
if err != nil {
35+
t.Fatalf("marshaling to JSON failed: %v", err)
36+
}
37+
got := string(data)
38+
expected := `{"query":{"match_all":{}}}`
39+
if got != expected {
40+
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
41+
}
42+
}
3043
func TestSearchSourceNoStoredFields(t *testing.T) {
3144
matchAllQ := NewMatchAllQuery()
3245
builder := NewSearchSource().Query(matchAllQ).NoStoredFields()

0 commit comments

Comments
 (0)