# Chain or relate results of one response to another in msearch

**URL:** <https://discuss.elastic.co/t/chain-or-relate-results-of-one-response-to-another-in-msearch/224397>\
**Category:** Elasticsearch\
**Created:** [March 20, 2020, 10:23am UTC](https://discuss.elastic.co/t/chain-or-relate-results-of-one-response-to-another-in-msearch/224397 "2020-03-20T10:23:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Noctis17](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/noctis17/32/41656_2.png) [@Noctis17](https://discuss.elastic.co/u/Noctis17)\
**Post date:** [March 20, 2020, 10:23am UTC](https://discuss.elastic.co/t/chain-or-relate-results-of-one-response-to-another-in-msearch/224397/1 "2020-03-20T10:23:10Z")

</div>

Hello and good day! With the pandemic COVID19 lurking around, hope all of you guys are fine

Just a quick question, with the past year, I've been tasked with certain endpoints that require Elasticsearch queries. I'm now in the optimization part, and just so happened I stumble upon the **Multi Search** or commonly known as **msearch**

Now with the idea of **msearch** , I've thought I could lessen the number of **searches** that I make to our ES servers. Before, I'm making 2 **search** queries, and now my aim is to have 1 **msearch** query

Is there a way for the first _aggs response_ of **msearch** be related to the next aggregations? I mean, is there a way for me to get the response of the first aggs in **msearch** then past it to the next in run-time?

Example Scenario:

**FIRST AGGS**  
I'm getting the MAX VALUE of a certain field

```
"aggs" => [
    'MAX_SVALUE' => [
        'max' => [
            'field' => 'latest.soc_mm_score'
        ]
    ],
]

```

**SECOND AGGS**  
Using the _max_ value above

> Please note, the $mxs there, is a PHP based array, it contains the max values form the **FIRST AGGS**

```
"aggs" => [
    "SVALUE" => [
        "terms" => [
            "script" => [
                "source"=>"
                    double temp = Double.parseDouble((((doc['latest.soc_mm_score'].value * 10) / ".$mxs[$platforms[$p]].").toString()));
                    return temp;
                ",
                "lang"=>"painless"
            ],
            "size"=> 10000
        ]
    ]
]

```

Thank you in advance for your help!

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 20, 2020, 1:29pm UTC](https://discuss.elastic.co/t/chain-or-relate-results-of-one-response-to-another-in-msearch/224397/2 "2020-03-20T13:29:46Z")

</div>

Hey,

every search operation within an msearch request is fully independent from each other, so the answer to this one would be `no`.

What are you trying to achieve with the second aggregation? Maybe we can find a way to do this in a single request or without using scripting to keep performance high.

--Alex

---

<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:** [April 17, 2020, 1:29pm UTC](https://discuss.elastic.co/t/chain-or-relate-results-of-one-response-to-another-in-msearch/224397/3 "2020-04-17T13:29:59Z")

</div>

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