Home > Enterprise >  Elasticsearch NEST MultiGet across multiple indexes
Elasticsearch NEST MultiGet across multiple indexes

Time:03-24

I want to run a MultiGet (mget) search query on several IDs across two indexes. This is because I have two indexes, but I don't know which index contains my ID. This is the query:

GET _mget 
{
    "docs" : [
        {
            "_id": "id1",
            "_index": "index1"
        },
        {
            "_id": "id1",
            "_index": "index2"
        }
        /* ....  */
    ]
}

The query works great manually - I get the results and I just ignore the result that returns found: false.

Nest does not support this functionality, only on one index. So I tried to use the low-level client to achieve this, like so:

var data = PostData.Serializable(new
            {
                docs = new[]
                {
                    new {
                        _id = "1",
                        _index = "index1"
                    },
                    new
                    {
                        _id = "1",
                        _index = "index2"
                    }
                }
            });

var response = await lowLevelClient.MultiGetAsync<MultiGetResponse>(data);

However, I'm getting the following exception: Elasticsearch.Net.UnexpectedElasticsearchClientException: 'Constructor on type 'Nest.MultiGetResponseFormatter' not found.'.

Is this the right way to achieve what I want?

CodePudding user response:

Following will help you achieve what you are looking for with NEST

var request = new MultiGetRequest();
request.Documents = new IMultiGetOperation[]
{
    new MultiGetOperation<object>("id1") { Index = "index1" },
    new MultiGetOperation<object>("id1") { Index = "index2" },
};
var multiGetResponse = await client.MultiGetAsync(request);
  • Related