# Sort a prefix query in compound queries in java

**URL:** <https://discuss.elastic.co/t/sort-a-prefix-query-in-compound-queries-in-java/94138>\
**Category:** Elasticsearch\
**Created:** [July 21, 2017, 5:08pm UTC](https://discuss.elastic.co/t/sort-a-prefix-query-in-compound-queries-in-java/94138 "2017-07-21T17:08:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jth1](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@jth1](https://discuss.elastic.co/u/jth1)\
**Post date:** [July 21, 2017, 5:08pm UTC](https://discuss.elastic.co/t/sort-a-prefix-query-in-compound-queries-in-java/94138/1 "2017-07-21T17:08:46Z")

</div>

prefix query, returns results with constant score. How to sort results alphabetically by a field in ascending in JAVA?

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [July 23, 2017, 7:58pm UTC](https://discuss.elastic.co/t/sort-a-prefix-query-in-compound-queries-in-java/94138/2 "2017-07-23T19:58:22Z")

</div>

I would have a non-analyzed multi-field [1] and then sort [2] on that field.

{  
"mappings": {  
"my\_type": {  
"properties": {  
"my\_field": {  
"type": "text",  
"fields": {  
"raw": {  
"type": "keyword"  
}  
}  
}  
}  
}  
}  
}

{  
"sort": [  
{  
"my\_field.raw": {  
"order": "asc"  
}  
}  
],  
"query": {  
...  
}  
}

[1]  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-fields.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-fields.html)  
[2]  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html)

---

<div class="post-metadata">

**Author:** ![jth1](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@jth1](https://discuss.elastic.co/u/jth1)\
**Post date:** [July 24, 2017, 4:03am UTC](https://discuss.elastic.co/t/sort-a-prefix-query-in-compound-queries-in-java/94138/3 "2017-07-24T04:03:21Z")

</div>

Thanks much for reply. I am using elasticsearch Java API. Document has multiple fields like symbol, name and description. Document should be picked if search term matches any of these 3 fields, even partial match like prefix/match. eg: if search term is "Bp" some of the documents picked by symbol partial matches like "Bp123", "Bp1"," Bp150". They all have same \_score. How to sort them? I am using a compound query DismaxQueryBuilder to build query adding queries match by NAME, Match By Symbol, Match by DESCRIPTION. Relevance returned by Elasticsearch by \_score is good. Only when scores are same, it would be good to have results in sorted order. How can I achieve this in java api.

---

<div class="post-metadata">

**Author:** ![jth1](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@jth1](https://discuss.elastic.co/u/jth1)\
**Post date:** [July 24, 2017, 4:12am UTC](https://discuss.elastic.co/t/sort-a-prefix-query-in-compound-queries-in-java/94138/4 "2017-07-24T04:12:22Z")

</div>

so as per reply, if we add sort to raw field in mappings at index time, will search results be returned in that order if we query that raw field.

---

<div class="post-metadata">

**Author:** ![jth1](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@jth1](https://discuss.elastic.co/u/jth1)\
**Post date:** [July 24, 2017, 3:14pm UTC](https://discuss.elastic.co/t/sort-a-prefix-query-in-compound-queries-in-java/94138/5 "2017-07-24T15:14:39Z")

</div>

I don't want to change the relevance returned based on scores, but for one of the queries like prefix in the dismaxquerybuilder the \_score are constant, so I want to sort only those results using any of the fields. eg: like "Bp123", "Bp1"," Bp150" with prefix query the scores are constant. I want the results like Bp1, Bp123, Bp150 . If I add SORT by field in the search request builder where the query set is dismaxquery of mutiple fields, all the documents are sorted by that field and relevance is gone. I want to sort those documents only whose scores are equal not all the documents.

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [July 25, 2017, 7:46pm UTC](https://discuss.elastic.co/t/sort-a-prefix-query-in-compound-queries-in-java/94138/6 "2017-07-25T19:46:24Z")

</div>

You can define a secondary sort if the first sort returns exact matches.

{  
"sort": [  
{  
"\_score",  
"my\_field.raw": {  
"order": "asc"  
}  
}  
],  
"query": {  
...  
}  
}

You do not query the raw field, only use it to sort by. The above sort will  
still score the documents and order them by score (descending), but you  
order items by the non analyzed field name if the score is identical.

---

<div class="post-metadata">

**Author:** ![jth1](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@jth1](https://discuss.elastic.co/u/jth1)\
**Post date:** [July 26, 2017, 1:47am UTC](https://discuss.elastic.co/t/sort-a-prefix-query-in-compound-queries-in-java/94138/7 "2017-07-26T01:47:57Z")

</div>

Thanks Ivan. I am new to elasticsearch. I am using DismaxQueryBuilder to get union of all the documents matched by matchQuery by SYMBOL, matchQuery by NAME, matchQuery by DESCRIPTION, prefixQuery by SYMBOL, prefixQuery by NAME and prefixQuery by DESCRIPTION. Documents picked by matchQuery has relevance by score, exactly what I wanted. But documents picked by prefix query have no relevance, \_scores are same, so I wanted those documents to be sorted by SYMBOL. Eg; the symbols in the documents are BP5, BP1, BP9, when I searched for BP all the three documents are picked by prefix query with no relevance which I wanted to be sorted like BP1, BP5, BP9 plus some other documents by matchQuery by other fields with relevance as expected and need not to be sorted.

---

<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:** [August 23, 2017, 1:48am UTC](https://discuss.elastic.co/t/sort-a-prefix-query-in-compound-queries-in-java/94138/8 "2017-08-23T01:48:02Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
