_score of and "OR" query is invalid?


(Chrisribe) #1

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?

Chris


(Mark Harwood) #2

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


(Chrisribe) #3

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"

curl -X POST "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" -d '{"id": 2, "ids":["5", "4", "3", "2", "1", "99", "99", "99", "99"]}'
curl -X POST "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:
https://pastebin.com/vtP3aNGF

Chris


(Mark Harwood) #4

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

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)

(Chrisribe) #5

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


(system) #6

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