-
Notifications
You must be signed in to change notification settings - Fork 608
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix JSONReader integration with client
This patch updates the esapi.newRequest() function to handle body passed as esutil.JSONReader properly. It makes the esutil.JSONReader exported, and renames the constructor function to esutil.NewJSONReader(). It also adds proper integration tests for esutil.JSONReader.
- Loading branch information
Showing
6 changed files
with
146 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/elastic/go-elasticsearch/v8" | ||
"github.com/elastic/go-elasticsearch/v8/esapi" | ||
"github.com/elastic/go-elasticsearch/v8/esutil" | ||
) | ||
|
||
func init() { | ||
log.SetFlags(0) | ||
} | ||
|
||
func main() { | ||
var ( | ||
res *esapi.Response | ||
err error | ||
) | ||
|
||
es, err := elasticsearch.NewDefaultClient() | ||
if err != nil { | ||
log.Fatalf("Error creating the client: %s", err) | ||
} | ||
|
||
doc := struct { | ||
Title string `json:"title"` | ||
}{Title: "Test"} | ||
|
||
res, err = es.Index("test", esutil.NewJSONReader(&doc), es.Index.WithRefresh("true")) | ||
if err != nil { | ||
log.Fatalf("Error getting response: %s", err) | ||
} | ||
|
||
log.Println(res) | ||
|
||
query := map[string]interface{}{ | ||
"query": map[string]interface{}{ | ||
"match": map[string]interface{}{ | ||
"title": "test", | ||
}, | ||
}, | ||
} | ||
|
||
res, err = es.Search( | ||
es.Search.WithIndex("test"), | ||
es.Search.WithBody(esutil.NewJSONReader(&query)), | ||
es.Search.WithPretty(), | ||
) | ||
if err != nil { | ||
log.Fatalf("Error getting response: %s", err) | ||
} | ||
|
||
log.Println(res) | ||
} |
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
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,68 @@ | ||
// +build integration | ||
|
||
package esutil_test | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/elastic/go-elasticsearch/v8" | ||
"github.com/elastic/go-elasticsearch/v8/esapi" | ||
"github.com/elastic/go-elasticsearch/v8/esutil" | ||
) | ||
|
||
func TestJSONReaderIntegration(t *testing.T) { | ||
t.Run("Index and search", func(t *testing.T) { | ||
var ( | ||
res *esapi.Response | ||
err error | ||
) | ||
|
||
es, err := elasticsearch.NewDefaultClient() | ||
if err != nil { | ||
t.Fatalf("Error creating the client: %s\n", err) | ||
} | ||
|
||
es.Indices.Delete([]string{"test"}) | ||
|
||
doc := struct { | ||
Title string `json:"title"` | ||
}{Title: "Foo Bar"} | ||
|
||
res, err = es.Index("test", esutil.NewJSONReader(&doc), es.Index.WithRefresh("true")) | ||
if err != nil { | ||
t.Fatalf("Error getting response: %s", err) | ||
} | ||
defer res.Body.Close() | ||
|
||
if res.IsError() { | ||
t.Fatalf("Error response: %s", res.String()) | ||
} | ||
|
||
query := map[string]interface{}{ | ||
"query": map[string]interface{}{ | ||
"match": map[string]interface{}{ | ||
"title": "foo", | ||
}, | ||
}, | ||
} | ||
|
||
res, err = es.Search( | ||
es.Search.WithIndex("test"), | ||
es.Search.WithBody(esutil.NewJSONReader(&query)), | ||
es.Search.WithPretty(), | ||
) | ||
if err != nil { | ||
t.Fatalf("Error getting response: %s", err) | ||
} | ||
defer res.Body.Close() | ||
|
||
if res.IsError() { | ||
t.Errorf("Error response: %s", res) | ||
} | ||
|
||
if !strings.Contains(res.String(), "Foo Bar") { | ||
t.Errorf("Unexpected response: %s", res) | ||
} | ||
}) | ||
} |
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