From 5f2e646bd1d11ea0ea6ac6a1d5e828892f3aeed6 Mon Sep 17 00:00:00 2001 From: Maxime Lamure Date: Wed, 29 Jun 2022 22:51:18 -0700 Subject: [PATCH] Fix backward compat issues --- client.go | 35 +++++++++++++++++------------------ client_test.go | 8 ++++---- go.mod | 3 +++ structs.go | 14 ++++++++++++-- 4 files changed, 36 insertions(+), 24 deletions(-) create mode 100644 go.mod diff --git a/client.go b/client.go index 209d666..22355af 100644 --- a/client.go +++ b/client.go @@ -23,7 +23,7 @@ type Client interface { InsertDocument(indexName, documentType, identifier string, data []byte) (*InsertDocument, error) Document(indexName, documentType, identifier string) (*Document, error) DeleteDocument(indexName, documentType, identifier string) (*Document, error) - Bulk(data []byte) (*Bulk, error) + Bulk(indexName string, data []byte) (*Bulk, error) Search(indexName, documentType, data string, explain bool) (*SearchResult, error) MSearch(queries []MSearchQuery) (*MSearchResult, error) Suggest(indexName, data string) ([]byte, error) @@ -56,11 +56,11 @@ func NewClientFromUrl(rawurl string) Client { } // CreateIndex instantiates an index -// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-create-index.html +// https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html func (c *client) CreateIndex(indexName, mapping string) (*Response, error) { url := c.Host.String() + "/" + indexName reader := bytes.NewBufferString(mapping) - response, err := sendHTTPRequest("POST", url, reader) + response, err := sendHTTPRequest("PUT", url, reader) if err != nil { return &Response{}, err } @@ -75,7 +75,7 @@ func (c *client) CreateIndex(indexName, mapping string) (*Response, error) { } // DeleteIndex deletes an existing index. -// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-delete-index.html +// https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-index.html func (c *client) DeleteIndex(indexName string) (*Response, error) { url := c.Host.String() + "/" + indexName response, err := sendHTTPRequest("DELETE", url, nil) @@ -93,7 +93,7 @@ func (c *client) DeleteIndex(indexName string) (*Response, error) { } // UpdateIndexSetting changes specific index level settings in real time -// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-update-settings.html +// https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html func (c *client) UpdateIndexSetting(indexName, mapping string) (*Response, error) { url := c.Host.String() + "/" + indexName + "/_settings" reader := bytes.NewBufferString(mapping) @@ -112,7 +112,7 @@ func (c *client) UpdateIndexSetting(indexName, mapping string) (*Response, error } // IndexSettings allows to retrieve settings of index -// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-get-settings.html +// https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-get-settings.html func (c *client) IndexSettings(indexName string) (Settings, error) { url := c.Host.String() + "/" + indexName + "/_settings" response, err := sendHTTPRequest("GET", url, nil) @@ -132,7 +132,7 @@ func (c *client) IndexSettings(indexName string) (Settings, error) { } // IndexExists allows to check if the index exists or not. -// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-exists.html +// https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-exists.html func (c *client) IndexExists(indexName string) (bool, error) { url := c.Host.String() + "/" + indexName httpClient := &http.Client{} @@ -162,9 +162,9 @@ func (c *client) Status(indices string) (*Settings, error) { } // InsertDocument adds or updates a typed JSON document in a specific index, making it searchable -// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-index_.html +// https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html func (c *client) InsertDocument(indexName, documentType, identifier string, data []byte) (*InsertDocument, error) { - url := c.Host.String() + "/" + indexName + "/" + documentType + "/" + identifier + url := c.Host.String() + "/" + indexName + "/_doc/" + identifier reader := bytes.NewBuffer(data) response, err := sendHTTPRequest("POST", url, reader) if err != nil { @@ -219,8 +219,8 @@ func (c *client) DeleteDocument(indexName, documentType, identifier string) (*Do // Bulk makes it possible to perform many index/delete operations in a single API call. // This can greatly increase the indexing speed. // http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-bulk.html -func (c *client) Bulk(data []byte) (*Bulk, error) { - url := c.Host.String() + "/_bulk" +func (c *client) Bulk(indexName string, data []byte) (*Bulk, error) { + url := c.Host.String() + "/" + indexName + "/_bulk" reader := bytes.NewBuffer(data) response, err := sendHTTPRequest("POST", url, reader) if err != nil { @@ -239,11 +239,8 @@ func (c *client) Bulk(data []byte) (*Bulk, error) { // Search allows to execute a search query and get back search hits that match the query // http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-delete.html func (c *client) Search(indexName, documentType, data string, explain bool) (*SearchResult, error) { - if len(documentType) > 0 { - documentType = documentType + "/" - } - url := c.Host.String() + "/" + indexName + "/" + documentType + "/_search" + url := c.Host.String() + "/" + indexName + "/_search" if explain { url += "?explain" } @@ -366,9 +363,11 @@ func sendHTTPRequest(method, url string, body io.Reader) ([]byte, error) { return nil, err } - if method == "POST" || method == "PUT" { - req.Header.Set("Content-Type", "application/x-www-form-urlencoded") - } + // if method == "POST" || method == "PUT" { + // req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + // } + + req.Header.Set("Content-Type", "application/json") newReq, err := client.Do(req) if err != nil { diff --git a/client_test.go b/client_test.go index 0a20e86..c687d3f 100644 --- a/client_test.go +++ b/client_test.go @@ -154,7 +154,7 @@ func TestSearch(t *testing.T) { buffer.WriteByte('\n') } - _, err := client.Bulk(buffer.Bytes()) + _, err := client.Bulk(IndexName, buffer.Bytes()) helper.OK(t, err) //We have to wait after a bulk @@ -163,7 +163,7 @@ func TestSearch(t *testing.T) { //Search search, err := client.Search(IndexName, ProductDocumentType, SearchByColorQuery("red"), false) helper.OK(t, err) - helper.Assert(t, search.Hits.Total == 2, "The search doesn't return all matched items") + helper.Assert(t, search.Hits.Total.Value == 2, "The search doesn't return all matched items") //MSearch @@ -173,8 +173,8 @@ func TestSearch(t *testing.T) { msresult, err := client.MSearch(mqueries) helper.OK(t, err) - helper.Assert(t, msresult.Responses[0].Hits.Total == 1, "The msearch doesn't return all matched items") - helper.Assert(t, msresult.Responses[1].Hits.Total == 2, "The msearch doesn't return all matched items") + helper.Assert(t, msresult.Responses[0].Hits.Total.Value == 1, "The msearch doesn't return all matched items") + helper.Assert(t, msresult.Responses[1].Hits.Total.Value == 2, "The msearch doesn't return all matched items") //Delete the index deleteResponse, err := client.DeleteIndex(IndexName) diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b520f9a --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/maximelamure/elasticsearch + +go 1.18 diff --git a/structs.go b/structs.go index ce8812c..868f6ab 100644 --- a/structs.go +++ b/structs.go @@ -67,7 +67,13 @@ type Bulk struct { ID string `json:"_id"` Version int `json:"_version"` Status int `json:"status"` - Error string `json:"error"` + Error struct { + Type string `json:"status"` + Reason string `json:"reason"` + Index_UUID string `json:"index_uuid"` + Shard string `json:"shard"` + Index string `json:"index"` + } `json:"error"` } `json:"index"` } `json:"items"` } @@ -79,6 +85,7 @@ type SearchResult struct { Shards struct { Total int `json:"total"` Successful int `json:"successful"` + Skipped int `json:"skipped"` Failed int `json:"failed"` } `json:"_shards"` Hits ResultHits `json:"hits"` @@ -87,7 +94,10 @@ type SearchResult struct { // ResultHits represents the result of the search hits type ResultHits struct { - Total int `json:"total"` + Total struct { + Value int `json:"value"` + Relation string `json:"relation"` + } `json:"total"` MaxScore float32 `json:"max_score"` Hits []struct { Index string `json:"_index"`