Skip to content

Commit

Permalink
Allow to use encoding/json with some Search APIs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
olivere committed Oct 14, 2021
1 parent 87d344c commit 68349f0
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
16 changes: 15 additions & 1 deletion search_queries_match_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package elastic

import "encoding/json"

// MatchAllQuery is the most simple query, which matches all documents,
// giving them all a _score of 1.0.
//
Expand Down Expand Up @@ -34,7 +36,7 @@ func (q *MatchAllQuery) QueryName(name string) *MatchAllQuery {
}

// Source returns JSON for the match all query.
func (q MatchAllQuery) Source() (interface{}, error) {
func (q *MatchAllQuery) Source() (interface{}, error) {
// {
// "match_all" : { ... }
// }
Expand All @@ -49,3 +51,15 @@ func (q MatchAllQuery) Source() (interface{}, error) {
}
return source, nil
}

// MarshalJSON enables serializing the type as JSON.
func (q *MatchAllQuery) MarshalJSON() ([]byte, error) {
if q == nil {
return nilByte, nil
}
src, err := q.Source()
if err != nil {
return nil, err
}
return json.Marshal(src)
}
13 changes: 13 additions & 0 deletions search_queries_match_all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,16 @@ func TestMatchAllQueryWithQueryName(t *testing.T) {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}

func TestMatchAllMarshalJSON(t *testing.T) {
in := NewMatchAllQuery().Boost(3.14)
data, err := json.Marshal(in)
if err != nil {
t.Fatal(err)
}
got := string(data)
expected := `{"match_all":{"boost":3.14}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}
13 changes: 13 additions & 0 deletions search_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package elastic

import (
"encoding/json"
"fmt"
)

Expand Down Expand Up @@ -632,6 +633,18 @@ func (s *SearchSource) Source() (interface{}, error) {
return source, nil
}

// MarshalJSON enables serializing the type as JSON.
func (q *SearchSource) MarshalJSON() ([]byte, error) {
if q == nil {
return nilByte, nil
}
src, err := q.Source()
if err != nil {
return nil, err
}
return json.Marshal(src)
}

// -- IndexBoosts --

// IndexBoost specifies an index by some boost factor.
Expand Down
13 changes: 13 additions & 0 deletions search_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ func TestSearchSourceMatchAllQuery(t *testing.T) {
}
}

func TestSearchSourceMarshalJSON(t *testing.T) {
matchAllQ := NewMatchAllQuery()
builder := NewSearchSource().Query(matchAllQ)
data, err := builder.MarshalJSON()
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"query":{"match_all":{}}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}
func TestSearchSourceNoStoredFields(t *testing.T) {
matchAllQ := NewMatchAllQuery()
builder := NewSearchSource().Query(matchAllQ).NoStoredFields()
Expand Down

0 comments on commit 68349f0

Please sign in to comment.