# Is there a way to sort by the existance of a specific field?

**URL:** <https://discuss.elastic.co/t/is-there-a-way-to-sort-by-the-existance-of-a-specific-field/35943>\
**Category:** Elasticsearch\
**Created:** [November 30, 2015, 7:53pm UTC](https://discuss.elastic.co/t/is-there-a-way-to-sort-by-the-existance-of-a-specific-field/35943 "2015-11-30T19:53:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gdeconto](https://avatars.discourse-cdn.com/v4/letter/g/cab0a1/32.png) [@gdeconto](https://discuss.elastic.co/u/gdeconto)\
**Post date:** [November 30, 2015, 7:53pm UTC](https://discuss.elastic.co/t/is-there-a-way-to-sort-by-the-existance-of-a-specific-field/35943/1 "2015-11-30T19:53:51Z")

</div>

say I have a field that may or may not exist for a given document (ie if there is no data, the field would not get created during data population)

is there a fast way to sort by the existence of that field (basically a boolean sort)? ie all document where that field exist are at the top of the results, followed by all documents where the field doesnt exist?

If the field were a string, know that I could sort by the content of that field, but a string sort would, I imagine, be slower than just checking to see if the field exists.

if the field was a node that might contain other fields (which may or may not exist), we could sort by the content of a subfield, but the subfields too might or might not exist.

the last resort would probably be to add a boolean field to the schema that would indicate whether or not data for that field exists when the document is populated.

any thoughts appreciated

---

<div class="post-metadata">

**Author:** ![gdeconto](https://avatars.discourse-cdn.com/v4/letter/g/cab0a1/32.png) [@gdeconto](https://discuss.elastic.co/u/gdeconto)\
**Post date:** [December 1, 2015, 5:35pm UTC](https://discuss.elastic.co/t/is-there-a-way-to-sort-by-the-existance-of-a-specific-field/35943/2 "2015-12-01T17:35:14Z")

</div>

note that I found one way to do this:

"sort": [  
{  
"node1.node1a.field1": {"missing" : "\_last"}  
}  
]

---

<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 5, 2017, 11:34pm UTC](https://discuss.elastic.co/t/is-there-a-way-to-sort-by-the-existance-of-a-specific-field/35943/3 "2017-07-05T23:34:22Z")

</div>


