# Sort same scored documents by field

**URL:** <https://discuss.elastic.co/t/sort-same-scored-documents-by-field/14119>\
**Category:** Elasticsearch\
**Created:** [October 25, 2013, 6:32pm UTC](https://discuss.elastic.co/t/sort-same-scored-documents-by-field/14119 "2013-10-25T18:32:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_Arkin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matt_arkin/32/2011_2.png) [@Matt\_Arkin](https://discuss.elastic.co/u/Matt_Arkin)\
**Post date:** [October 25, 2013, 6:32pm UTC](https://discuss.elastic.co/t/sort-same-scored-documents-by-field/14119/1 "2013-10-25T18:32:08Z")

</div>

I have a bunch of documents across several indexes that might have a field  
(tags), that all have timestamps, and that all have the same id.  
I want to search them so that if they have tags (so if tags exists), that  
they have a higher score, and then sort all the documents by timestamp.

I noticed if I add a sort by timestamp descending to my query though, that  
the order is based purely on timestamp and not on whether the tags exist.

So for instance I have this query:

```
   query: {
        custom_filters_score: {
          query: {
            bool: {
              must: [
                {
                  text: {
                    _id: title
                  }
                }
              ],
              must_not: [],
              should: []
            }
          },
          filters: [
            {
              filter: {
                exists: {
                  field: "tags"
                }
              },
              boost: "2"
            }
          ],
          score_mode: "first"
        }
      }
}

```

this returns a bunch of documents with either score 1 (if it doesn't have tags) or score 2 (if it does).

So can I sort them by score and then by timestamp?

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [October 27, 2013, 8:55pm UTC](https://discuss.elastic.co/t/sort-same-scored-documents-by-field/14119/2 "2013-10-27T20:55:29Z")

</div>

Hi,

You can ask Elasticsearch to sort by \_score and then timestamp, for example:

"sort" : [  
"\_score",  
{ "timestamp" : {"order" : "desc" } }  
]

On Fri, Oct 25, 2013 at 8:32 PM, Matt Arkin [arkin@endlessm.com](mailto:arkin@endlessm.com) wrote:

> I have a bunch of documents across several indexes that might have a field  
> (tags), that all have timestamps, and that all have the same id.  
> I want to search them so that if they have tags (so if tags exists), that  
> they have a higher score, and then sort all the documents by timestamp.
> 
> I noticed if I add a sort by timestamp descending to my query though, that  
> the order is based purely on timestamp and not on whether the tags exist.
> 
> So for instance I have this query:
> 
> ```
> query: {
> custom_filters_score: {
> query: {
> bool: {
> must: [
> {
> text: {
> _id: title
> }
> }
> ],
> must_not: [],
> should: []
> }
> },
> filters: [
> {
> filter: {
> exists: {
> field: "tags"
> }
> },
> boost: "2"
> }
> ],
> score_mode: "first"
> }
> }
> }
> 
> ```
> 
> this returns a bunch of documents with either score 1 (if it doesn't have tags) or score 2 (if it does).
> 
> So can I sort them by score and then by timestamp?
> 
> --  
> 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).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Matt\_Arkin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matt_arkin/32/2011_2.png) [@Matt\_Arkin](https://discuss.elastic.co/u/Matt_Arkin)\
**Post date:** [October 29, 2013, 10:32pm UTC](https://discuss.elastic.co/t/sort-same-scored-documents-by-field/14119/3 "2013-10-29T22:32:59Z")

</div>

Well that was easy. Thanks 🙂

On Sun, Oct 27, 2013 at 1:55 PM, Adrien Grand \<  
[adrien.grand@elasticsearch.com](mailto:adrien.grand@elasticsearch.com)\> wrote:

> Hi,
> 
> You can ask Elasticsearch to sort by \_score and then timestamp, for  
> example:
> 
> "sort" : [
> 
> ```
> "_score",
> { "timestamp" : {"order" : "desc" } }
> 
> ]
> 
> ```
> 
> On Fri, Oct 25, 2013 at 8:32 PM, Matt Arkin [arkin@endlessm.com](mailto:arkin@endlessm.com) wrote:
> 
> > I have a bunch of documents across several indexes that might have a  
> > field (tags), that all have timestamps, and that all have the same id.  
> > I want to search them so that if they have tags (so if tags exists), that  
> > they have a higher score, and then sort all the documents by timestamp.
> > 
> > I noticed if I add a sort by timestamp descending to my query though,  
> > that the order is based purely on timestamp and not on whether the tags  
> > exist.
> > 
> > So for instance I have this query:
> > 
> > ```
> > query: {
> > custom_filters_score: {
> > query: {
> > bool: {
> > must: [
> > {
> > text: {
> > _id: title
> > }
> > }
> > ],
> > must_not: [],
> > should: []
> > }
> > },
> > filters: [
> > {
> > filter: {
> > exists: {
> > field: "tags"
> > }
> > },
> > boost: "2"
> > }
> > ],
> > score_mode: "first"
> > }
> > }
> > }
> > 
> > ```
> > 
> > this returns a bunch of documents with either score 1 (if it doesn't have tags) or score 2 (if it does).
> > 
> > So can I sort them by score and then by timestamp?
> > 
> > --  
> > 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).  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> 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/PZWiwbBEHL0/unsubscribe](https://groups.google.com/d/topic/elasticsearch/PZWiwbBEHL0/unsubscribe).  
> To unsubscribe from this group and all its topics, send an email to  
> [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:09am UTC](https://discuss.elastic.co/t/sort-same-scored-documents-by-field/14119/4 "2017-07-06T02:09:56Z")

</div>


