# Join the result of two different queries indices

**URL:** <https://discuss.elastic.co/t/join-the-result-of-two-different-queries-indices/275470>\
**Category:** Elasticsearch\
**Created:** [June 9, 2021, 3:46pm UTC](https://discuss.elastic.co/t/join-the-result-of-two-different-queries-indices/275470 "2021-06-09T15:46:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Walter\_Jimenez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/walter_jimenez/32/89999_2.png) [@Walter\_Jimenez](https://discuss.elastic.co/u/Walter_Jimenez)\
**Post date:** [June 9, 2021, 3:46pm UTC](https://discuss.elastic.co/t/join-the-result-of-two-different-queries-indices/275470/1 "2021-06-09T15:46:48Z")

</div>

Good morning, excuse my English. I am new to elastic and I am doing some tests with the queries, I have the following case:

I have two indeces, metrics and a custom one of my own, I was able to make each one a separate queries that are fired when a record is obtained, now what I want to do is try to join both queries, I tried as follows:

```auto
GET _search
{
  "size": 1,
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "apache.status.uptime.uptime": {
              "from": 0,
              "to": null,
              "include_lower": false,
              "include_upper": false,
              "boost": 1.0
            }
          }
        },
        {
          "range": {
            "@timestamp": {
              "gte": "now-20s",
              "to": "now",
              "include_lower": false,
              "include_upper": false,
              "time_zone": "Z",
              "format": "strict_date_optional_time_nanos",
              "boost": 1.0
            }
          }
        },
        {
          "range": {
            "duration": {
              "from": 0,
              "to": null,
              "include_lower": false,
              "include_upper": false,
              "boost": 1.0
            }
          }
        },
        {
          "range": {
            "start2": {
              "gte": "now-30m",
              "to": "now",
              "include_lower": false,
              "include_upper": false,
              
              "boost": 1.0
              
            }
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1.0
    }
  },
  "_source": false,
  "fields": [
    {
      "field": "apache.status.uptime.uptime"
    },
    {
      "field": "duration"
      
    },
    {
      "field": "answer2"
    },
    {
      "field": "answer"
    }
  ]
 
}

```

I am assuming that it does not bring results when I join them, because it tries to search in the same index all 4 conditions. What I'm trying to do is not possible?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [June 10, 2021, 8:29pm UTC](https://discuss.elastic.co/t/join-the-result-of-two-different-queries-indices/275470/2 "2021-06-10T20:29:52Z")

</div>

Indeed, for a document to be a hit for this search request, it must satisfy all 4 conditions inside a must clause.

---

<div class="post-metadata">

**Author:** ![Walter\_Jimenez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/walter_jimenez/32/89999_2.png) [@Walter\_Jimenez](https://discuss.elastic.co/u/Walter_Jimenez)\
**Post date:** [June 11, 2021, 8:47pm UTC](https://discuss.elastic.co/t/join-the-result-of-two-different-queries-indices/275470/3 "2021-06-11T20:47:10Z")

</div>

> [@mayya](#):
>
> De hecho, para que un documento sea un éxito para esta solicitud de búsqueda, debe cumplir las 4 condiciones dentro de una cláusula obligatoria.

Thanks for answering.

I think I approach the problem wrong, is there a way that will return something to me when two conditions are met in one document and two other conditions are met in another document? that is, if in one time interval, document one there are records and in another time interval, document two there are records, return 'something'. Is this possible with queries?

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [June 11, 2021, 9:53pm UTC](https://discuss.elastic.co/t/join-the-result-of-two-different-queries-indices/275470/4 "2021-06-11T21:53:38Z")

</div>

You can do something like this:

```auto
bool
 should
    must
      condition1
      condition2
    must
       condition3
       condition4

```

Then for a document to be a hit in must either satisfy (conditions 1 and condition 2) OR (condition3 and condition4). But it could also be a hit if a document satisfies all 4 conditions.

---

<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 9, 2021, 9:54pm UTC](https://discuss.elastic.co/t/join-the-result-of-two-different-queries-indices/275470/5 "2021-07-09T21:54:11Z")

</div>

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