olivere / elastic

Deprecated: Use the official Elasticsearch client for Go at https://github.com/elastic/go-elasticsearch
https://olivere.github.io/elastic/
MIT License
7.39k stars 1.15k forks source link

API search call returns 400 when aggregation is added: all shards failed [type=search_phase_execution_exception] #1640

Closed jshenpdp closed 2 years ago

jshenpdp commented 2 years ago

Please use the following questions as a guideline to help me answer your issue/question without further inquiry. Thank you.

Which version of Elastic are you using?

[x] elastic.v7 (for Elasticsearch 7.x) [ ] elastic.v6 (for Elasticsearch 6.x) [ ] elastic.v5 (for Elasticsearch 5.x) [ ] elastic.v3 (for Elasticsearch 2.x) [ ] elastic.v2 (for Elasticsearch 1.x)

Please describe the expected behavior

I am currently migrating our client side ES logic from 6 to 7. As I changed the library from github.com/olivere/elastic to github.com/olivere/elastic/v7, I am seeing the API call failure as below.

Please describe the actual behavior

Currently I am seeing getting client errors from a search:

elastic: Error 400 (Bad Request): all shards failed [type=search_phase_execution_exception]

This only appears after search with aggregation. As the aggregation is removed, the search result returns 200. This only appears after client is using Elasticsearch 7.x This appears after client creates and index and inserts a document into an index:

Any steps to reproduce the behavior?

Client side code to create an index if not created, or use an existing index if already created:

       // I am aware that the v7 specifies mapping definition cannot be nested under a type [_doc] unless include_type_name is set to true. Thus I am adding IncludeTypeName(true) for both methods
    _, err = c.IndexPutTemplate(idxPrefix).IncludeTypeName(true).BodyJson(idxTmpl).Order(1).Do(ctx)
    ...
    _, err = c.CreateIndex(idxName).IncludeTypeName(true).BodyString(aliasCfg).Do(ctx)

Client side code to insert a document into an index:

_, err = client.Index().
        Index(alias.WriteAlias).
        Type("_doc").
        Id(ixn.InteractionId).
        BodyJson(doc).
        Do(ctx)

Search code:

// I have used both SearchType("string") and SearchType("_doc") but none of them fixed the 400 client error, neither did adding TypedKeys(true)
earch := s.client.Search(alias.SearchAlias).
            //SearchType("string").
            //TypedKeys(true).
            Aggregation(aggregationName, agg).
            Size(0).
            TrackScores(false)

Also I have a question: The library readme suggests

This is a development branch that is actively being worked on. DO NOT USE IN PRODUCTION! If you want to use stable versions of Elastic, please use Go modules for the 7.x release (or later) or a dependency manager like dep for earlier releases.

Is there another library that I need to use?

olivere commented 2 years ago

You should have a look at the exact output from Elasticsearch. A 4xx response indicates that Elasticsearch didn't understand or denied your request due to an error, and the error message will almost always tell you what exactly it doesn't like.

If you are in the process of updating the library, I advise you to update to the official Elasticsearch driver over at https://github.com/elastic/go-elasticsearch. It recently comes with a so-called Typed API that resembles most of the benefits of this library.