# Sorting on a boolean field

**URL:** <https://discuss.elastic.co/t/sorting-on-a-boolean-field/21491>\
**Category:** Elasticsearch\
**Created:** [January 5, 2015, 10:09pm UTC](https://discuss.elastic.co/t/sorting-on-a-boolean-field/21491 "2015-01-05T22:09:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mbaker](https://avatars.discourse-cdn.com/v4/letter/m/ecb155/32.png) [@mbaker](https://discuss.elastic.co/u/mbaker)\
**Post date:** [January 5, 2015, 10:09pm UTC](https://discuss.elastic.co/t/sorting-on-a-boolean-field/21491/1 "2015-01-05T22:09:10Z")

</div>

I would like to sort the results of a query such that all of the records  
with a value of true in the expires field will be at the top. I am not  
using relevance for sorting. I tried adding this as a sort, but it doesn't  
seem to work with either sorting order: { expires: { order: "desc" } }

All of the records are in the result but they aren't sorted. Is there a  
different way I should be going about this?

For reference, here is my entire query:

{  
"query": {  
"match\_all": {  
}  
},  
"size": 50,  
"from": 0,  
"fields": [  
"id"  
],  
"sort": [  
{  
"expires": {  
"order": "desc"  
}  
}  
]  
}

--  
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/0bfc5579-8855-44a8-97e3-58ede3bb10d4%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/0bfc5579-8855-44a8-97e3-58ede3bb10d4%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 6, 2015, 6:34am UTC](https://discuss.elastic.co/t/sorting-on-a-boolean-field/21491/2 "2015-01-06T06:34:44Z")

</div>

I think sorting on boolean should work.

If you can't make it working, you could also use function\_score to "boost" expires:true vs expires:false.

HTH

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

> Le 5 janv. 2015 à 23:09, [mbaker@trunkclub.com](mailto:mbaker@trunkclub.com) a écrit :
> 
> I would like to sort the results of a query such that all of the records with a value of true in the expires field will be at the top. I am not using relevance for sorting. I tried adding this as a sort, but it doesn't seem to work with either sorting order: { expires: { order: "desc" } }
> 
> All of the records are in the result but they aren't sorted. Is there a different way I should be going about this?
> 
> For reference, here is my entire query:
> 
> ## { "query": { "match\_all": { } }, "size": 50, "from": 0, "fields": ["id"], "sort": [{ "expires": { "order": "desc" } }] }
> 
> 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/0bfc5579-8855-44a8-97e3-58ede3bb10d4%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/0bfc5579-8855-44a8-97e3-58ede3bb10d4%40googlegroups.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/BB982DA5-E7BF-4E23-AC76-CDC2C10CDE7B%40pilato.fr](https://groups.google.com/d/msgid/elasticsearch/BB982DA5-E7BF-4E23-AC76-CDC2C10CDE7B%40pilato.fr).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Joao\_Nelas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joao_nelas/32/1014_2.png) [@Joao\_Nelas](https://discuss.elastic.co/u/Joao_Nelas)\
**Post date:** [January 11, 2015, 4:40pm UTC](https://discuss.elastic.co/t/sorting-on-a-boolean-field/21491/3 "2015-01-11T16:40:40Z")

</div>

No, I don't think sorting on booleans works. At least I've had no luck with  
it.

On Tuesday, January 6, 2015 at 6:34:52 AM UTC, David Pilato wrote:

> I think sorting on boolean should work.
> 
> If you can't make it working, you could also use function\_score to "boost"  
> expires:true vs expires:false.
> 
> HTH
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 5 janv. 2015 à 23:09, [mba...@trunkclub.com](mailto:mba...@trunkclub.com) \<javascript:\> a écrit :
> 
> I would like to sort the results of a query such that all of the records  
> with a value of true in the expires field will be at the top. I am not  
> using relevance for sorting. I tried adding this as a sort, but it doesn't  
> seem to work with either sorting order: { expires: { order: "desc" } }
> 
> All of the records are in the result but they aren't sorted. Is there a  
> different way I should be going about this?
> 
> For reference, here is my entire query:
> 
> {  
> "query": {  
> "match\_all": {  
> }  
> },  
> "size": 50,  
> "from": 0,  
> "fields": [  
> "id"  
> ],  
> "sort": [  
> {  
> "expires": {  
> "order": "desc"  
> }  
> }  
> ]  
> }
> 
> --  
> 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/0bfc5579-8855-44a8-97e3-58ede3bb10d4%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/0bfc5579-8855-44a8-97e3-58ede3bb10d4%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/0bfc5579-8855-44a8-97e3-58ede3bb10d4%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/0bfc5579-8855-44a8-97e3-58ede3bb10d4%40googlegroups.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/68ead506-a232-43ba-9f16-b5d933132de7%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/68ead506-a232-43ba-9f16-b5d933132de7%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 12, 2015, 8:44am UTC](https://discuss.elastic.co/t/sorting-on-a-boolean-field/21491/4 "2015-01-12T08:44:19Z")

</div>

I just tried this with elasticsearch 1.4.2:

DELETE test  
PUT test/doc/1  
{  
"foo": true  
}  
PUT test/doc/2  
{  
"foo": false  
}

# Gives doc 2 first

GET test/\_search  
{  
"sort": [  
{  
"foo": {  
"order": "asc"  
}  
}  
]  
}

# Gives doc 1 first

GET test/\_search  
{  
"sort": [  
{  
"foo": {  
"order": "desc"  
}  
}  
]  
}

--  
David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
@dadoonet [https://twitter.com/dadoonet](https://twitter.com/dadoonet) | @elasticsearchfr [https://twitter.com/elasticsearchfr](https://twitter.com/elasticsearchfr) | @scrutmydocs [https://twitter.com/scrutmydocs](https://twitter.com/scrutmydocs)

> Le 11 janv. 2015 à 17:40, Joao Nelas [joao.nelas@gmail.com](mailto:joao.nelas@gmail.com) a écrit :
> 
> No, I don't think sorting on booleans works. At least I've had no luck with it.
> 
> On Tuesday, January 6, 2015 at 6:34:52 AM UTC, David Pilato wrote:  
> I think sorting on boolean should work.
> 
> If you can't make it working, you could also use function\_score to "boost" expires:true vs expires:false.
> 
> HTH
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 5 janv. 2015 à 23:09, [mba...@trunkclub.com](mailto:mba...@trunkclub.com) \<javascript:\> a écrit :
> 
> > I would like to sort the results of a query such that all of the records with a value of true in the expires field will be at the top. I am not using relevance for sorting. I tried adding this as a sort, but it doesn't seem to work with either sorting order: { expires: { order: "desc" } }
> > 
> > All of the records are in the result but they aren't sorted. Is there a different way I should be going about this?
> > 
> > For reference, here is my entire query:
> > 
> > {  
> > "query": {  
> > "match\_all": {  
> > }  
> > },  
> > "size": 50,  
> > "from": 0,  
> > "fields": [  
> > "id"  
> > ],  
> > "sort": [  
> > {  
> > "expires": {  
> > "order": "desc"  
> > }  
> > }  
> > ]  
> > }
> > 
> > --  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/0bfc5579-8855-44a8-97e3-58ede3bb10d4%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/0bfc5579-8855-44a8-97e3-58ede3bb10d4%40googlegroups.com) [https://groups.google.com/d/msgid/elasticsearch/0bfc5579-8855-44a8-97e3-58ede3bb10d4%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/0bfc5579-8855-44a8-97e3-58ede3bb10d4%40googlegroups.com?utm_medium=email&utm_source=footer).  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout) [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) [mailto:elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/68ead506-a232-43ba-9f16-b5d933132de7%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/68ead506-a232-43ba-9f16-b5d933132de7%40googlegroups.com) [https://groups.google.com/d/msgid/elasticsearch/68ead506-a232-43ba-9f16-b5d933132de7%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/68ead506-a232-43ba-9f16-b5d933132de7%40googlegroups.com?utm_medium=email&utm_source=footer).  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout) [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/998FB640-5163-4426-8911-5FB705B3D857%40pilato.fr](https://groups.google.com/d/msgid/elasticsearch/998FB640-5163-4426-8911-5FB705B3D857%40pilato.fr).  
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, 12:39am UTC](https://discuss.elastic.co/t/sorting-on-a-boolean-field/21491/5 "2017-07-06T00:39:36Z")

</div>


