Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[typedclient] Add responses #618

Merged
merged 7 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# 8.7.0

## Typed API

* Inclusion of responses structures.

**Changes**

* `Do` method on endpoints now return a typed response, one per endpoint.
* `Perform` method added on endpoints, returns `http.Response` as did `Do`.
* Elasticsearch exceptions are now handled as `types.ElasticsearchError` with `.As` and `.Is` methods.
* `.Raw` now takes a reader as input.
* User defined values such as `_source` in `Hits` are now `json.RawMessage` to highlight they later deserializable nature.

# 8.6.0

## API
Expand Down
132 changes: 62 additions & 70 deletions elasticsearch_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ import (
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"github.com/elastic/go-elasticsearch/v8/typedapi/core/search"
"github.com/elastic/go-elasticsearch/v8/typedapi/indices/create"
"github.com/elastic/go-elasticsearch/v8/typedapi/some"
"github.com/elastic/go-elasticsearch/v8/typedapi/types"
"github.com/elastic/go-elasticsearch/v8/typedapi/types/enums/refresh"
"github.com/elastic/go-elasticsearch/v8/typedapi/types/enums/result"
"github.com/elastic/go-elasticsearch/v8/typedapi/types/enums/sortorder"
"log"
"net"
Expand Down Expand Up @@ -306,37 +308,24 @@ func TestTypedClient(t *testing.T) {
if err != nil {
t.Fatalf("error reading Info request: %s", err)
}
defer res.Body.Close()

var d map[string]interface{}
err = json.NewDecoder(res.Body).Decode(&d)
if err != nil {
log.Fatalf("Error parsing the response: %s\n", err)
}

if d["tagline"] != "You Know, for Search" {
t.Errorf("invalid tagline, got: %s", d["tagline"])
if res.Tagline != "You Know, for Search" {
t.Errorf("invalid tagline, got: %s", res.Tagline)
}
})

t.Run("Index & Search", func(t *testing.T) {
es, err := elasticsearch.NewTypedClient(elasticsearch.Config{})

u, _ := url.Parse("http://localhost:9200")
tp, _ := elastictransport.New(
elastictransport.Config{
URLs: []*url.URL{u},
Logger: &elastictransport.ColorLogger{os.Stdout, true, true},
},
)
es.Transport = tp
es, err := elasticsearch.NewTypedClient(elasticsearch.Config{
Addresses: []string{"http://localhost:9200"},
Logger: &elastictransport.ColorLogger{os.Stdout, true, true},
})
if err != nil {
t.Fatalf("error creating the client: %s", err)
}

// If the index doesn't exist we create it with a mapping.
indexName := "test-index"
if ok, err := es.Indices.Exists(indexName).IsSuccess(nil); !ok && err == nil {
if exists, err := es.Indices.Exists(indexName).IsSuccess(context.Background()); !exists && err == nil {
res, err := es.Indices.Create(indexName).
Request(&create.Request{
Mappings: &types.TypeMapping{
Expand All @@ -346,11 +335,15 @@ func TestTypedClient(t *testing.T) {
},
},
}).
Do(nil)
Do(context.Background())
if err != nil {
t.Fatalf("error creating index test-index: %s", err)
}
defer res.Body.Close()

if !res.Acknowledged && res.Index != indexName {
t.Fatalf("unexpected error during index creation, got : %#v", res)
}

} else if err != nil {
t.Error(err)
}
Expand All @@ -363,11 +356,14 @@ func TestTypedClient(t *testing.T) {
}
}()

for _, document := range []struct {
type Document struct {
Id int `json:"id"`
Name string `json:"name"`
Price int `json:"price"`
}{
}

// Indexing synchronously with refresh.Waitfor, one document at a time
for _, document := range []Document{
{
Id: 1,
Name: "Foo",
Expand All @@ -384,21 +380,36 @@ func TestTypedClient(t *testing.T) {
Price: 4,
},
} {
indexResponse, err := es.Index(indexName).
indexed, err := es.Index(indexName).
Request(document).
Id(strconv.Itoa(document.Id)).
Refresh(refresh.Waitfor).
Do(context.Background())
if err != nil {
t.Fatalf("error indexing document: %s", err)
}
defer indexResponse.Body.Close()
if indexed.Result != result.Created {
t.Fatalf("unexpected result during indexation of document: %v, response: %v", document, indexed)
}
}

if ok, err := es.Get(indexName, "1").IsSuccess(nil); !ok {
// Check for document existence in index
if ok, err := es.Get(indexName, "1").IsSuccess(context.Background()); !ok {
t.Fatalf("could not retrieve document: %s", err)
}

// Try to retrieve a faulty index name
if ok, _ := es.Get("non-existent-index", "9999").IsSuccess(context.Background()); ok {
t.Fatalf("index shouldn't exist")
}

// Same faulty index name with error handling
_, err = es.Get("non-existent-index", "9999").Do(context.Background())
if !errors.As(err, &types.ElasticsearchError{}) && !errors.Is(err, &types.ElasticsearchError{Status: 404}) {
t.Fatalf("expected ElasticsearchError, got: %v", err)
}

// Simple search matching name
res, err := es.Search().
Index(indexName).
Request(&search.Request{
Expand All @@ -412,48 +423,26 @@ func TestTypedClient(t *testing.T) {
if err != nil {
t.Fatalf("error runnning search query: %s", err)
}
defer res.Body.Close()

type SearchResult struct {
Hits struct {
Total struct {
Value int
Relation string
} `json:"total"`
Hits []struct {
Index string `json:"_index"`
Source struct {
Id int
Name string
} `json:"_source"`
} `json:"hits"`
} `json:"hits"`
}

sr := SearchResult{}

err = json.NewDecoder(res.Body).Decode(&sr)
if err != nil {
t.Fatal(err)
}

if sr.Hits.Total.Value != 1 && sr.Hits.Hits[0].Source.Name != "Foo" {
if res.Hits.Total.Value == 1 {
doc := Document{}
err = json.Unmarshal(res.Hits.Hits[0].Source_, &doc)
if err != nil {
t.Fatalf("cannot unmarshal document: %s", err)
}
if doc.Name != "Foo" {
t.Fatalf("unexpected search result")
}
} else {
t.Fatalf("unexpected search result")
}

type PriceAggregation struct {
Aggregations map[string]struct{ Value float64 } `json:"aggregations"`
}

pa := PriceAggregation{}

size := 0

totalPricesAgg, err := es.Search().
// Aggregate prices with a SumAggregation
searchResponse, err := es.Search().
Index(indexName).
Request(
&search.Request{
Size: &size,
Size: some.Int(0),
Aggregations: map[string]types.Aggregations{
"total_prices": {
Sum: &types.SumAggregation{
Expand All @@ -466,15 +455,18 @@ func TestTypedClient(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer totalPricesAgg.Body.Close()

err = json.NewDecoder(totalPricesAgg.Body).Decode(&pa)
if err != nil {
t.Fatal(err)
}

if pa.Aggregations["total_prices"].Value != 26. {
t.Fatalf("error in aggregation, should be 26, got: %f", pa.Aggregations["total_prices"].Value)
for name, agg := range searchResponse.Aggregations {
if name == "total_prices" {
switch aggregation := agg.(type) {
case *types.SumAggregate:
if aggregation.Value != 26. {
t.Fatalf("error in aggregation, should be 26, got: %f", aggregation.Value)
}
default:
fmt.Printf("unexpected aggregation: %#v\n", agg)
}
}
}
})

Expand Down
13 changes: 11 additions & 2 deletions elasticsearch_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,13 @@ func TestNewTypedClient(t *testing.T) {
Header: http.Header{"X-Elastic-Product": []string{"Elasticsearch"}},
StatusCode: http.StatusOK,
Status: "OK",
Body: ioutil.NopCloser(strings.NewReader("")),
Body: ioutil.NopCloser(strings.NewReader(`{
"version" : {
"number" : "8.0.0-SNAPSHOT",
"build_flavor" : "default"
},
"tagline" : "You Know, for Search"
}`)),
}, nil
},
},
Expand All @@ -859,7 +865,10 @@ func TestNewTypedClient(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
defer res.Body.Close()

if res.Tagline != "You Know, for Search" {
t.Fatal("unexpected tagline")
}

_, err = NewClient(Config{})
if err != nil {
Expand Down
4 changes: 1 addition & 3 deletions typedapi/api._.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 36 additions & 6 deletions typedapi/asyncsearch/delete/delete.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading