# \_score of and "OR" query is invalid?

**URL:** <https://discuss.elastic.co/t/-score-of-and-or-query-is-invalid/98732>\
**Category:** Elasticsearch\
**Created:** [August 29, 2017, 4:54pm UTC](https://discuss.elastic.co/t/-score-of-and-or-query-is-invalid/98732 "2017-08-29T16:54:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisribe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chrisribe/32/22398_2.png) [@chrisribe](https://discuss.elastic.co/u/chrisribe)\
**Post date:** [August 29, 2017, 4:54pm UTC](https://discuss.elastic.co/t/-score-of-and-or-query-is-invalid/98732/1 "2017-08-29T16:54:22Z")

</div>

Hi,

Pulling my hair out here.  
I created a query on a string field and want to get documents that have the most matches by order desc.

Query:  
_exists_:ids AND (ids:5 || 17 || 21 || 27 || 38 || 41 || 43 || 46 || 47 || 77 || 91 || 92 || 95 || 96 || 97 || 113 || 118 || 128 || 129 || 134 || 135 || 138 || 140 || 141 || 142)

Documents:  
{id: 1, ids:"5 46 142 138 135"}  
{id: 2, ids:"5 46 142 138"}  
{id: 3, ids:"5 46 142"}

I was expecting that the \_score could be used to determine the closest match but no, it seems random or maybe sorting alphanumerically ?

'sort' =\> array(  
'\_score' =\> array('order' =\> 'desc')  
)

But I am getting odd results, what type of query or sort do I need ?  
Thanks

Came across this and seems to be similar no?

> <https://github.com/elastic/elasticsearch/issues/13806>

Chris

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [August 30, 2017, 8:56am UTC](https://discuss.elastic.co/t/-score-of-and-or-query-is-invalid/98732/2 "2017-08-30T08:56:07Z")

</div>

Let's have a look at the explain output: [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-explain.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-explain.html)

---

<div class="post-metadata">

**Author:** ![chrisribe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chrisribe/32/22398_2.png) [@chrisribe](https://discuss.elastic.co/u/chrisribe)\
**Post date:** [September 20, 2017, 3:33pm UTC](https://discuss.elastic.co/t/-score-of-and-or-query-is-invalid/98732/3 "2017-09-20T15:33:51Z")

</div>

Hi Mark,

Sorry I did not reply sooner got pulled off this problem.  
Just so everything is clearer I made a demo data set that shows that the sort is not what I expect.

> curl -XDELETE 'localhost:9200/demo?pretty'  
> curl -X PUT "[http://localhost:9200/demo](http://localhost:9200/demo)"
> 
> curl -X POST "[http://localhost:9200/demo/1](http://localhost:9200/demo/1)" -d '{"id": 1, "ids":["1", "2", "3", "4", "5", "6", "7", "8", "9", "22"]}'  
> curl -X POST "[http://localhost:9200/demo/2](http://localhost:9200/demo/2)" -d '{"id": 2, "ids":["5", "4", "3", "2", "1", "99", "99", "99", "99"]}'  
> curl -X POST "[http://localhost:9200/demo/3](http://localhost:9200/demo/3)" -d '{"id": 3, "ids":["1", "3", "2", "99", "99", "99", "99", "99", "99"]}'

When searching with this query:

> ids: 1 | 2 || 3 || 4 || 5 || 22

I get id: 2,1,3 as the results it should be 1,2,3 no ?

Here are the results with explain:

> **[Array( \[took\] =\> 2 \[timed\_out\] =\> \[\_shards\] - Pastebin.com](https://pastebin.com/vtP3aNGF)**
>
> Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.

Chris

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [September 21, 2017, 3:24pm UTC](https://discuss.elastic.co/t/-score-of-and-or-query-is-invalid/98732/4 "2017-09-21T15:24:52Z")

</div>

The explain output has mentions of matches on the `_all` field. I expect that is because your query is not what you expect:

> [@chrisribe](#):
>
> exists:ids AND (ids:5 || 17 || 21 || 27 || 38 || 41 || 43 || 46 || 47 || 77 || 91 || 92 || 95 || 96 || 97 || 113 || 118 || 128 || 129 || 134 || 135 || 138 || 140 || 141 || 142)

Here I think the value `5` is the only term targeting the `ids` fields and terms `17`,`21` etc are targeting the default `_all` field. Try put the fieldname outside of the brackets to change the default field context for all terms between the brackets i.e.

```
ids:( 5 || 17) 

```

rather than

```
(ids:5 || 17)

```

---

<div class="post-metadata">

**Author:** ![chrisribe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chrisribe/32/22398_2.png) [@chrisribe](https://discuss.elastic.co/u/chrisribe)\
**Post date:** [September 22, 2017, 6:53pm UTC](https://discuss.elastic.co/t/-score-of-and-or-query-is-invalid/98732/5 "2017-09-22T18:53:41Z")

</div>

YES !!

Thank you Mark, that was exactly my problem !! Now the sort works in an expected way !

I have been messing with this for at least a week, going trough different query methods and even field boosting. It was getting confusing and complex for nothing. I just always was using that bracket format....

Why is it always the little things that trip you up...  
Thanks again.  
Chris

---

<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:** [October 20, 2017, 6:53pm UTC](https://discuss.elastic.co/t/-score-of-and-or-query-is-invalid/98732/6 "2017-10-20T18:53:51Z")

</div>

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