elasticsearch boost importance of exact phrase mat

2019-03-12 08:04发布

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.

5条回答
不美不萌又怎样
2楼-- · 2019-03-12 08:37

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.

查看更多
Juvenile、少年°
3楼-- · 2019-03-12 08:38

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"
      }
    }
  ]
}}}
查看更多
贪生不怕死
4楼-- · 2019-03-12 08:46

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
    }
  }
}
查看更多
别忘想泡老子
5楼-- · 2019-03-12 08:46

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.

查看更多
我命由我不由天
6楼-- · 2019-03-12 08:48

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

查看更多
登录 后发表回答