# Sorting on a Field that does not exist

**URL:** <https://discuss.elastic.co/t/sorting-on-a-field-that-does-not-exist/5820>\
**Category:** Elasticsearch\
**Created:** [November 8, 2011, 9:38pm UTC](https://discuss.elastic.co/t/sorting-on-a-field-that-does-not-exist/5820 "2011-11-08T21:38:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![LORDs\_diakonos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lords_diakonos/32/3009_2.png) [@LORDs\_diakonos](https://discuss.elastic.co/u/LORDs_diakonos)\
**Post date:** [November 8, 2011, 9:38pm UTC](https://discuss.elastic.co/t/sorting-on-a-field-that-does-not-exist/5820/1 "2011-11-08T21:38:32Z")

</div>

Sometimes I need to sort on a field that doesn't exist. I of course  
do not know it doesn't exist until runtime.

I tried the following

FieldSortBuilder sb = new  
FieldSortBuilder(sortbyArr[0].toLowerCase());  
sb.missing(null);  
sb.order(sortbyArr.length\>1 &&  
sortbyArr[1].equalsIgnoreCase("desc") ?  
SortOrder.DESC : SortOrder.ASC);  
srb.addSort(sb);

I have sett the midding() to other values but it doesn't work because  
my field is a String and ElasticSearch complains.  
srb is the SearchRequestBuilder

We are replacing Lucene with ElasticSearch. In Lucene this worked for  
us as we would construct the SortField Object and add that to the  
query.

How can I make this work?

---

<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 9, 2011, 11:16am UTC](https://discuss.elastic.co/t/sorting-on-a-field-that-does-not-exist/5820/2 "2011-11-09T11:16:41Z")

</div>

Sorting on a field that does not exists (in the mappings) will fail,  
because elasticsearch needs to use the relevant sorting implementation  
(numerics, string, ...).

On Tue, Nov 8, 2011 at 11:38 PM, LORDs\_diakonos [jasontesser@gmail.com](mailto:jasontesser@gmail.com)wrote:

> Sometimes I need to sort on a field that doesn't exist. I of course  
> do not know it doesn't exist until runtime.
> 
> I tried the following
> 
> FieldSortBuilder sb = new  
> FieldSortBuilder(sortbyArr[0].toLowerCase());  
> sb.missing(null);  
> sb.order(sortbyArr.length\>1 &&  
> sortbyArr[1].equalsIgnoreCase("desc") ?  
> SortOrder.DESC : SortOrder.ASC);  
> srb.addSort(sb);
> 
> I have sett the midding() to other values but it doesn't work because  
> my field is a String and Elasticsearch complains.  
> srb is the SearchRequestBuilder
> 
> We are replacing Lucene with Elasticsearch. In Lucene this worked for  
> us as we would construct the SortField Object and add that to the  
> query.
> 
> How can I make this work?

---

<div class="post-metadata">

**Author:** ![LORDs\_diakonos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lords_diakonos/32/3009_2.png) [@LORDs\_diakonos](https://discuss.elastic.co/u/LORDs_diakonos)\
**Post date:** [November 9, 2011, 11:50am UTC](https://discuss.elastic.co/t/sorting-on-a-field-that-does-not-exist/5820/3 "2011-11-09T11:50:43Z")

</div>

so 2 questions then

1. Is there a problem with having tons and tons of fields cause I would  
have to generate thousands or 10s of thousands of fields and set many to  
null ro fix my issue.

2. Is there a speedy way to check for fields that exist so I know what to  
sort on?

Thanks,  
Jason Tesser  
dotCMS Director, Support Services  
1-305-858-1422

On Wed, Nov 9, 2011 at 6:16 AM, Shay Banon [kimchy@gmail.com](mailto:kimchy@gmail.com) wrote:

> Sorting on a field that does not exists (in the mappings) will fail,  
> because elasticsearch needs to use the relevant sorting implementation  
> (numerics, string, ...).
> 
> On Tue, Nov 8, 2011 at 11:38 PM, LORDs\_diakonos [jasontesser@gmail.com](mailto:jasontesser@gmail.com)wrote:
> 
> > Sometimes I need to sort on a field that doesn't exist. I of course  
> > do not know it doesn't exist until runtime.
> > 
> > I tried the following
> > 
> > FieldSortBuilder sb = new  
> > FieldSortBuilder(sortbyArr[0].toLowerCase());  
> > sb.missing(null);  
> > sb.order(sortbyArr.length\>1 &&  
> > sortbyArr[1].equalsIgnoreCase("desc") ?  
> > SortOrder.DESC : SortOrder.ASC);  
> > srb.addSort(sb);
> > 
> > I have sett the midding() to other values but it doesn't work because  
> > my field is a String and Elasticsearch complains.  
> > srb is the SearchRequestBuilder
> > 
> > We are replacing Lucene with Elasticsearch. In Lucene this worked for  
> > us as we would construct the SortField Object and add that to the  
> > query.
> > 
> > How can I make this work?

---

<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 13, 2011, 6:59am UTC](https://discuss.elastic.co/t/sorting-on-a-field-that-does-not-exist/5820/4 "2011-11-13T06:59:16Z")

</div>

Not sure I understand..., are the fields supposed to be in this type or  
not? You can use get mapping for a specific index/type, and see which  
fields are currently mapped in the index.

On Wed, Nov 9, 2011 at 1:50 PM, Jason Tesser [jasontesser@gmail.com](mailto:jasontesser@gmail.com) wrote:

> so 2 questions then
> 
> 1. Is there a problem with having tons and tons of fields cause I would  
> have to generate thousands or 10s of thousands of fields and set many to  
> null ro fix my issue.
> 
> 2. Is there a speedy way to check for fields that exist so I know what to  
> sort on?
> 
> Thanks,  
> Jason Tesser  
> dotCMS Director, Support Services  
> 1-305-858-1422
> 
> On Wed, Nov 9, 2011 at 6:16 AM, Shay Banon [kimchy@gmail.com](mailto:kimchy@gmail.com) wrote:
> 
> > Sorting on a field that does not exists (in the mappings) will fail,  
> > because elasticsearch needs to use the relevant sorting implementation  
> > (numerics, string, ...).
> > 
> > On Tue, Nov 8, 2011 at 11:38 PM, LORDs\_diakonos [jasontesser@gmail.com](mailto:jasontesser@gmail.com)wrote:
> > 
> > > Sometimes I need to sort on a field that doesn't exist. I of course  
> > > do not know it doesn't exist until runtime.
> > > 
> > > I tried the following
> > > 
> > > FieldSortBuilder sb = new  
> > > FieldSortBuilder(sortbyArr[0].toLowerCase());  
> > > sb.missing(null);  
> > > sb.order(sortbyArr.length\>1 &&  
> > > sortbyArr[1].equalsIgnoreCase("desc") ?  
> > > SortOrder.DESC : SortOrder.ASC);  
> > > srb.addSort(sb);
> > > 
> > > I have sett the midding() to other values but it doesn't work because  
> > > my field is a String and Elasticsearch complains.  
> > > srb is the SearchRequestBuilder
> > > 
> > > We are replacing Lucene with Elasticsearch. In Lucene this worked for  
> > > us as we would construct the SortField Object and add that to the  
> > > query.
> > > 
> > > How can I make this work?

---

<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, 3:49am UTC](https://discuss.elastic.co/t/sorting-on-a-field-that-does-not-exist/5820/5 "2017-07-06T03:49:00Z")

</div>


