elasticsearch boost importance of exact phrase match

倾然丶 夕夏残阳落幕 提交于 2019-12-08 23:05:44

问题


Is there a way in elasticsearch to boost the importance of the exact phrase appearing in the the document?

For example if I was searching for the phrase "web developer" and if the words "web developer" appeared together they would be boosted by 5 compared to "web" and "developer" appearing separately throughout the document. Thereby any document that contained "web developer" together would appear first in the results.


回答1:


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
    }
  }
}



回答2:


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.




回答3:


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




回答4:


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"
      }
    }
  ]
}}}



回答5:


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.



来源:https://stackoverflow.com/questions/18481600/elasticsearch-boost-importance-of-exact-phrase-match

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