1

I have an issue to implement elasticsearch with the query "energy saving tv". I have 3 objects with "title" field:

T1: Phone with LG application is an energy saving tv

T2: That tv made by energy saving LG applications

T3: Phone with LG application ensures optimal energy saving

Then I used "match" and "AND" operator for query "energy saving tv":

GET my_index/_search
{
    "query": {
        "match": {
            "title": {
                "query": "energy saving tv",
                "operator": "and"
            }
        }
    }
}

Result:

  • Score T1: 5.0
  • Score T2: 5.37

So T2's score is higher than T1's score, but I wanna title that has form "energy*saving*tv" (in the order of words in the keyword) will have a score higher. Pls help me. Thank you very much!

TanPS
  • 31
  • 3

1 Answers1

0

You can use a Match phrase query to match a phrase comprised of several words.

{
  "query": {
    "match_phrase": {
      "title": "energy saving tv"
    }
  }
}

Note that this will only match T1 since the exact order is preserved.

If you also want to include other results with a more mixed up or spread apart word order you can add the slop parameter.

This will also match T2, but with a lower score:

{
  "query": {
    "match_phrase": {
      "title": {
        "query": "energy saving tv",
        "slop": 10
      }
    }
  }
}

The slop basically defines the upper limit to how often you can move a query term to the right or left in order to match the document. It defaults to 0.

E.g. going from the query "energy saving tv" to the document "energy tv saving" would require a slop of 2, since tv moves one term to the left and saving moves one term to the right.

See this answer for a great visual explanation.

Onno Rouast
  • 662
  • 5
  • 14