0
votes

Why the documents are fetched from primary shard and replica shard when I run the same query again and again. Because of this I am getting different search results.

Example response - 1 - Replica "_shard": 0, "_node": "node_1", "_index": "sample_ind", "_type": "my_type", "_id": "E1", "_score": 2.9560382,

Response-2 Primary shard                            
    "_shard": 0,
    "_node": "node_2",
    "_index": "sample_ind",
    "_type": "my_type",
    "_id": "E2",
    "_score": 2.956294,

node-1 has the replica shard and node-2 has the primary shard. How the query fetch works and why the response comes from primary shard and replica shard when i run the same query multiple time ?

2

2 Answers

1
votes

Difficult to say, may you give me more detail about your results ?

Elastic's site contains a good article to understand how to query fetch results from primary/replica shards: https://www.elastic.co/guide/en/elasticsearch/guide/current/_query_phase.html

Hth,

1
votes

This is basic Elasticsearch information and I strongly suggest to go over the documentation to at least grasp the elementary knowledge about Elasticsearch.

In short, when a query comes to the cluster, the shards that need to be queried can be either primaries or replicas. It does not matter, they have the same data in them and can perform the query equally. I don't recommend running your queries against only primaries or only replicas, as it will create hot-spots in your cluster and can destabilize the cluster.

Also, the scoring on primaries and replicas should be almost the same. Part of the algorithm to calculate the score involves how many documents exist in the shard and the frequency of terms in these documents. The tricky part is that when you update or delete a document, that document is not immediately removed from disk, it is only marked for deletion. In the background Elasticsearch merges shard files and takes smaller, similar in size segments and create a bigger segment and deletes the smaller ones. At merging time the marked-as-deleted documents actually are removed from the index.

Until then, these documents are not returned in searches, but they are considered when calculating the scores as mentioned above.