# Can ElasticSearch sort data that is stored as a linked list?

**URL:** <https://discuss.elastic.co/t/can-elasticsearch-sort-data-that-is-stored-as-a-linked-list/17629>\
**Category:** Elasticsearch\
**Created:** [May 20, 2014, 6:06pm UTC](https://discuss.elastic.co/t/can-elasticsearch-sort-data-that-is-stored-as-a-linked-list/17629 "2014-05-20T18:06:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shane\_Claggett](https://avatars.discourse-cdn.com/v4/letter/s/59ef9b/32.png) [@Shane\_Claggett](https://discuss.elastic.co/u/Shane_Claggett)\
**Post date:** [May 20, 2014, 6:06pm UTC](https://discuss.elastic.co/t/can-elasticsearch-sort-data-that-is-stored-as-a-linked-list/17629/1 "2014-05-20T18:06:42Z")

</div>

Hi all,

Is it possible for ES to sort records that are related to one another as a  
linked list? For example, is there a query that would order the following  
sample data?

[  
{  
"id":"572bc3df-f567-432e-8087-718b1517099a",  
"previous":"",  
"next":"3481ba2f-2bf8-4ea2-a187-311ed33d8f85"  
},  
{  
"id":"3481ba2f-2bf8-4ea2-a187-311ed33d8f85",  
"previous":"572bc3df-f567-432e-8087-718b1517099a",  
"next":"dc01b22e-1663-4276-9ebe-5ecdb0d976ef"  
},  
{  
"id":"dc01b22e-1663-4276-9ebe-5ecdb0d976ef",  
"previous":"3481ba2f-2bf8-4ea2-a187-311ed33d8f85",  
"next":"5281764d-7f84-4a8e-910d-124e003aabed"  
},  
{  
"id":"5281764d-7f84-4a8e-910d-124e003aabed",  
"previous":"dc01b22e-1663-4276-9ebe-5ecdb0d976ef",  
"next":""  
}  
]

I've read the scoring and sorting documentation as well as googled around,  
but haven't found anything that would allow me to say "put this item before  
that item if 'this.id'=='that.previous'". Is such a thing possible with ES?

Thanks,  
Shane

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/8b58eb8b-7c01-456f-b8a8-7798844ce548%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8b58eb8b-7c01-456f-b8a8-7798844ce548%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [May 21, 2014, 7:28am UTC](https://discuss.elastic.co/t/can-elasticsearch-sort-data-that-is-stored-as-a-linked-list/17629/2 "2014-05-21T07:28:50Z")

</div>

Unfortunately, this is not possible.

On Tue, May 20, 2014 at 8:06 PM, Shane Claggett [shaneclaggett@gmail.com](mailto:shaneclaggett@gmail.com)wrote:

> Hi all,
> 
> Is it possible for ES to sort records that are related to one another as a  
> linked list? For example, is there a query that would order the following  
> sample data?
> 
> [  
> {  
> "id":"572bc3df-f567-432e-8087-718b1517099a",  
> "previous":"",  
> "next":"3481ba2f-2bf8-4ea2-a187-311ed33d8f85"  
> },  
> {  
> "id":"3481ba2f-2bf8-4ea2-a187-311ed33d8f85",  
> "previous":"572bc3df-f567-432e-8087-718b1517099a",  
> "next":"dc01b22e-1663-4276-9ebe-5ecdb0d976ef"  
> },  
> {  
> "id":"dc01b22e-1663-4276-9ebe-5ecdb0d976ef",  
> "previous":"3481ba2f-2bf8-4ea2-a187-311ed33d8f85",  
> "next":"5281764d-7f84-4a8e-910d-124e003aabed"  
> },  
> {  
> "id":"5281764d-7f84-4a8e-910d-124e003aabed",  
> "previous":"dc01b22e-1663-4276-9ebe-5ecdb0d976ef",  
> "next":""  
> }  
> ]
> 
> I've read the scoring and sorting documentation as well as googled around,  
> but haven't found anything that would allow me to say "put this item before  
> that item if 'this.id'=='that.previous'". Is such a thing possible with  
> ES?
> 
> Thanks,  
> Shane
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/8b58eb8b-7c01-456f-b8a8-7798844ce548%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8b58eb8b-7c01-456f-b8a8-7798844ce548%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/8b58eb8b-7c01-456f-b8a8-7798844ce548%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/8b58eb8b-7c01-456f-b8a8-7798844ce548%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
Adrien Grand

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j6wo7mqm3JYnUXXrBRv2OSpJJJL-2GQBVh3fRSjXCRYoQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j6wo7mqm3JYnUXXrBRv2OSpJJJL-2GQBVh3fRSjXCRYoQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Shane\_Claggett](https://avatars.discourse-cdn.com/v4/letter/s/59ef9b/32.png) [@Shane\_Claggett](https://discuss.elastic.co/u/Shane_Claggett)\
**Post date:** [May 21, 2014, 2:44pm UTC](https://discuss.elastic.co/t/can-elasticsearch-sort-data-that-is-stored-as-a-linked-list/17629/3 "2014-05-21T14:44:36Z")

</div>

Thanks Adrien, I had the suspicion that was the case, but I appreciate you  
confirming it. On to plan B!

On Wed, May 21, 2014 at 12:28 AM, Adrien Grand \<  
[adrien.grand@elasticsearch.com](mailto:adrien.grand@elasticsearch.com)\> wrote:

> Unfortunately, this is not possible.
> 
> On Tue, May 20, 2014 at 8:06 PM, Shane Claggett [shaneclaggett@gmail.com](mailto:shaneclaggett@gmail.com)wrote:
> 
> > Hi all,
> > 
> > Is it possible for ES to sort records that are related to one another as  
> > a linked list? For example, is there a query that would order the following  
> > sample data?
> > 
> > [  
> > {  
> > "id":"572bc3df-f567-432e-8087-718b1517099a",  
> > "previous":"",  
> > "next":"3481ba2f-2bf8-4ea2-a187-311ed33d8f85"  
> > },  
> > {  
> > "id":"3481ba2f-2bf8-4ea2-a187-311ed33d8f85",  
> > "previous":"572bc3df-f567-432e-8087-718b1517099a",  
> > "next":"dc01b22e-1663-4276-9ebe-5ecdb0d976ef"  
> > },  
> > {  
> > "id":"dc01b22e-1663-4276-9ebe-5ecdb0d976ef",  
> > "previous":"3481ba2f-2bf8-4ea2-a187-311ed33d8f85",  
> > "next":"5281764d-7f84-4a8e-910d-124e003aabed"  
> > },  
> > {  
> > "id":"5281764d-7f84-4a8e-910d-124e003aabed",  
> > "previous":"dc01b22e-1663-4276-9ebe-5ecdb0d976ef",  
> > "next":""  
> > }  
> > ]
> > 
> > I've read the scoring and sorting documentation as well as googled  
> > around, but haven't found anything that would allow me to say "put this  
> > item before that item if 'this.id'=='that.previous'". Is such a thing  
> > possible with ES?
> > 
> > Thanks,  
> > Shane
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).
> > 
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/8b58eb8b-7c01-456f-b8a8-7798844ce548%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8b58eb8b-7c01-456f-b8a8-7798844ce548%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/8b58eb8b-7c01-456f-b8a8-7798844ce548%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/8b58eb8b-7c01-456f-b8a8-7798844ce548%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> --  
> Adrien Grand
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/11fVxjIHg7w/unsubscribe](https://groups.google.com/d/topic/elasticsearch/11fVxjIHg7w/unsubscribe).  
> To unsubscribe from this group and all its topics, send an email to  
> [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j6wo7mqm3JYnUXXrBRv2OSpJJJL-2GQBVh3fRSjXCRYoQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j6wo7mqm3JYnUXXrBRv2OSpJJJL-2GQBVh3fRSjXCRYoQ%40mail.gmail.com)[https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j6wo7mqm3JYnUXXrBRv2OSpJJJL-2GQBVh3fRSjXCRYoQ%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j6wo7mqm3JYnUXXrBRv2OSpJJJL-2GQBVh3fRSjXCRYoQ%40mail.gmail.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAMyodgYwSA4g%2BKafi4vJEAxadTxCqBvtT3WtMccBdzjE4QbGFg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAMyodgYwSA4g%2BKafi4vJEAxadTxCqBvtT3WtMccBdzjE4QbGFg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![dmitry\_polushkin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dmitry_polushkin/32/1326_2.png) [@dmitry\_polushkin](https://discuss.elastic.co/u/dmitry_polushkin)\
**Post date:** [August 21, 2014, 8:28pm UTC](https://discuss.elastic.co/t/can-elasticsearch-sort-data-that-is-stored-as-a-linked-list/17629/4 "2014-08-21T20:28:19Z")

</div>

Hi Shane,

How did you solved this problem?

Regards,  
Dmitry

---

<div class="post-metadata">

**Author:** ![Shane\_Claggett](https://avatars.discourse-cdn.com/v4/letter/s/59ef9b/32.png) [@Shane\_Claggett](https://discuss.elastic.co/u/Shane_Claggett)\
**Post date:** [August 27, 2014, 5:35pm UTC](https://discuss.elastic.co/t/can-elasticsearch-sort-data-that-is-stored-as-a-linked-list/17629/5 "2014-08-27T17:35:43Z")

</div>

Hi Dmitry,

I did indeed come up with a solution. Basically, I added a field to the  
schema of type "double", and use that to sort the items. Linked list items  
that are added to the end of the growing list are assigned values that are  
sequential integers, e.g.:

Item A - 1.0  
Item B - 2.0  
Item C - 3.0

If an item is inserted into the linked list between A and B, it is assigned  
a value midway:

Item A - 1.0  
Item D - 1.5  
Item B - 2.0  
Item C - 3.0

Likewise if something is inserted at the head of the list:

Item E - 0.5  
Item A - 1.0  
Item D - 1.5  
Item B - 2.0  
Item C - 3.0

This is in addition to keeping the previous and next item references, so  
the index can be regenerated as needed, e.g. if something gets corrupted,  
which is entirely likely when writing something like this yourself.

This solution certainly has the feel of a hack, but it has worked well so  
far. Double values allow for a fairly high degree of precision, so this  
trick can be carried on for the insertion of many items. It's not as good  
as native support in the system for a linked list structure, but adequate  
for the practical needs of the application I’m working on.

Be aware that two threads inserting an item at the same place and same time  
would lead to duplicate index values unless some sort of "fuzziness" is  
included when calculating the index.

Cheers,  
Shane

On Thu, Aug 21, 2014 at 4:28 PM, dmitry.polushkin \<  
[dmitry.polushkin@gmail.com](mailto:dmitry.polushkin@gmail.com)\> wrote:

> Hi Shane,
> 
> How did you solved this problem?
> 
> Regards,  
> Dmitry
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/Can-ElasticSearch-sort-data-that-is-stored-as-a-linked-list-tp4056166p4062249.html](http://elasticsearch-users.115913.n3.nabble.com/Can-ElasticSearch-sort-data-that-is-stored-as-a-linked-list-tp4056166p4062249.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/11fVxjIHg7w/unsubscribe](https://groups.google.com/d/topic/elasticsearch/11fVxjIHg7w/unsubscribe).  
> To unsubscribe from this group and all its topics, send an email to  
> [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/1408652899659-4062249.post%40n3.nabble.com](https://groups.google.com/d/msgid/elasticsearch/1408652899659-4062249.post%40n3.nabble.com)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAMyodgbiuLXV827OFkEXsfRzsovSv6MYPCofxFd\_jpc%3DgH9izQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAMyodgbiuLXV827OFkEXsfRzsovSv6MYPCofxFd_jpc%3DgH9izQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [July 6, 2017, 1:05am UTC](https://discuss.elastic.co/t/can-elasticsearch-sort-data-that-is-stored-as-a-linked-list/17629/6 "2017-07-06T01:05:59Z")

</div>


