# Sort Doubt

**URL:** <https://discuss.elastic.co/t/sort-doubt/10817>\
**Category:** Elasticsearch\
**Created:** [February 20, 2013, 12:17pm UTC](https://discuss.elastic.co/t/sort-doubt/10817 "2013-02-20T12:17:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maria\_John\_Franklin](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@Maria\_John\_Franklin](https://discuss.elastic.co/u/Maria_John_Franklin)\
**Post date:** [February 20, 2013, 12:17pm UTC](https://discuss.elastic.co/t/sort-doubt/10817/1 "2013-02-20T12:17:05Z")

</div>

{  
"from" : 0, "size" :10,  
"fields" :["id","co","prd","pd","st","int","ab","cls","pt"],  
"query" : {"query\_string":  
{

"query": "((test) AND ( (test1) OR (test2))) OR (test)"}},  
"facets": {  
"tagid": {  
"terms": {  
"field": ["tagid"],"size": 100 }  
}  
}}

This Query is working fine..But it return value Order By rank desc..I Don't  
need like that.. I need the Result Order by "Or" condition Success...Is It  
Possible?

--  
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:** ![Maria\_John\_Franklin](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@Maria\_John\_Franklin](https://discuss.elastic.co/u/Maria_John_Franklin)\
**Post date:** [February 20, 2013, 12:21pm UTC](https://discuss.elastic.co/t/sort-doubt/10817/2 "2013-02-20T12:21:27Z")

</div>

On Wednesday, February 20, 2013 8:17:05 PM UTC+8, Maria John Franklin wrote:

> {  
> "from" : 0, "size" :10,  
> "fields" :["id","co","prd","pd","st","int","ab","cls","pt"],  
> "query" : {"query\_string":  
> {
> 
> "query": "((test) AND ( (test1) OR (test2))) OR (test)"}},  
> "facets": {  
> "tagid": {  
> "terms": {  
> "field": ["tagid"],"size": 100 }  
> }  
> }}
> 
> This Query is working fine..But it return value Order By rank desc..I  
> Don't need like that.. I need the Result Order by "Or" condition  
> Success...Is It Possible?

Result Should be like this

1. ((test) AND ( (test1) OR (test2)) First this condition result
2. OR (test) Next this Result

--  
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:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [February 20, 2013, 2:43pm UTC](https://discuss.elastic.co/t/sort-doubt/10817/3 "2013-02-20T14:43:43Z")

</div>

You can sorta do this with boosting, where you artificially boost the  
"first query" so that it shows above the "second" query. There may be a  
few oddities (e.g. a highly relevant "second" query may score higher than a  
poorly relevant "first" query, even with boosting), but it should probably  
get you 90% there. Disclaimer I have not tried any of these queries and  
I'm not entirely certain they will work. Buyer beware =)

Alternatively, you could always run two queries and combine the results in  
your application code

First method, inline boosting with Query\_String:  
{  
"query":{  
"query\_string":{  
"query":"((test^5) AND ( (test1^5) OR (test2^5))) OR (test)"  
}  
}  
}

Second method, same idea but uses custom\_boost\_factor to artificially  
inflate the first query.

{  
"query":{  
"bool":{  
"must":{},  
"must\_not":{},  
"should":[  
{  
"custom\_boost\_factor":{  
"query":{  
"query\_string":{  
"query":"((test) AND ( (test1) OR (test2)))"  
}  
},  
"boost\_factor":5  
}  
},  
{  
"query\_string":{  
"query":"test"  
}  
}  
]  
}  
}  
}

If you don't need a strict delineation between results and can allow them  
to mix a bit, this is probably a better query:  
{  
"query":{  
"bool":{  
"must":{  
"query\_string":{  
"query":"test"  
}  
},  
"must\_not":{},  
"should":{  
"custom\_boost\_factor":{  
"query":{  
"query\_string":{  
"query":"((test1) OR (test2)))"  
}  
},  
"boost\_factor":2  
}  
},  
"minimum\_number\_should\_match" : 0  
}  
}  
}

-Zach

On Wednesday, February 20, 2013 7:17:05 AM UTC-5, Maria John Franklin wrote:

> {  
> "from" : 0, "size" :10,  
> "fields" :["id","co","prd","pd","st","int","ab","cls","pt"],  
> "query" : {"query\_string":  
> {
> 
> "query": "((test) AND ( (test1) OR (test2))) OR (test)"}},  
> "facets": {  
> "tagid": {  
> "terms": {  
> "field": ["tagid"],"size": 100 }  
> }  
> }}
> 
> This Query is working fine..But it return value Order By rank desc..I  
> Don't need like that.. I need the Result Order by "Or" condition  
> Success...Is It Possible?

--  
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:** ![Maria\_John\_Franklin](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@Maria\_John\_Franklin](https://discuss.elastic.co/u/Maria_John_Franklin)\
**Post date:** [February 22, 2013, 8:39am UTC](https://discuss.elastic.co/t/sort-doubt/10817/4 "2013-02-22T08:39:13Z")

</div>

On Wednesday, February 20, 2013 8:17:05 PM UTC+8, Maria John Franklin wrote:

> {  
> "from" : 0, "size" :10,  
> "fields" :["id","co","prd","pd","st","int","ab","cls","pt"],  
> "query" : {"query\_string":  
> {
> 
> "query": "((test) AND ( (test1) OR (test2))) OR (test)"}},  
> "facets": {  
> "tagid": {  
> "terms": {  
> "field": ["tagid"],"size": 100 }  
> }  
> }}
> 
> This Query is working fine..But it return value Order By rank desc..I  
> Don't need like that.. I need the Result Order by "Or" condition  
> Success...Is It Possible?

```
After Boosting it's working fine... Thanks for your idea Zachary Tong

```

🙂

--  
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:50am UTC](https://discuss.elastic.co/t/sort-doubt/10817/5 "2017-07-06T02:50:07Z")

</div>


