# How to avoid repeating the same sub-queries in a single POST request?

**URL:** <https://discuss.elastic.co/t/how-to-avoid-repeating-the-same-sub-queries-in-a-single-post-request/148766>\
**Category:** Elasticsearch\
**Created:** [September 17, 2018, 7:02am UTC](https://discuss.elastic.co/t/how-to-avoid-repeating-the-same-sub-queries-in-a-single-post-request/148766 "2018-09-17T07:02:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![NewHere](https://avatars.discourse-cdn.com/v4/letter/n/5e9695/32.png) [@NewHere](https://discuss.elastic.co/u/NewHere)\
**Post date:** [September 17, 2018, 7:02am UTC](https://discuss.elastic.co/t/how-to-avoid-repeating-the-same-sub-queries-in-a-single-post-request/148766/1 "2018-09-17T07:02:00Z")

</div>

Hi I have a set of sub-queries that I first need to find the MAX score of sub-queries (using dis\_max) then add together the SUM of the sub-queries scores as the final score. Is there a way to put them together in one POST API call without repeating the exact same set of sub-queries in two places? Thank you.

e.g.:

```
{
	"query":{
		"bool":{
			"should":[{
				"dis_max":{
					"queries": [{...sub-queries...}]
				}
			},{
				"bool":{
					"should":[{...sub-queries...}]
				}
			}]
		}
	}
}
```

---

<div class="post-metadata">

**Author:** ![NewHere](https://avatars.discourse-cdn.com/v4/letter/n/5e9695/32.png) [@NewHere](https://discuss.elastic.co/u/NewHere)\
**Post date:** [September 18, 2018, 12:58am UTC](https://discuss.elastic.co/t/how-to-avoid-repeating-the-same-sub-queries-in-a-single-post-request/148766/2 "2018-09-18T00:58:09Z")

</div>

Bump. Hoping someone in the know could shed some light.  
And I have looked at query template, but the sub-queries here are not some fixed queries that one just fill in the parameters. The sub-queries are from user input so query template is not going to help.

---

<div class="post-metadata">

**Author:** ![NewHere](https://avatars.discourse-cdn.com/v4/letter/n/5e9695/32.png) [@NewHere](https://discuss.elastic.co/u/NewHere)\
**Post date:** [September 19, 2018, 3:37am UTC](https://discuss.elastic.co/t/how-to-avoid-repeating-the-same-sub-queries-in-a-single-post-request/148766/3 "2018-09-19T03:37:22Z")

</div>

Bump. Hoping someone in the know could shed some light.

---

<div class="post-metadata">

**Author:** ![NewHere](https://avatars.discourse-cdn.com/v4/letter/n/5e9695/32.png) [@NewHere](https://discuss.elastic.co/u/NewHere)\
**Post date:** [September 26, 2018, 1:02am UTC](https://discuss.elastic.co/t/how-to-avoid-repeating-the-same-sub-queries-in-a-single-post-request/148766/4 "2018-09-26T01:02:02Z")

</div>

Bump. Hoping someone in the know could shed some light.

---

<div class="post-metadata">

**Author:** ![NewHere](https://avatars.discourse-cdn.com/v4/letter/n/5e9695/32.png) [@NewHere](https://discuss.elastic.co/u/NewHere)\
**Post date:** [September 27, 2018, 4:04am UTC](https://discuss.elastic.co/t/how-to-avoid-repeating-the-same-sub-queries-in-a-single-post-request/148766/5 "2018-09-27T04:04:28Z")

</div>

Do you all have to have a paid membership before the devs will answer your posts?

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [September 27, 2018, 5:42am UTC](https://discuss.elastic.co/t/how-to-avoid-repeating-the-same-sub-queries-in-a-single-post-request/148766/6 "2018-09-27T05:42:02Z")

</div>

As far as I know there is no way to avoid this duplication, and the lack of response may indicate that there isn't anyone else that has a solution either. Usually queries are assembled programatically, so I do not understand why this duplication would be a problem. Could you please elaborate?

---

<div class="post-metadata">

**Author:** ![NewHere](https://avatars.discourse-cdn.com/v4/letter/n/5e9695/32.png) [@NewHere](https://discuss.elastic.co/u/NewHere)\
**Post date:** [September 28, 2018, 1:55am UTC](https://discuss.elastic.co/t/how-to-avoid-repeating-the-same-sub-queries-in-a-single-post-request/148766/7 "2018-09-28T01:55:45Z")

</div>

Thank you @Christian_Dahlqvist. I surely can take a No as the answer, I asked merely thinking I might have written my query poorly, as it just seemed unnecessarily pumping up the size of the POST request and I was hoping something like a **function\_score** query that operates on \_scores instead of \_score, so that I can do Math.max(\_scores) + Math.sum(\_scores).

---

<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:** [October 26, 2018, 1:55am UTC](https://discuss.elastic.co/t/how-to-avoid-repeating-the-same-sub-queries-in-a-single-post-request/148766/8 "2018-10-26T01:55:47Z")

</div>

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