# How to multiply score of shouldClause and mustClause in booleanQuery?

**URL:** https://discuss.elastic.co/t/how-to-multiply-score-of-shouldclause-and-mustclause-in-booleanquery/276566
**Category:** Elasticsearch
**Created:** [June 21, 2021, 5:12pm UTC](https://discuss.elastic.co/t/how-to-multiply-score-of-shouldclause-and-mustclause-in-booleanquery/276566 "2021-06-21T17:12:52Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![lunablessing](https://avatars.discourse-cdn.com/v4/letter/l/d07c76/32.png) [@lunablessing](https://discuss.elastic.co/u/lunablessing)
#### Post date: [June 21, 2021, 5:12pm UTC](https://discuss.elastic.co/t/how-to-multiply-score-of-shouldclause-and-mustclause-in-booleanquery/276566/1 "2021-06-21T17:12:52Z")

</div>

I know booleanQuery will add score of should clauses and must clauses. I used to do **log(scoreOfShould) + log(scoreOfMust) + log(\_score)** for sorting as scoreOfShould \* scoreOfMust \* \_score.

In my case, scoreOfShould is querying nested doc, and the final score sometimes less than 1 which leads log(scoreOfShould) to be negative.

My query object looks like this. The outer object has titile, description, outerRatio and logOuterRatio. The nested object has keyword, innerRatio and logInnerRatio. What I want is sorting by outerRatio \* innerRatio \* \_score. So I get \_score from the multiMatch, then use script\_score to calulate logOutRatio + log(\_score), then add the max log(innerRatio) of hit child docs. Sometimes, log(innerRatio) is negative to weaken the must result.

**But negative score is not supported in 7.x, any one can help on this?**

```auto
{
	"from": 0,
	"size": 20,
	"query": {
		"bool": {
			"should": [
				{
					"nested": {
						"path": "innerObject",
						"score_mode": "max",
						"query": {
							"function_score": {
								"functions": [
									{
										"field_value_factor": {
											"field": "innerObject.logInnerRatio"
										}
									}
								],
								"query": {
									"bool": {
										"filter": {
											"terms": {
												"innerObject.keyword": [
													"led",
													"logo"
												]
											}
										}
									}
								},
								"boost_mode": "replace"
							}
						}
					}
				}
			],
			"must": [
				{
					"function_score": {
						"functions": [
							{
								"script_score": {
									"script": {
										"source": "Math.log(_score) + doc['logOuterRatio'].value"
									}
								}
							}
						],
						"query": {
							"bool": {
								"must": {
									"multi_match": {
										"query": "led logo",
										"type": "cross_fields",
										"fields": [
											"title^2.0",
											"description^0.5"
										]
									}
								}
							}
						},
						"boost_mode": "replace"
					}
				}
			]
		}
	},
	"_source": [
		"id",
		"uid",
		"title"
	],
	"sort": [
		{
			"_score": {
				"order": "desc"
			}
		}
	]
}

```

---

<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: [July 19, 2021, 5:13pm UTC](https://discuss.elastic.co/t/how-to-multiply-score-of-shouldclause-and-mustclause-in-booleanquery/276566/2 "2021-07-19T17:13:38Z")

</div>

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