Help Center/ MapReduce Service/ Component Operation Guide (LTS) (Ankara Region)/ Using Elasticsearch/ Common Issues About Elasticsearch/ What Can I Do If two Different Values Are Returned for hits.total When the Same Statement Is Used to Query Data in Elasticsearch in the Same Condition for Twice?
Updated on 2024-11-29 GMT+08:00

What Can I Do If two Different Values Are Returned for hits.total When the Same Statement Is Used to Query Data in Elasticsearch in the Same Condition for Twice?

Symptom

The values returned for hits.total are different when the same Elasticsearch statement is used to query data in the same query condition (for example, the value ranges from 1 to 92).

Cause Analysis

  1. This problem is a difficult problem in the Elasticsearch community. For details, visit https://github.com/elastic/elasticsearch/issues/25603.
  2. The community provides two workarounds:
    1. Specify the preference parameter _primary_first, which indicates that the query is preferentially performed on the primary shard. However, the query performance is affected when the number of requests with high concurrency is large.
    1. Add the sort parameter to the query statement to sort data by keyword in the index.

Solution

Add the sort parameter to the query statement to sort data by keyword in the index. Determine whether to change the sorting rules based on the actual service logic if sorting needs to be used.