elasticsearch boost importance of exact phrase match

做~自己de王妃 提交于 2019-11-30 03:44:40

You can combine different queries together using a bool query, and you can assing a different boost to them as well. Let's say you have a regular match query for both the terms, regardless of their positions, and then a phrase query with a higher boost.

Something like the following:

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "field": "web developer"
          }
        },
        {
          "match_phrase": {
            "field": "web developer",
            "boost": 5
          }
        }
      ],
      "minimum_number_should_match": 1
    }
  }
}

As an alternative to javanna's answer, you could do something similar with must and should clauses within a bool query:

{
  "query": {
    "bool": {
      "must": {
          "match": {
            "field": "web developer",
            "operator": "and"
          }
      },
      "should": {
          "match_phrase": {
            "field": "web developer"
          }
      }
    }
  }
}

Untested, but I believe the must clause here will match results containing both 'web' and 'developer' and the should clause will score phrases matching 'web developer' higher.

You could try using rescore to run an exact phrase match on your initial results. From the docs:

"Rescoring can help to improve precision by reordering just the top (eg 100 - 500) documents returned by the query and post_filter phases, using a secondary (usually more costly) algorithm, instead of applying the costly algorithm to all documents in the index."

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-rescore.html

I used below sample query in my case which is working. It brings exact + fuzzy results but exact ones are boosted!

{ "query": {
"bool": {
  "should": [
    {
      "match": {
        "name": "pala"
      }
    },
    {
      "fuzzy": {
        "name": "pala"
      }
    }
  ]
}}}

I think its default behaviour already with match query "or" operator. It'll filter phrase "web developer" first and then terms like "web" or "develeper". Though you can boost your query using above answers. Correct me if I'm wrong.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!