# Index.sort on sequence numbers

**URL:** <https://discuss.elastic.co/t/index-sort-on-sequence-numbers/274485>\
**Category:** Elasticsearch\
**Created:** [May 31, 2021, 10:37am UTC](https://discuss.elastic.co/t/index-sort-on-sequence-numbers/274485 "2021-05-31T10:37:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![fhalde](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fhalde/32/21419_2.png) [@fhalde](https://discuss.elastic.co/u/fhalde)\
**Post date:** [May 31, 2021, 10:37am UTC](https://discuss.elastic.co/t/index-sort-on-sequence-numbers/274485/1 "2021-05-31T10:37:23Z")

</div>

We have a use case that can greatly benefit from the ability to have an `index.sort` on `_seq_no` meta field of ES but that's not possible it seems

As of now, that's not possible

```auto
curl -XPUT -H "Content-Type:application/json" elasticsearch.local:9200/myindex -d'{ "settings": { "index": { "sort.field": "_seq_no", "sort.order": "asc" } } }'

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "unknown index sort field:[_seq_no]"
      }
    ],
    "type": "illegal_argument_exception",
    "reason": "unknown index sort field:[_seq_no]"
  },
  "status": 400
}

```

I thought maybe as a workaround, I could use `scripted_upsert` and use `_seq_no` from the ctx and add it into the document but ctx does not expose `_seq_no`

Are any of the options I have doable? I can fork off ES temporarily and make those changes!

QQ,  
Do documents with a higher seq\_no on the same shard become visible for search before documents with a lower seq\_no?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [May 31, 2021, 2:30pm UTC](https://discuss.elastic.co/t/index-sort-on-sequence-numbers/274485/2 "2021-05-31T14:30:10Z")

</div>

Hey,

maybe you can explain, why you think this? Wondering if there are others ways of making this work... like adding the current time to a field of the document (no need for seq\_no then).

I'd consider the sequence number an implementation detail and thus would be vary to use this anyway.

--Alex

---

<div class="post-metadata">

**Author:** ![fhalde](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fhalde/32/21419_2.png) [@fhalde](https://discuss.elastic.co/u/fhalde)\
**Post date:** [May 31, 2021, 2:35pm UTC](https://discuss.elastic.co/t/index-sort-on-sequence-numbers/274485/3 "2021-05-31T14:35:25Z")

</div>

@spinscale anything that can be monotonically increasing could work for us. afaik timestamps do not provide that property

I am trying to implement a consumer protocol to read from elasticsearch. the index is expected to be insert-only (no updates). the seq\_no works as an offset for this consumer protocol

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [May 31, 2021, 2:51pm UTC](https://discuss.elastic.co/t/index-sort-on-sequence-numbers/274485/4 "2021-05-31T14:51:58Z")

</div>

Would the Elasticsearch ID autogeneration work? You could store the `_id` field in an extra field in the document via a pipeline.

---

<div class="post-metadata">

**Author:** ![fhalde](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fhalde/32/21419_2.png) [@fhalde](https://discuss.elastic.co/u/fhalde)\
**Post date:** [May 31, 2021, 2:53pm UTC](https://discuss.elastic.co/t/index-sort-on-sequence-numbers/274485/5 "2021-05-31T14:53:26Z")

</div>

`_id` won't provide me an ordered log right? (ordering = insert order). think of this like a kafka log. the offset in kafka = seq\_no in elasticsearch

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [June 1, 2021, 8:03am UTC](https://discuss.elastic.co/t/index-sort-on-sequence-numbers/274485/6 "2021-06-01T08:03:46Z")

</div>

IIRC this is the ID generation code in java [elasticsearch/TimeBasedUUIDGenerator.java at master · elastic/elasticsearch · GitHub](https://github.com/elastic/elasticsearch/blob/master/server/src/main/java/org/elasticsearch/common/TimeBasedUUIDGenerator.java) - not sure if that works with your workload, as it is optimized for lucene.

That said, you are probably aware that there is a commercial feature in elasticsearch doing what you need (namely cross cluster replication), and it took a significant engineering approach to get there. sequence ids played an important part in that, but were just one of the building blocks..

---

<div class="post-metadata">

**Author:** ![fhalde](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fhalde/32/21419_2.png) [@fhalde](https://discuss.elastic.co/u/fhalde)\
**Post date:** [June 1, 2021, 8:41am UTC](https://discuss.elastic.co/t/index-sort-on-sequence-numbers/274485/7 "2021-06-01T08:41:05Z")

</div>

Yes, I'm aware of CCR & it certainly doesn't seem like an easy task whatever I'm doing ☹

---

<div class="post-metadata">

**Author:** ![fhalde](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fhalde/32/21419_2.png) [@fhalde](https://discuss.elastic.co/u/fhalde)\
**Post date:** [June 1, 2021, 8:42am UTC](https://discuss.elastic.co/t/index-sort-on-sequence-numbers/274485/8 "2021-06-01T08:42:21Z")

</div>

Interesting stuff about ID generation! Thx let me take a look

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 29, 2021, 8:42am UTC](https://discuss.elastic.co/t/index-sort-on-sequence-numbers/274485/9 "2021-06-29T08:42:30Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
