Relevance by type on same field in elasticsearch
Asked Answered
S

1

1

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.

Scintillator answered 23/1, 2017 at 17:33 Comment(0)
B
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"
                        }
                     }
                  ]
               }
            }
         ]
      }
   }
}
Beitnes answered 23/1, 2017 at 18:53 Comment(1)
Thank You, works as expected! Seems that this technique might be also helpful when filtering different type documents by different conditions.Scintillator

© 2022 - 2024 — McMap. All rights reserved.