1

What is the importance of the token position to scoring? I'd expect when the token matches that a token in position 1 to be returned higher in the results than the token in position 2.

I've an index with orange coconut apple coconut orange apple coconut apple orange coconut red orange

Search for the word "coconut" with a match query.

It is returned in these positions 1 orange coconut apple 2 coconut red orange 3 coconut orange apple 4 coconut apple orange

I'd expect to see orange coconut apple returned 4th.

Why is this occurring and what may be the solution/setting I'm missing?

Thanks

PUT name
{
  "template": "name*",
  "mappings": {
    "name": {
      "_all": {
        "enabled": false
      },
      "properties": {
        "name": {
          "type": "text",
          "analyzer": "standard"
        }
      }
    }
  }
}

PUT name/name/1
{ 
    "name" : "orange coconut apple"
}
PUT name/name/2
{ 
    "name" : "coconut orange apple"
}
PUT name/name/3
{ 
    "name" : "coconut red orange"
}
PUT name/name/4
{ 
    "name" : "coconut apple orange"
}

GET name/_search
{
    "query": {
        "match" : { "name" : "coconut" }
    }
}
Insy
  • 11
  • 2

1 Answers1

1

try using a match phrase query inside of a boolean query, where your above query is a must clause and the match phrase query is a should clause.

For an explanation check Elasticsearch - The definitive guide

I highly recommend reading the whole search in depth chapter, as this clears a lot of things for controlling relevance in queries, which are non-obvious.

alr
  • 1,744
  • 1
  • 10
  • 11
  • Thanks @alr , i'll read up on those sections Do you mean like this? If so it doesn't change the order. `GET name/_search { "query": { "bool": { "must": [ { "match" : { "name" : "coconut" } } ], "should": [ { "match_phrase" : { "name" : "coconut" } } ] } } }` – Insy Jul 18 '17 at 10:49
  • oh, I'm sorry, I misread your query and thought it contained two words. Positions are not taken into account unless your are doing proximity/phrase query. You could take a look at the [span query family](https://www.elastic.co/guide/en/elasticsearch/reference/5.5/span-queries.html) and use that in a should clause. – alr Jul 18 '17 at 13:51