elasticsearch

Does Elasticsearch return partial results when total matches is less than pagination size?


I was wondering, considering millions of element. If I have a query with a pagination of 100 elements and there are exactly 100 elements that match my query, does ElasticSearch will always return all of them once, or is there a possibility that it will sometimes return less that 100 elements with a pagination index ?


Solution

  • The size parameter limits the number of results returned. If the number of matched docs is less than 100, it can return less than 100 results. It can be even 0 results.

    Elasticsearch can return "partial results" if there is any issue with shards. By checking the curl API call response you can make sure about it.

    {
      "took": 12,
      "timed_out": false,
      "_shards": {
        "total": 5,  <-- number of total shards that hits the query
        "successful": 5,  <-- successfully searched shards.
        "skipped": 0,
        "failed": 0 <-- if you see bigger than 0 the result can be partial
      },
      "hits": {
        "total": {
          "value": 100,  <-- the query hit 100 results.
          "relation": "eq"
        },
        "max_score": 1.5,
        "hits": [ <-- there are 100 objects in the JSON because the size parameter in query is set to 100.
          { "_id": "1", "_source": { "name": "Item 1" } },
          { "_id": "2", "_source": { "name": "Item 2" } },
          ...
          { "_id": "100", "_source": { "name": "Item 100" } }
        ]
      }
    }
    

    As a summary answer, if everything works normally If you have a query with a pagination of 100 elements and there are exactly 100 elements that match your query you should always see 100 results in the response body.