# Boost two related fields in a search query

**URL:** <https://discuss.elastic.co/t/boost-two-related-fields-in-a-search-query/278428>\
**Category:** Elasticsearch\
**Created:** [July 12, 2021, 12:45pm UTC](https://discuss.elastic.co/t/boost-two-related-fields-in-a-search-query/278428 "2021-07-12T12:45:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Antojvlt](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@Antojvlt](https://discuss.elastic.co/u/Antojvlt)\
**Post date:** [July 12, 2021, 12:45pm UTC](https://discuss.elastic.co/t/boost-two-related-fields-in-a-search-query/278428/1 "2021-07-12T12:45:57Z")

</div>

Hi,

I am new to Elasticsearch and I encounter a difficultly to make my search query to work as expected.  
I want to search on documents that contain two field:

```auto
"review_count" : {
          "type" : "long"
        },
"stars" : {
          "type" : "float"
        }

```

And I would like to return documents based on my search and to prioritize those with the highest stars value but also with an high review\_count value. This of course needs a trade off.

I tried to use a "function\_score" with "field\_value\_factor" to boost those values:

```auto
{
  "size": 3,
  "query": {
    "function_score": {
      "functions": [
        {
          "field_value_factor": {
            "field": "stars",
            "factor": 10,
            "missing": 1
          }
        },
        {
          "field_value_factor": {
            "field": "review_count",
            "factor": 1.2,
            "modifier": "sqrt",
            "missing": 1
          }
        }
      ],
      "query": {
        "multi_match" : {
          "query" : "restaurant",
          "fields" : ["name", "categories"] 
        }
      }
    }
  },
  "_source": [
    "stars",
    "review_count"
  ]
}

```

But this query isn't powerful enough as the tradeoff is not right and I don't have a clear idea on how I could proceed. This query returns:

```auto
{
        "_score" : 7810.9053,
        "_source" : {
          "review_count" : 3771,
          "stars" : 4.0
        }
      },
      {
        "_score" : 6566.245,
        "_source" : {
          "review_count" : 1213,
          "stars" : 4.0
        }
      },
      {
        "_score" : 6180.8545,
        "_source" : {
          "review_count" : 1483,
          "stars" : 4.5
        }
      }

```

**In the ideal result, the third result would be the first as it has 0.5 stars more and it still has a lot of review\_count (1483).**

I also tried something with a "script\_score" and with a script using a min-max scaling but it doesn't look right at all:

```auto
{
  "size": 3,
  "query": {
    "script_score": {
      "query": {
        "multi_match" : {
          "query" : "restaurant",
          "fields" : ["name", "categories"] 
        }
      },
      "script": {
        "params": {
          "stars_factor": 0.6,
          "review_count_factor": 0.4
        },
        "source": """(((doc['stars'].value + 1) - Math.min(doc['stars'].value, doc['review_count'].value)) / (Math.max(doc['stars'].value, doc['review_count'].value) - Math.min(doc['stars'].value, doc['review_count'].value))) * params.stars_factor + (((doc['review_count'].value + 1) - Math.min(doc['stars'].value, doc['review_count'].value)) / (Math.max(doc['stars'].value, doc['review_count'].value) - Math.min(doc['stars'].value, doc['review_count'].value))) * params.review_count_factor"""
      }
    }
  },
  "_source": [
    "stars",
    "review_count"
  ]
}

```

Thank you for your help!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 9, 2021, 12:46pm UTC](https://discuss.elastic.co/t/boost-two-related-fields-in-a-search-query/278428/2 "2021-08-09T12:46:35Z")

</div>

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