Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sequence number vs version number in elasticsearch

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?

like image 362
Bhawan Avatar asked Nov 08 '19 06:11

Bhawan


People also ask

What is_ seq_ no in Elasticsearch?

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.

How do I check my Elasticsearch version?

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.

What is primary term in Elasticsearch?

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.


1 Answers

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 index

So 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:

  • for all documents, version is 1
  • for document 1, _seq_no is 0 (first index operation)
  • for document 2, _seq_no is 1 (second index operation)
  • for document 3, _seq_no is 2 (third index operation)
like image 99
Val Avatar answered Jan 04 '23 01:01

Val