Elastic Search: Boost Results with “Exact match” in “query_string” without specifying the field

问题: I am using query_string to search for records from Elastic, Example Query : GET /stories_qa/_search { "query": { "query_string": { "query": "Johnson &&...

问题:

I am using query_string to search for records from Elastic,

Example Query :

GET /stories_qa/_search
{
  "query": {
    "query_string": {
      "query": "Johnson && Johnson"
    }
  }
}

This query gives me relevant records, but the exact match record is not on top, I figured out on how we can boost records based on specific fields, but in my case, I don't want to provide field and make search restrictive, Is there an option in ElasticSearch to achieve the same.


回答1:

You can probably do something like below, but do thorough testing and test multiple scenarios before you move it to production.

POST <your_index_name>/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              {
                "query_string": {
                  "query": ""Karnataka Delhi"",
                  "default_operator": "AND"
                }
              }
            ]
          }
        },
        {
          "query_string": {
            "query": "Karnataka Delhi",
            "default_operator": "OR",
            "boost": 2
          }
        },
        {
          "query_string": {
            "query": "Karnataka Delhi",
            "default_operator": "AND",
            "boost": 4
          }
        }
      ]
    }
  }
}

Hope this helps!


回答2:

It would help with examples, but you can use phrase_match query by adding double quote. If you add a boost to the phrase_match query you will be able to get to exact match ranked higher.

The following query should do what you want.

GET /stories_qa/_search
{
  "query": {
    "query_string": {
      "query": "("Johnson && Johnson")^2 OR (Johnson && Johnson)"
    }
  }
}
  • 发表于 2019-07-07 02:40
  • 阅读 ( 165 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除