Relevance by type on same field in elasticsearch

2019-08-01 06:32发布

问题:

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