I am reading the concepts of elasticsearch-7.4 and I got to know about two fields.
_seq_no
and _version
.
As per the documentation:
Version
Returns a version for each search hit.
Sequence Numbers and Primary Term
Returns the sequence number and primary term of the last modification to each search hit.
But it is not clearing anything related to when they both will be different or same for a document.
I created an index test
PUT /test/_doc/_mapping
{
"properties": {
"total_price" : {
"type": "integer"
},
"final_price": {
"type": "integer"
},
"base_price": {
"enabled": false
}
}
}
I am updating the full document using PUT API
.
PUT /test/_doc/2
{
"total_price": 10,
"final_price": 10,
"base_price": 10
}
Both _seq_no and _version are increasing in this case.
On doing partial updates using UPDATE API
,
POST /test/_doc/2/_update
{
"doc" : {
"base_price" : 10000
}
}
Both _seq_no and _version are increasing in this case too.
So, I am unable to find the case when only one field is changing but the other is not.
When will both the fields be different?
the _seq_no and _primary_term as parameter needed to implement the optimistic locking. Elasticsearch keeps tracks of the sequence number and primary term of the last operation to have changed each of the documents it stores.
If the Elasticsearch process isn't running, you may need to grab the version number using the command line. At first, navigate your terminal to the Elasticsearch installation directory. When using apt to install the database, you may find it in the /usr/share/elasticsearch directory.
The primary term for a replication group is just a counter for how many times the primary shard has changed. More: Primary terms are a way for Elasticsearch to distinguish between old and new primary shards when the primary shard of a replication group has changed.
Sequence numbers have been introduced in ES 6.0.0. Just before that release came out, they were very well explained in this blog article.
But in summary,
version
is a sequential number that counts the number of time a document was updated_seq_no
is a sequential number that counts the number of operations that happened on the indexSo if you create a second document, you'll see that version
and _seq_no
will be different.
Let's create three documents:
POST test/_doc/_bulk
{"index": {}}
{"test": 1}
{"index": {}}
{"test": 2}
{"index": {}}
{"test": 3}
In the response, you'll get the payload below.
{
"took" : 166,
"errors" : false,
"items" : [
{
"index" : {
"_index" : "test",
"_type" : "_doc",
"_id" : "d2zbSW4BJvP7VWZfYMwQ",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "test",
"_type" : "_doc",
"_id" : "eGzbSW4BJvP7VWZfYMwQ",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1,
"status" : 201
}
},
{
"index" : {
"_index" : "test",
"_type" : "_doc",
"_id" : "eWzbSW4BJvP7VWZfYMwQ",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1,
"status" : 201
}
}
]
}
As you can see:
_seq_no
is 0 (first index operation)_seq_no
is 1 (second index operation)_seq_no
is 2 (third index operation)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With