How can I do a “match_phrase” that ranks solely on “does the phrase exists”?

Is there a way to make “match_phrase” only look for a match, but not take other things into account, like the match in relation to other terms in the text field?

i.e. If I search "star wars", "Star Wars: Revelations" receives a hire score than "Star Wars: The Last Jedi" because "Star Wars: Revelations" has 3 terms vs "Star Wars: The Last Jedi" which has 5 terms.

I would like all of them to receive a consistent score. Because I am then using function_score to rank them on an internal popularity value.

I guess I am looking for something like does the text field have the term? Score is: yes=1 no=0

That should be possible with constant score, something like this for default boost/score of 1.0 for every match and will not return documents which do not match here:

GET my_index/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "match_phrase": {
          "title": "Star Wars"
        }
      }
    }
  }
}

Or if the aim is to return all documenst with a title field and add score 1 to the phrase matches (other docs will have score 0):

GET my_index/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "exists": {
            "field": "title"
          }
        }
      ],
      "should": [
        {
          "constant_score": {
            "filter": {
              "match_phrase": {
                "title": "Star Wars"
              }
            }
          }
        }
      ]
    }
  }
}
1 Like

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