# Problem sorting a field descendingly with SearchRequestBuilder

**URL:** <https://discuss.elastic.co/t/problem-sorting-a-field-descendingly-with-searchrequestbuilder/14144>\
**Category:** Elasticsearch\
**Created:** [October 28, 2013, 6:44pm UTC](https://discuss.elastic.co/t/problem-sorting-a-field-descendingly-with-searchrequestbuilder/14144 "2013-10-28T18:44:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joao\_Felix](https://avatars.discourse-cdn.com/v4/letter/j/f9ae1b/32.png) [@Joao\_Felix](https://discuss.elastic.co/u/Joao_Felix)\
**Post date:** [October 28, 2013, 6:44pm UTC](https://discuss.elastic.co/t/problem-sorting-a-field-descendingly-with-searchrequestbuilder/14144/1 "2013-10-28T18:44:46Z")

</div>

Hi, I am having a problem using the SearchRequestBuilder to sort a  
field descendingly.

I have some documents mapped with:

{  
"network" : {  
"properties" : {  
"n\_cpes": {  
"type": "multi\_field",  
"fields": {  
"n\_cpes": {  
"type": "long",  
"index" : "analyzed"  
},  
"raw": {  
"type": "long",  
"index": "not\_analyzed"  
}  
}  
},  
"n\_accesses": {  
"type": "multi\_field",  
"fields": {  
"n\_accesses": {  
"type": "long",  
"index" : "analyzed"  
},  
"raw": {  
"type": "long",  
"index": "not\_analyzed"  
}  
}  
}  
}  
}  
}

I have no problems sorting the n\_accesses field but when I try to order the  
n\_cpes, ES returns an unordered list!

The thing is, when I curl ES to sort the n\_cpes field with a JSON similar  
to the one built with SearchRequestBuilder the result is the expected one.

Here is how I use the SearchRequestBuilder (the query is built client-side  
and so i use the extraSource method add the query):

SearchRequestBuilder requestBuilder = client.prepareSearch(indexes.toArray(  
new String[indexes.size()]))  
.setTypes(new String[]{type})  
.setFrom(from)  
.setSize(size);

if (sortField != null) {  
requestBuilder.addSort(prepareSorter(sortField, sortAscending));  
}

if (jsFilter != null && jsFilter.length() \> 0) {  
requestBuilder.setFilter(prepareFilterExpression(jsFilter));  
}

try {  
requestBuilder.setExtraSource(query.toString().getBytes("UTF-8"));

} catch (UnsupportedEncodingException e) {  
throw new IllegalStateException("We cannot continue without UTF-8", e);  
}

SearchResponse response = requestBuilder.execute().actionGet();

return new JSONObject(response.toString());

The prepareSorter method looks like this:

SortOrder order = ascending ? SortOrder.ASC : SortOrder.DESC;

return new FieldSortBuilder(field + ".raw")  
.order(order)  
.ignoreUnmapped(true);

Am I doing something wrong?

I would appreciate some help!

Thanks!

João Félix

--  
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:10am UTC](https://discuss.elastic.co/t/problem-sorting-a-field-descendingly-with-searchrequestbuilder/14144/2 "2017-07-06T02:10:09Z")

</div>


