Search code examples
elasticsearchnest

Is it possible to combine MultiGet with a Bool query in elasticsearch?


The MultiGet api can be used to fetch multiple documents based on given ids. (https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-multi-get.html)

Is it possible to add more filters to this operation? Given a list of ids, I only want to find the documents for which 'color' = 'green'.

Example:

Let's say the following documents exist:

  • Doc 1: {"id": "1", "color": "green" }
  • Doc 2: {"id": "2", "color": "red" }
  • Doc 3: {"id": "3", "color": "green"}
  • Doc 4: {"id": "4", "color": "green"}

Goal:

Get documents with ids 1, 2 or 4, for which the color is "Green".

Expected result:

[Doc 1, Doc 4]

The query so far...:

GET /_mget
{
  "docs": [
    {
      "_index": "my-index-000001",
      "_id": "1"
    },
    {
      "_index": "my-index-000001",
      "_id": "2"
    },
    {
      "_index": "my-index-000001",
      "_id": "4"
    }
  ]
}

or using NEST in C#:

var ids = new List<string> {"1", "2", "4"};
var result = await _elasticClient
   .MultiGetAsync(s => s
                    .Index("my-index-000001")
                    .GetMany<SomeRecordObject>(ids)
                );

So I'm actually looking for a way to add a Bool query to the MultiGet query (I think). Anyone who can point me towards the right direction?


Solution

  • multi get is only for getting multiple documents by id. If you need to perform querying or filtering, then a search query is needed.

    Note that multi get and search have different behaviour when it comes to just indexed documents. With multi get, a just indexed document can be immediately retrieved using its id. With search however, a document will only appear in search results once the index is refreshed (refresh interval elapses (by default, 1 second) or index is manually refreshed with the refresh API).