# Sort search results based on query parameters

**URL:** <https://discuss.elastic.co/t/sort-search-results-based-on-query-parameters/23120>\
**Category:** Elasticsearch\
**Created:** [April 7, 2015, 6:03am UTC](https://discuss.elastic.co/t/sort-search-results-based-on-query-parameters/23120 "2015-04-07T06:03:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ravi063](https://avatars.discourse-cdn.com/v4/letter/r/df705f/32.png) [@ravi063](https://discuss.elastic.co/u/ravi063)\
**Post date:** [April 7, 2015, 6:03am UTC](https://discuss.elastic.co/t/sort-search-results-based-on-query-parameters/23120/1 "2015-04-07T06:03:13Z")

</div>

Hello,

I am trying to search for some keywords (eg java, hibernate, j2ee, jSP etc)  
in two text fields [summary and description]. Since I need to get records  
with any of the key words, I am using \*BooleanQuery \*  
with _should_ clause. And to search, I am using _Multi-Match query_. Below  
is my query,

The query is working fine, but I want to get records with keyword _java_  
first and then get records with other keywords. How can I achieve this?

{  
"query": {  
"bool": {  
"should": [  
{  
"multi\_match": {  
"query": "java",  
"fields": [  
"summary",  
"description"  
],  
"type": "phrase"  
}  
},  
{  
"multi\_match": {  
"query": "hibernate",  
"fields": [  
"summary",  
"description"  
],  
"type": "phrase"  
}  
},  
...........  
]  
}  
}  
}

Thanks

--  
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/1661b6b3-c1f5-4500-83df-f145e994640e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1661b6b3-c1f5-4500-83df-f145e994640e%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:** [April 7, 2015, 6:46am UTC](https://discuss.elastic.co/t/sort-search-results-based-on-query-parameters/23120/2 "2015-04-07T06:46:47Z")

</div>

You mean that Java is a mandatory term?  
In that case use a must clause instead of should.

David

> Le 7 avr. 2015 à 08:03, [ravi063@gmail.com](mailto:ravi063@gmail.com) a écrit :
> 
> Hello,
> 
> I am trying to search for some keywords (eg java, hibernate, j2ee, jSP etc) in two text fields [summary and description]. Since I need to get records with any of the key words, I am using BooleanQuery  
> with should clause. And to search, I am using Multi-Match query. Below is my query,
> 
> The query is working fine, but I want to get records with keyword java first and then get records with other keywords. How can I achieve this?
> 
> {  
> "query": {  
> "bool": {  
> "should": [  
> {  
> "multi\_match": {  
> "query": "java",  
> "fields": [  
> "summary",  
> "description"  
> ],  
> "type": "phrase"  
> }  
> },  
> {  
> "multi\_match": {  
> "query": "hibernate",  
> "fields": [  
> "summary",  
> "description"  
> ],  
> "type": "phrase"  
> }  
> },  
> ...........  
> ]  
> }  
> }  
> }
> 
> ## Thanks
> 
> 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/1661b6b3-c1f5-4500-83df-f145e994640e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1661b6b3-c1f5-4500-83df-f145e994640e%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/5F8943FD-4CE5-4CA6-8DE6-1A60434A4B81%40pilato.fr](https://groups.google.com/d/msgid/elasticsearch/5F8943FD-4CE5-4CA6-8DE6-1A60434A4B81%40pilato.fr).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![ravi063](https://avatars.discourse-cdn.com/v4/letter/r/df705f/32.png) [@ravi063](https://discuss.elastic.co/u/ravi063)\
**Post date:** [April 7, 2015, 7:21am UTC](https://discuss.elastic.co/t/sort-search-results-based-on-query-parameters/23120/3 "2015-04-07T07:21:32Z")

</div>

Hi David,

Thanks for your reply. Java is not mandatory term. I want Elasticsearch to  
return records having term Java first(if such records exists) followed by  
records with other terms. Basically, I want to search for 2-3 keywords and  
I should be able to sort results based on one of the search terms (java in  
this case)

On Tuesday, April 7, 2015 at 12:17:01 PM UTC+5:30, David Pilato wrote:

> You mean that Java is a mandatory term?  
> In that case use a must clause instead of should.
> 
> David
> 
> Le 7 avr. 2015 à 08:03, [rav...@gmail.com](mailto:rav...@gmail.com) \<javascript:\> a écrit :
> 
> Hello,
> 
> I am trying to search for some keywords (eg java, hibernate, j2ee, jSP  
> etc) in two text fields [summary and description]. Since I need to get  
> records with any of the key words, I am using \*BooleanQuery \*  
> with _should_ clause. And to search, I am using _Multi-Match query_.  
> Below is my query,
> 
> The query is working fine, but I want to get records with keyword _java_  
> first and then get records with other keywords. How can I achieve this?
> 
> {  
> "query": {  
> "bool": {  
> "should": [  
> {  
> "multi\_match": {  
> "query": "java",  
> "fields": [  
> "summary",  
> "description"  
> ],  
> "type": "phrase"  
> }  
> },  
> {  
> "multi\_match": {  
> "query": "hibernate",  
> "fields": [  
> "summary",  
> "description"  
> ],  
> "type": "phrase"  
> }  
> },  
> ...........  
> ]  
> }  
> }  
> }
> 
> Thanks
> 
> --  
> 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/1661b6b3-c1f5-4500-83df-f145e994640e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1661b6b3-c1f5-4500-83df-f145e994640e%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/1661b6b3-c1f5-4500-83df-f145e994640e%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/1661b6b3-c1f5-4500-83df-f145e994640e%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/4ffb97aa-dd75-4a15-8330-04f377bee503%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4ffb97aa-dd75-4a15-8330-04f377bee503%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:** [April 7, 2015, 7:28am UTC](https://discuss.elastic.co/t/sort-search-results-based-on-query-parameters/23120/4 "2015-04-07T07:28:09Z")

</div>

I think you could wrap your query in a boosting query: [Boosting Query | Elasticsearch Guide [1.5] | Elastic](http://www.elastic.co/guide/en/elasticsearch/reference/1.5/query-dsl-boosting-query.html)

Would this work?

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

> Le 7 avr. 2015 à 09:21, [ravi063@gmail.com](mailto:ravi063@gmail.com) a écrit :
> 
> Hi David,
> 
> Thanks for your reply. Java is not mandatory term. I want Elasticsearch to return records having term Java first(if such records exists) followed by records with other terms. Basically, I want to search for 2-3 keywords and I should be able to sort results based on one of the search terms (java in this case)
> 
> > On Tuesday, April 7, 2015 at 12:17:01 PM UTC+5:30, David Pilato wrote:  
> > You mean that Java is a mandatory term?  
> > In that case use a must clause instead of should.
> > 
> > David
> > 
> > > Le 7 avr. 2015 à 08:03, [rav...@gmail.com](mailto:rav...@gmail.com) a écrit :
> > > 
> > > Hello,
> > > 
> > > I am trying to search for some keywords (eg java, hibernate, j2ee, jSP etc) in two text fields [summary and description]. Since I need to get records with any of the key words, I am using BooleanQuery  
> > > with should clause. And to search, I am using Multi-Match query. Below is my query,
> > > 
> > > The query is working fine, but I want to get records with keyword java first and then get records with other keywords. How can I achieve this?
> > > 
> > > {  
> > > "query": {  
> > > "bool": {  
> > > "should": [  
> > > {  
> > > "multi\_match": {  
> > > "query": "java",  
> > > "fields": [  
> > > "summary",  
> > > "description"  
> > > ],  
> > > "type": "phrase"  
> > > }  
> > > },  
> > > {  
> > > "multi\_match": {  
> > > "query": "hibernate",  
> > > "fields": [  
> > > "summary",  
> > > "description"  
> > > ],  
> > > "type": "phrase"  
> > > }  
> > > },  
> > > ...........  
> > > ]  
> > > }  
> > > }  
> > > }
> > > 
> > > ## Thanks
> > > 
> > > 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).  
> > > To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/1661b6b3-c1f5-4500-83df-f145e994640e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1661b6b3-c1f5-4500-83df-f145e994640e%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/4ffb97aa-dd75-4a15-8330-04f377bee503%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4ffb97aa-dd75-4a15-8330-04f377bee503%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/08CDDCAC-3AB5-4533-A067-912EE2D54410%40pilato.fr](https://groups.google.com/d/msgid/elasticsearch/08CDDCAC-3AB5-4533-A067-912EE2D54410%40pilato.fr).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![ravi063](https://avatars.discourse-cdn.com/v4/letter/r/df705f/32.png) [@ravi063](https://discuss.elastic.co/u/ravi063)\
**Post date:** [April 7, 2015, 5:43pm UTC](https://discuss.elastic.co/t/sort-search-results-based-on-query-parameters/23120/5 "2015-04-07T17:43:30Z")

</div>

Hi David,

Based on your inputs I tried two options:

1. _Boosting Query:_ As suggested by you I used Boosting query and it  
worked like charm. But, search hit count remained same even after adding  
more and more terms. I am bit confused by this behavior (is my query right  
?). Below is my modified query:  
{  
"query": {  
"boosting": {  
"_positive_": {  
"multi\_match": {  
"query": "java",  
"fields": [  
"summary",  
"description"  
],  
"type": "phrase"  
}  
},  
"_negative_": {  
"bool": {  
"should": [  
{  
"multi\_match": {  
"query": "jsp",  
"fields": [  
"summary",  
"description"  
],  
"type": "phrase"  
}  
},  
{  
"multi\_match": {  
"query": "servlets",  
"fields": [  
"summary",  
"description"  
],  
"type": "phrase"  
}  
}  
]  
}  
},  
"negative\_boost": 0.2  
}  
}  
}

2. Added \*boost \*parameter: I added boost parameter to my old query and got  
good results. I included boost parameter to only one search term(java).

I added boost parameter to one of the

On Tuesday, April 7, 2015 at 12:58:37 PM UTC+5:30, David Pilato wrote:

> I think you could wrap your query in a boosting query:  
> [Boosting Query | Elasticsearch Guide [1.5] | Elastic](http://www.elastic.co/guide/en/elasticsearch/reference/1.5/query-dsl-boosting-query.html)
> 
> Would this work?
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 7 avr. 2015 à 09:21, [rav...@gmail.com](mailto:rav...@gmail.com) \<javascript:\> a écrit :
> 
> Hi David,
> 
> Thanks for your reply. Java is not mandatory term. I want Elasticsearch to  
> return records having term Java first(if such records exists) followed by  
> records with other terms. Basically, I want to search for 2-3 keywords and  
> I should be able to sort results based on one of the search terms (java in  
> this case)
> 
> On Tuesday, April 7, 2015 at 12:17:01 PM UTC+5:30, David Pilato wrote:
> 
> > You mean that Java is a mandatory term?  
> > In that case use a must clause instead of should.
> > 
> > David
> > 
> > Le 7 avr. 2015 à 08:03, [rav...@gmail.com](mailto:rav...@gmail.com) a écrit :
> > 
> > Hello,
> > 
> > I am trying to search for some keywords (eg java, hibernate, j2ee, jSP  
> > etc) in two text fields [summary and description]. Since I need to get  
> > records with any of the key words, I am using \*BooleanQuery \*  
> > with _should_ clause. And to search, I am using _Multi-Match query_.  
> > Below is my query,
> > 
> > The query is working fine, but I want to get records with keyword _java_  
> > first and then get records with other keywords. How can I achieve this?
> > 
> > {  
> > "query": {  
> > "bool": {  
> > "should": [  
> > {  
> > "multi\_match": {  
> > "query": "java",  
> > "fields": [  
> > "summary",  
> > "description"  
> > ],  
> > "type": "phrase"  
> > }  
> > },  
> > {  
> > "multi\_match": {  
> > "query": "hibernate",  
> > "fields": [  
> > "summary",  
> > "description"  
> > ],  
> > "type": "phrase"  
> > }  
> > },  
> > ...........  
> > ]  
> > }  
> > }  
> > }
> > 
> > Thanks
> > 
> > --  
> > 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).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/1661b6b3-c1f5-4500-83df-f145e994640e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1661b6b3-c1f5-4500-83df-f145e994640e%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/1661b6b3-c1f5-4500-83df-f145e994640e%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/1661b6b3-c1f5-4500-83df-f145e994640e%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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/4ffb97aa-dd75-4a15-8330-04f377bee503%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4ffb97aa-dd75-4a15-8330-04f377bee503%40googlegroups.com)  
> > [https://groups.google.com/d/msgid/elasticsearch/4ffb97aa-dd75-4a15-8330-04f377bee503%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/4ffb97aa-dd75-4a15-8330-04f377bee503%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/b7aca4f4-24c3-4a5e-96de-c28db0c8894d%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/b7aca4f4-24c3-4a5e-96de-c28db0c8894d%40googlegroups.com).  
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:21am UTC](https://discuss.elastic.co/t/sort-search-results-based-on-query-parameters/23120/6 "2017-07-06T00:21:04Z")

</div>


