# Sorting Second Search Based on Sort of First Search (Sorting Path Feature)

**URL:** <https://discuss.elastic.co/t/sorting-second-search-based-on-sort-of-first-search-sorting-path-feature/3602>\
**Category:** Elasticsearch\
**Created:** [November 26, 2010, 2:15pm UTC](https://discuss.elastic.co/t/sorting-second-search-based-on-sort-of-first-search-sorting-path-feature/3602 "2010-11-26T14:15:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ted\_Karmel](https://avatars.discourse-cdn.com/v4/letter/t/e9a140/32.png) [@Ted\_Karmel](https://discuss.elastic.co/u/Ted_Karmel)\
**Post date:** [November 26, 2010, 2:15pm UTC](https://discuss.elastic.co/t/sorting-second-search-based-on-sort-of-first-search-sorting-path-feature/3602/1 "2010-11-26T14:15:39Z")

</div>

Hi Elasticsearchers,

I was wondering if anyone encountered the issue above and what their  
experience was.

Basically, say you have two related searches for two types of  
documents. Perhaps, the documents are sharded using the new routing  
feature based on a document field value of the first search. Assume  
that both documents have date fields. But, you want to sort the first  
search based on the date field values of the first type of document.  
And you will display the second search results side by side with the  
first search results. But, to maintain some order, you want to sort  
the second search results based on the date field values of the first  
type of documents. Otherwise, you may have search results displayed  
side by side that are not related.

In other words, is there a sorting path akin to routing path in  
Elasticsearch? Or does this have to be done externally?

Cheers,  
Ted

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [November 26, 2010, 8:33pm UTC](https://discuss.elastic.co/t/sorting-second-search-based-on-sort-of-first-search-sorting-path-feature/3602/2 "2010-11-26T20:33:10Z")

</div>

Not sure I followed exactly what you are after. Are those two different  
search queries, but both sorted by date? How does the first query affect the  
second query?

On Fri, Nov 26, 2010 at 4:15 PM, Ted Karmel [ted.karmel@gmail.com](mailto:ted.karmel@gmail.com) wrote:

> Hi Elasticsearchers,
> 
> I was wondering if anyone encountered the issue above and what their  
> experience was.
> 
> Basically, say you have two related searches for two types of  
> documents. Perhaps, the documents are sharded using the new routing  
> feature based on a document field value of the first search. Assume  
> that both documents have date fields. But, you want to sort the first  
> search based on the date field values of the first type of document.  
> And you will display the second search results side by side with the  
> first search results. But, to maintain some order, you want to sort  
> the second search results based on the date field values of the first  
> type of documents. Otherwise, you may have search results displayed  
> side by side that are not related.
> 
> In other words, is there a sorting path akin to routing path in  
> Elasticsearch? Or does this have to be done externally?
> 
> Cheers,  
> Ted

---

<div class="post-metadata">

**Author:** ![Ted\_Karmel](https://avatars.discourse-cdn.com/v4/letter/t/e9a140/32.png) [@Ted\_Karmel](https://discuss.elastic.co/u/Ted_Karmel)\
**Post date:** [November 27, 2010, 9:00pm UTC](https://discuss.elastic.co/t/sorting-second-search-based-on-sort-of-first-search-sorting-path-feature/3602/3 "2010-11-27T21:00:20Z")

</div>

Yes, they are two different queries on two different document types.  
The documents share a field. But you do not want to sort based on  
that field. You want to sort based on the date field of the first  
search.

A more concrete example may perhaps make what I'm trying to say more  
clear. Say you have one document which basically represents an image:  
{ imagename: joe, imageurl: /pictures/joe.jpg, creationdate:  
2010-11-20T13:12:09 }. And you have a document which represents the  
number of times the image has been viewed: { imagename: joe,  
imageviews: 26, lastviewed: 20010-11-26T10:21:34 }.

You need to display the documents side by side - the image name and  
url along with the total number of views. You have multiple documents  
and you choose to sort by the creationdate instead of name. Needless  
to say, if both documents were sorted by the shared field of imagename  
we wouldn't be discussing this.

So the issue is how do you ensure that the results from the query on  
the second document of image views is ordered in the same way as the  
results from the query on the first document?

I hope this is clear. Apologies if it isn't. Let me know and I will  
try to explain myself better.

On Fri, Nov 26, 2010 at 8:33 PM, Shay Banon  
[shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com) wrote:

> Not sure I followed exactly what you are after. Are those two different  
> search queries, but both sorted by date? How does the first query affect the  
> second query?
> 
> On Fri, Nov 26, 2010 at 4:15 PM, Ted Karmel [ted.karmel@gmail.com](mailto:ted.karmel@gmail.com) wrote:
> 
> > Hi Elasticsearchers,
> > 
> > I was wondering if anyone encountered the issue above and what their  
> > experience was.
> > 
> > Basically, say you have two related searches for two types of  
> > documents. Perhaps, the documents are sharded using the new routing  
> > feature based on a document field value of the first search. Assume  
> > that both documents have date fields. But, you want to sort the first  
> > search based on the date field values of the first type of document.  
> > And you will display the second search results side by side with the  
> > first search results. But, to maintain some order, you want to sort  
> > the second search results based on the date field values of the first  
> > type of documents. Otherwise, you may have search results displayed  
> > side by side that are not related.
> > 
> > In other words, is there a sorting path akin to routing path in  
> > Elasticsearch? Or does this have to be done externally?
> > 
> > Cheers,  
> > Ted

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [November 27, 2010, 9:07pm UTC](https://discuss.elastic.co/t/sorting-second-search-based-on-sort-of-first-search-sorting-path-feature/3602/4 "2010-11-27T21:07:09Z")

</div>

I think I got it now. No, there isn't a mechanism to do it out of the box in  
ES, so you will have to do it on the client side. I have some ideas on how  
to solve this, but it still need to be implemented 😉

On Sat, Nov 27, 2010 at 11:00 PM, Ted Karmel [ted.karmel@gmail.com](mailto:ted.karmel@gmail.com) wrote:

> Yes, they are two different queries on two different document types.  
> The documents share a field. But you do not want to sort based on  
> that field. You want to sort based on the date field of the first  
> search.
> 
> A more concrete example may perhaps make what I'm trying to say more  
> clear. Say you have one document which basically represents an image:  
> { imagename: joe, imageurl: /pictures/joe.jpg, creationdate:  
> 2010-11-20T13:12:09 }. And you have a document which represents the  
> number of times the image has been viewed: { imagename: joe,  
> imageviews: 26, lastviewed: 20010-11-26T10:21:34 }.
> 
> You need to display the documents side by side - the image name and  
> url along with the total number of views. You have multiple documents  
> and you choose to sort by the creationdate instead of name. Needless  
> to say, if both documents were sorted by the shared field of imagename  
> we wouldn't be discussing this.
> 
> So the issue is how do you ensure that the results from the query on  
> the second document of image views is ordered in the same way as the  
> results from the query on the first document?
> 
> I hope this is clear. Apologies if it isn't. Let me know and I will  
> try to explain myself better.
> 
> On Fri, Nov 26, 2010 at 8:33 PM, Shay Banon  
> [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com) wrote:
> 
> > Not sure I followed exactly what you are after. Are those two different  
> > search queries, but both sorted by date? How does the first query affect  
> > the  
> > second query?
> > 
> > On Fri, Nov 26, 2010 at 4:15 PM, Ted Karmel [ted.karmel@gmail.com](mailto:ted.karmel@gmail.com)  
> > wrote:
> > 
> > > Hi Elasticsearchers,
> > > 
> > > I was wondering if anyone encountered the issue above and what their  
> > > experience was.
> > > 
> > > Basically, say you have two related searches for two types of  
> > > documents. Perhaps, the documents are sharded using the new routing  
> > > feature based on a document field value of the first search. Assume  
> > > that both documents have date fields. But, you want to sort the first  
> > > search based on the date field values of the first type of document.  
> > > And you will display the second search results side by side with the  
> > > first search results. But, to maintain some order, you want to sort  
> > > the second search results based on the date field values of the first  
> > > type of documents. Otherwise, you may have search results displayed  
> > > side by side that are not related.
> > > 
> > > In other words, is there a sorting path akin to routing path in  
> > > Elasticsearch? Or does this have to be done externally?
> > > 
> > > Cheers,  
> > > Ted

---

<div class="post-metadata">

**Author:** ![Ted\_Karmel](https://avatars.discourse-cdn.com/v4/letter/t/e9a140/32.png) [@Ted\_Karmel](https://discuss.elastic.co/u/Ted_Karmel)\
**Post date:** [November 27, 2010, 9:29pm UTC](https://discuss.elastic.co/t/sorting-second-search-based-on-sort-of-first-search-sorting-path-feature/3602/5 "2010-11-27T21:29:03Z")

</div>

Ok thanks Shay... Appreciated anyway.

On Sat, Nov 27, 2010 at 9:07 PM, Shay Banon  
[shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com) wrote:

> I think I got it now. No, there isn't a mechanism to do it out of the box in  
> ES, so you will have to do it on the client side. I have some ideas on how  
> to solve this, but it still need to be implemented 😉
> 
> On Sat, Nov 27, 2010 at 11:00 PM, Ted Karmel [ted.karmel@gmail.com](mailto:ted.karmel@gmail.com) wrote:
> 
> > Yes, they are two different queries on two different document types.  
> > The documents share a field. But you do not want to sort based on  
> > that field. You want to sort based on the date field of the first  
> > search.
> > 
> > A more concrete example may perhaps make what I'm trying to say more  
> > clear. Say you have one document which basically represents an image:  
> > { imagename: joe, imageurl: /pictures/joe.jpg, creationdate:  
> > 2010-11-20T13:12:09 }. And you have a document which represents the  
> > number of times the image has been viewed: { imagename: joe,  
> > imageviews: 26, lastviewed: 20010-11-26T10:21:34 }.
> > 
> > You need to display the documents side by side - the image name and  
> > url along with the total number of views. You have multiple documents  
> > and you choose to sort by the creationdate instead of name. Needless  
> > to say, if both documents were sorted by the shared field of imagename  
> > we wouldn't be discussing this.
> > 
> > So the issue is how do you ensure that the results from the query on  
> > the second document of image views is ordered in the same way as the  
> > results from the query on the first document?
> > 
> > I hope this is clear. Apologies if it isn't. Let me know and I will  
> > try to explain myself better.
> > 
> > On Fri, Nov 26, 2010 at 8:33 PM, Shay Banon  
> > [shay.banon@elasticsearch.com](mailto:shay.banon@elasticsearch.com) wrote:
> > 
> > > Not sure I followed exactly what you are after. Are those two different  
> > > search queries, but both sorted by date? How does the first query affect  
> > > the  
> > > second query?
> > > 
> > > On Fri, Nov 26, 2010 at 4:15 PM, Ted Karmel [ted.karmel@gmail.com](mailto:ted.karmel@gmail.com)  
> > > wrote:
> > > 
> > > > Hi Elasticsearchers,
> > > > 
> > > > I was wondering if anyone encountered the issue above and what their  
> > > > experience was.
> > > > 
> > > > Basically, say you have two related searches for two types of  
> > > > documents. Perhaps, the documents are sharded using the new routing  
> > > > feature based on a document field value of the first search. Assume  
> > > > that both documents have date fields. But, you want to sort the first  
> > > > search based on the date field values of the first type of document.  
> > > > And you will display the second search results side by side with the  
> > > > first search results. But, to maintain some order, you want to sort  
> > > > the second search results based on the date field values of the first  
> > > > type of documents. Otherwise, you may have search results displayed  
> > > > side by side that are not related.
> > > > 
> > > > In other words, is there a sorting path akin to routing path in  
> > > > Elasticsearch? Or does this have to be done externally?
> > > > 
> > > > Cheers,  
> > > > Ted

---

<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, 4:16am UTC](https://discuss.elastic.co/t/sorting-second-search-based-on-sort-of-first-search-sorting-path-feature/3602/6 "2017-07-06T04:16:04Z")

</div>


