Implementing Function Score in Elasticsearch with BM25, Views, and Matching Categories

--in short -----

i want to reflect category mathcing every time it matches document's category field and it is list


Hello!

I'm in the process of implementing a search functionality using Elasticsearch's function score feature. My goal is to have the search results sorted based on a combination of factors: the BM25 score of the query, the number of views, and the degree of match with preferred categories.

Incorporating the number of views into the BM25 score is relatively straightforward, as shown below:

"functions": [
  {
    "script_score": {
      "script": {
        "source": "doNormalize(_score + doc['views'].value)"
      }
    }
  }
],
"boost_mode": "replace"

(Note: I intend to use normalized values for the actual implementation.)

The challenge arises when trying to account for category matches. Specifically, I need to add weight each time a category in the search query matches a category in the document's category field, which is expected to be a list or set.

Considering the query will include up to 3 categories in a set, crafting a script to handle this dynamic matching is proving to be quite difficult.

I'm reaching out for any advice, insights, or examples on how to effectively script this part of the function score query.

I need your help.

Wishing everyone a great day.

Hi @dan_kim

Maybe I may have misunderstood, but you want your doc to score better if the category exists in your category list field. If you use the score function, wouldn't using the filter give you the result you want?

Like this:

    "functions": [
        {
          "filter": {
            "terms": {
              "categories": [
                "a"
              ],
              "boost": 1
            }
          },
          "weight": 10
        }
      ]

Thank you for your answer!
I think you are right when i have only one category parameter

But im not sure it will work when i try parameter as category list

like parameter of list of aCategory, bCategory matching make weight of 2
And aCategory make weight of 1

I understand, so you can have a list of categories as a parameter but these lists must have different weights in the case of a match. In this case I think the Terms set query (here you can configure how many itens in list are required for match) might make sense.

As for the weight, perhaps you need to have a logic that when creating the query creates the section of functions with each weight for the list of categories because I think it is not possible to set the weight per parameter.

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.