# Elasticsearch query Combine 2 queries or Elastic search Join Query

**URL:** <https://discuss.elastic.co/t/elasticsearch-query-combine-2-queries-or-elastic-search-join-query/305805>\
**Category:** Elasticsearch\
**Created:** [May 27, 2022, 1:30pm UTC](https://discuss.elastic.co/t/elasticsearch-query-combine-2-queries-or-elastic-search-join-query/305805 "2022-05-27T13:30:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vipin.meshram13098](https://avatars.discourse-cdn.com/v4/letter/v/bbe5ce/32.png) [@vipin.meshram13098](https://discuss.elastic.co/u/vipin.meshram13098)\
**Post date:** [May 27, 2022, 1:30pm UTC](https://discuss.elastic.co/t/elasticsearch-query-combine-2-queries-or-elastic-search-join-query/305805/1 "2022-05-27T13:30:39Z")

</div>

This is my one elastic document example:

```auto
"_source": {
    "leg_b":{
            "team_name" : "aaaa",
            "channel_uuid" : "12121"
    },  
    "wrapup":[
      "0":{
            "name": "sss",
            "channel_uuid": "12121"
          },
      "1":{
            "name": "bbb",
            "channel_uuid" : "21212"
          },
      "2":{
            "name": "acbs",
            "channel_uuid" : "111211"
          }
     ]
}

```

I have to perform search on "leg\_b.team\_name" value and find out its "channel\_uuid" value on the basis of leg\_b's "channel\_uuid",NEXT I will search in wrapup object for common "channel\_uuid" values in the Same Query .

**Example ,** lets consider above document, First I will searchon leg\_b.team\_name = "aaaa" after that I will take leg\_b.channel\_uuid = "12121"and search on wrapup object channel\_uuid.

So it should return,

```auto
"_source": {
"leg_b":{
        "team_name" : "aaaa",
        "channel_uuid" : "12121"
},  
"wrapup":[
  "0":{
        "name": "sss",
        "channel_uuid": "12121"
      },
  "1":{
        "name": "bbb",
        "channel_uuid" : "21212"
      }
      ]
}

```

_Please suggest some approach, Thank you._

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [May 28, 2022, 8:24pm UTC](https://discuss.elastic.co/t/elasticsearch-query-combine-2-queries-or-elastic-search-join-query/305805/2 "2022-05-28T20:24:25Z")

</div>

Hi!

I would get the docs by the "team\_name" field and my application would filter the channel\_uuid in the wrapup.

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [May 29, 2022, 12:15am UTC](https://discuss.elastic.co/t/elasticsearch-query-combine-2-queries-or-elastic-search-join-query/305805/3 "2022-05-29T00:15:35Z")

</div>

Is there any problem with filtering the nested wrapup documents by client side?

Or one possible way is to adda new "same\_channnel\_uuid\_as\_leg\_b" field in wrapup objects and use nested query on that field. With this strategy, if a document has no appropriate wrapup object, the document itself drops from the search result. If you also need such documents, you have to use boolean queries not to drop them (something like `should` clause with `minimum_should_match: 0` and `inner_hits` parameters.

---

<div class="post-metadata">

**Author:** ![vipin.meshram13098](https://avatars.discourse-cdn.com/v4/letter/v/bbe5ce/32.png) [@vipin.meshram13098](https://discuss.elastic.co/u/vipin.meshram13098)\
**Post date:** [May 30, 2022, 5:38am UTC](https://discuss.elastic.co/t/elasticsearch-query-combine-2-queries-or-elastic-search-join-query/305805/4 "2022-05-30T05:38:42Z")

</div>

@Tomo_M ,  
Minimum Should Match is another search technique that allows you to conduct a more controlled search on related or co-occurring topics by specifying the number of search terms or phrases in the query that should occur within the records returned.

but in my case I need all the wrapup.Channel\_uuid records which is common in wrapup object. So, these all should return me in one query.

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [May 30, 2022, 6:25am UTC](https://discuss.elastic.co/t/elasticsearch-query-combine-2-queries-or-elastic-search-join-query/305805/5 "2022-05-30T06:25:58Z")

</div>

My first question was why you need to get such return in one query. I hope you will answer this question.

My intention about `minimum_should_match:0` was:

```auto
PUT test_filter_nested
{
  "mappings": {
    "properties": {
      "wrapup":{
        "type":"nested",
        "properties": {
          "same_channnel_uuid_as_leg_b":{
            "type": "boolean"
          }
        }
      }
    }
  }
}

POST test_filter_nested/_doc
{
  "leg_b":{
            "team_name" : "aaaa",
            "channel_uuid" : "12121"
    },  
    "wrapup":[
      {
            "name": "sss",
            "channel_uuid": "12121",
            "same_channnel_uuid_as_leg_b": true
          },
      {
            "name": "bbb",
            "channel_uuid" : "12121",
            "same_channnel_uuid_as_leg_b": true
          },
      {
            "name": "acbs",
            "channel_uuid" : "111211",
            "same_channnel_uuid_as_leg_b": false
          }
     ]
}

POST test_filter_nested/_doc
{
  "leg_b":{
            "team_name" : "aaaa",
            "channel_uuid" : "12121"
    },  
    "wrapup":[
      {
            "name": "acbs",
            "channel_uuid" : "111211",
            "same_channnel_uuid_as_leg_b": false
          }
     ]
}

GET test_filter_nested/_search

GET test_filter_nested/_search?filter_path=hits.hits._source,hits.hits.inner_hits.wrapup.hits.hits._source
{
  "query":{
    "bool": {
      "must": [
        {"term": {
          "leg_b.team_name": {
            "value": "aaaa"
          }
        }}
      ], 
      "should":[
      {
        "nested": {
          "path": "wrapup",
          "query": {
            "term": {
              "wrapup.same_channnel_uuid_as_leg_b": {
                "value": true
              }
            }
          },
          "inner_hits": {}
        }
        
      }],
      "minimum_should_match": 0
    }
  },
  "_source":"leg_b"
}

```

Then you get:

```auto
{
  "hits" : {
    "hits" : [
      {
        "_source" : {
          "leg_b" : {
            "channel_uuid" : "12121",
            "team_name" : "aaaa"
          }
        },
        "inner_hits" : {
          "wrapup" : {
            "hits" : {
              "hits" : [
                {
                  "_source" : {
                    "name" : "sss",
                    "channel_uuid" : "12121",
                    "same_channnel_uuid_as_leg_b" : true
                  }
                },
                {
                  "_source" : {
                    "name" : "bbb",
                    "channel_uuid" : "12121",
                    "same_channnel_uuid_as_leg_b" : true
                  }
                }
              ]
            }
          }
        }
      },
      {
        "_source" : {
          "leg_b" : {
            "channel_uuid" : "12121",
            "team_name" : "aaaa"
          }
        }
      }
    ]
  }
}

```

This may be one of some approaches.

---

<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:** [June 27, 2022, 6:26am UTC](https://discuss.elastic.co/t/elasticsearch-query-combine-2-queries-or-elastic-search-join-query/305805/6 "2022-06-27T06:26:18Z")

</div>

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