Adjusting Field Boost Based on Indexed Scores in Elasticsearch

Description:

I’m working on an e-commerce site where products are indexed in Elasticsearch. Users search through fields like brand, publisher, product_name, and category. I typically give the highest boost to the name field when matching search queries. However, some product names are unique, and I want to adjust the boost based on a pre-determined score for each product’s name field that I store during indexing.

Here’s how I’ve structured my Elasticsearch index for scoring:

json

Copy code

"properties": {
"fieldScores": {
"type": "nested",
"properties": {
"name": {
"properties": {
"value": {
"type": "double",
"default": 1
},
"weight": {
"type": "double",
"default": 1
}
}
}
}
}
}


Now, I want to adjust the boost during the search based on the `fieldScores.name.value` from the indexed data.

For example:

1. **Product 1**: Product name is "cotton swab", category is "books", brand is "xyz", and `fieldScores.name.value` = 0.
2. **Product 2**: Product name is "cotton swab 100 pieces", category is "cotton swabs", brand is "gök", and `fieldScores.name.value` = 1.
3. **Product 3**: Product name is "cotton swab 3000 pieces", category is "cotton swabs", brand is "gök", and `fieldScores.name.value` = 1.

When a user searches for "cotton swab", the first product should not receive any boost from the `name` field despite the exact match because `fieldScores.name.value` is 0. However, the second and third products should receive a boost from the `name` field as their `fieldScores.name.value` is 1.

I need help constructing an Elasticsearch query that adjusts the boost for the `name` field based on the stored `fieldScores.name.value`. How can I achieve this?

Hi @Gokhan_Gunes

Try use the field value factor.

The field_value_factor function allows you to use a field from a document to influence the score.

We can call it boost from doc

"I have a query where I search across multiple fields and assign boost values to those fields. However, I want to influence the field-specific boost based on the score stored in the document.

For example, if a product's name is very generic, I want it to receive a lower score from the name field. But if the search term is not related to the product name and is instead related to the product category, the document should get its score from the categoryName field and not be penalized by the name field score. Essentially, I want the boost value for the name field to be influenced by the score stored in the document for that field, but only when searching within the name field."