# How to sort my data in elasticsearch

**URL:** <https://discuss.elastic.co/t/how-to-sort-my-data-in-elasticsearch/338072>\
**Category:** Logstash\
**Created:** [July 11, 2023, 8:01am UTC](https://discuss.elastic.co/t/how-to-sort-my-data-in-elasticsearch/338072 "2023-07-11T08:01:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![lz840408](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lz840408/32/106315_2.png) [@lz840408](https://discuss.elastic.co/u/lz840408)\
**Post date:** [July 11, 2023, 8:01am UTC](https://discuss.elastic.co/t/how-to-sort-my-data-in-elasticsearch/338072/1 "2023-07-11T08:01:16Z")

</div>

how to sort by asc in logstash?  
i want new add field,it's self increment column,and insert dest index,how make it?

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [July 11, 2023, 9:11am UTC](https://discuss.elastic.co/t/how-to-sort-my-data-in-elasticsearch/338072/2 "2023-07-11T09:11:52Z")

</div>

Hi @lz840408,

Are you ingesting your data using Logstash into Elasticsearch or another target? Is there any reason you don't want to apply the sorting at query stage rather than ingestion?

---

<div class="post-metadata">

**Author:** ![lz840408](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lz840408/32/106315_2.png) [@lz840408](https://discuss.elastic.co/u/lz840408)\
**Post date:** [July 11, 2023, 10:43am UTC](https://discuss.elastic.co/t/how-to-sort-my-data-in-elasticsearch/338072/3 "2023-07-11T10:43:49Z")

</div>

> [@carly.richmond](#):
>
> Are you ingesting your data using Logstash into Elasticsearch or another target? Is there any reason you don't want to apply the sorting at query stage rather than ingestion?

if input is elasticsearch,how to sort in input?

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [July 11, 2023, 11:27am UTC](https://discuss.elastic.co/t/how-to-sort-my-data-in-elasticsearch/338072/4 "2023-07-11T11:27:05Z")

</div>

Can you please give a bit more information on what you're trying to do? What are the inputs and outputs to your Logstash pipeline?

---

<div class="post-metadata">

**Author:** ![Rios](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rios/32/95745_2.png) [@Rios](https://discuss.elastic.co/u/Rios)\
**Post date:** [July 12, 2023, 5:12pm UTC](https://discuss.elastic.co/t/how-to-sort-my-data-in-elasticsearch/338072/5 "2023-07-12T17:12:34Z")

</div>

If I understood correctly, you want to sort data as is comes.  
Badger already [responded](https://stackoverflow.com/questions/71216330/about-logstash-hot-to-output-the-fillered-log-by-collect-order)  
Another option is to use ID from input or generate by your as autoincrement and use that for doc\_id in output. Always you can use Ruby code for tweaking.

If this not helped, as Carly said, pls provide more information.

---

<div class="post-metadata">

**Author:** ![lz840408](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lz840408/32/106315_2.png) [@lz840408](https://discuss.elastic.co/u/lz840408)\
**Post date:** [July 13, 2023, 6:53am UTC](https://discuss.elastic.co/t/how-to-sort-my-data-in-elasticsearch/338072/7 "2023-07-13T06:53:49Z")

</div>

> [@lz840408](#):
>
> you are right,i want test increnment sync data between es,so i want build a new column as incr sequence attribute.  
> do you have ruby code or sample?  
> because i trying some metod,but not effect

you are right,i want test increnment sync data between es,so i want build a new column as incr sequence attribute.  
do you have ruby code or sample?  
because i trying some metod,but not effect

---

<div class="post-metadata">

**Author:** ![lz840408](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lz840408/32/106315_2.png) [@lz840408](https://discuss.elastic.co/u/lz840408)\
**Post date:** [July 13, 2023, 6:58am UTC](https://discuss.elastic.co/t/how-to-sort-my-data-in-elasticsearch/338072/8 "2023-07-13T06:58:49Z")

</div>

i found an "action" parameter in output of elasticsearch plugins,it's had four option index,delete,create,update,i notice update and delete option,it means opertion by id column,this id column is elasticsearch default \_id column?  
if my index is not assign id obvious,it's random generate, what can i do?

---

<div class="post-metadata">

**Author:** ![carly.richmond](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/carly.richmond/32/104935_2.png) [@carly.richmond](https://discuss.elastic.co/u/carly.richmond)\
**Post date:** [July 13, 2023, 8:23am UTC](https://discuss.elastic.co/t/how-to-sort-my-data-in-elasticsearch/338072/9 "2023-07-13T08:23:06Z")

</div>

For Ruby code samples as per one of @Rios's suggestion, I would recommend having a look at the [examples in the documentation](https://www.elastic.co/guide/en/logstash/current/plugins-filters-ruby.html), particularly those for creating a new field.

If you don't have an id attribute in your documents already that makes sense, you could also have a look at the [UUID plugin as well](https://www.elastic.co/guide/en/logstash/current/plugins-filters-uuid.html). There are many ways to generate an ID.

---

<div class="post-metadata">

**Author:** ![Rios](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rios/32/95745_2.png) [@Rios](https://discuss.elastic.co/u/Rios)\
**Post date:** [July 17, 2023, 5:13am UTC](https://discuss.elastic.co/t/how-to-sort-my-data-in-elasticsearch/338072/10 "2023-07-17T05:13:38Z")

</div>

Have you solved this?  
Can be messy with autoincrementing, because after every exit process you have to get max(id) from ES.

---

<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:** [August 14, 2023, 5:13am UTC](https://discuss.elastic.co/t/how-to-sort-my-data-in-elasticsearch/338072/11 "2023-08-14T05:13:52Z")

</div>

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