Relevance by type on same field in elasticsearch

心不动则不痛 提交于 2019-12-11 06:43:57

问题


Is there any way to boost search results on same field depending on type?

My basic boosting is something like:

GET _search
{
   "query": {
      "simple_query_string": {
         "query": "mangan",
         "fields":["_all", "title^6"]
      }
   }
}

But for some other documents I want title to be less important, so I tried to prefix it with type:

GET _search
{
   "query": {
      "simple_query_string": {
         "query": "mangan",
         "fields":[
            "_all",
            "DocumentationPage.title^6",
            "DocumentationPage.title^6"]
      }
   }
}

But then it does not boost at all. As a last resort I could use Funcsion/Script Score bu would like to avoid it.

For sake of example, assume that document contains just title field.


回答1:


A simple way to achieve this is re-writing the query in the OP as a dis-max query.

Example for elasticsearch 5.x:

 {
   "query": {
      "dis_max": {
         "queries": [
            {
               "simple_query_string": {
                  "fields": [
                     "_all"
                  ],
                  "query": "mangan"
               }
            },
            {
               "bool": {
                  "filter": {
                     "type": {
                        "value": "DocumentationPage"
                     }
                  },
                  "must": [
                     {
                        "simple_query_string": {
                           "fields": [
                              "title^6"
                           ],
                           "query": "mangan"
                        }
                     }
                  ]
               }
            }
         ]
      }
   }
}


来源:https://stackoverflow.com/questions/41812231/relevance-by-type-on-same-field-in-elasticsearch

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