0
votes

I have tons of articles in various stores. Some of these articles are own brand articles and should be ranked higher than other articles in my elasticsearch search results (both ownbrand and non ownbrand should be shown however.)

I already tried different approached with field_value_factor but that doesn't seem to go well with a boolean field. I also tried the approached solution in Boosting an elasticsearch result based on a boolean field value but that didn't worked well for me. The results with the ownBrand approach were still way lower ranked then a lot of non ownBrand articles.

Index:

schema: {
    articleId: { type: 'text' },
    brandId: { type: 'text' },
    brandName: { type: 'text' },
    countryId: { type: 'text' },
    description: { type: 'text' },
    isOwnBrand: { type: 'boolean' },
    stores: { type: 'keyword' },
  },
};

Query:

      query: {
          function_score: {
            query: {
              bool: {
                must: {
                  multi_match: {
                    query: searchterm,
                    fields: ['name^5', 'name.ngram'],
                    fuzziness: 'auto',
                    prefix_length: 2,
                  },
                },
                filter: [{ term: { stores: storeId } }],
              },
            },
          },
        },
      };

The result should prioritize fields with isOwnBrand = true at the top while still showing relevant articles with isOwnBrand = false below.

I am a bit lost on how to handle this.

1

1 Answers

0
votes

You can use Field Value factor. Below should work fine even on a boolean field as well. try it

{
    "query": {
        "function_score": {
           "query" {...}, #your normal query as in question
            #add below after query
            "field_value_factor": {
                "field": "isOwnBrand",
                "factor": 1.2,
                "modifier": "sqrt",
                "missing": 1
            }
        }
    }
}

One caveat i can think of but haven't tested - since false is 0, above script will score down all documents with false to 0 score, which messes up scoring. You could either make the isOwnBrand a number field and set priority starting 1

OR you could also use script_score