# Join two searches with nested field

**URL:** <https://discuss.elastic.co/t/join-two-searches-with-nested-field/339213>\
**Category:** Elasticsearch\
**Created:** [July 25, 2023, 3:42pm UTC](https://discuss.elastic.co/t/join-two-searches-with-nested-field/339213 "2023-07-25T15:42:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Murilo\_Livorato](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/murilo_livorato/32/123848_2.png) [@Murilo\_Livorato](https://discuss.elastic.co/u/Murilo_Livorato)\
**Post date:** [July 25, 2023, 3:42pm UTC](https://discuss.elastic.co/t/join-two-searches-with-nested-field/339213/1 "2023-07-25T15:42:15Z")

</div>

hello , I have been trying to join those two searches , but I didnt managed .  
I want to do this -  
SELECT user-data WHERE company.id = 1 AND timestamp BETWEEEN 2023-05-04 - 2023-06-05  
// RESULT 100

```auto
GET /user-data/_search
{
  "query": {
    "nested": {
      "path": "company",
      "query": {
        "bool": {
          "must": [
            { "match": { "company.id": 1 } }
          ]
        }
      }
    }
  }
}

```

// RESULT 20

```auto
GET /user-data/_search
{
 "query": {
    "bool": {
     "must": [
        { "range": {
            "timestamp": {
              "gte": "2023-05-04",
               "lte": "2023-06-05"
            }
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 25, 2023, 4:53pm UTC](https://discuss.elastic.co/t/join-two-searches-with-nested-field/339213/2 "2023-07-25T16:53:07Z")

</div>

May be something like:

```auto
GET /user-data/_search
{
 "query": {
    "bool": {
     "must": [
{
    "nested": {
      "path": "company",
      "query": {
            "match": { "company.id": 1 }
        }
      }
    }
  },
        { 
          "range": {
            "timestamp": {
              "gte": "2023-05-04",
               "lte": "2023-06-05"
            }
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Murilo\_Livorato](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/murilo_livorato/32/123848_2.png) [@Murilo\_Livorato](https://discuss.elastic.co/u/Murilo_Livorato)\
**Post date:** [July 25, 2023, 5:23pm UTC](https://discuss.elastic.co/t/join-two-searches-with-nested-field/339213/4 "2023-07-25T17:23:05Z")

</div>

Thanks

---

<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 22, 2023, 5:23pm UTC](https://discuss.elastic.co/t/join-two-searches-with-nested-field/339213/5 "2023-08-22T17:23:27Z")

</div>

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