# How to express the logic to let nested field match multiple groups condition

**URL:** <https://discuss.elastic.co/t/how-to-express-the-logic-to-let-nested-field-match-multiple-groups-condition/307614>\
**Category:** Elasticsearch\
**Created:** [June 20, 2022, 7:27am UTC](https://discuss.elastic.co/t/how-to-express-the-logic-to-let-nested-field-match-multiple-groups-condition/307614 "2022-06-20T07:27:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bruce\_Zu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bruce_zu/32/107211_2.png) [@Bruce\_Zu](https://discuss.elastic.co/u/Bruce_Zu)\
**Post date:** [June 20, 2022, 7:27am UTC](https://discuss.elastic.co/t/how-to-express-the-logic-to-let-nested-field-match-multiple-groups-condition/307614/1 "2022-06-20T07:27:25Z")

</div>

Elasticsearch version 6.8.0

mappings like

```auto
{
  "properties": {
    "id": {
      "type": "long"
    },
    "authentication_worker": {
      "type": "nested",
      "properties": {
        "authorized_id": {
          "type": "long"
        },
        "authorized_role": {
          "type": "keyword"
        },
        "authorized_type": {
          "type": "long"
        }
      }
    }
  }
}

```

a document example

```auto
   "id":123000,
   "authentication_worker":[
      {
         "authorized_id":0001,
         "authorized_role":"customer-agency-team",
         "authorized_type":1
      },
      {
         "authorized_id":0002,
         "authorized_role":"customer-agency-team",
         "authorized_type":1
      },
      {
         "authorized_id":1001,
         "authorized_role":"developer",
         "authorized_type":0
      },
      {
         "authorized_id":1002,
         "authorized_role":"developer",
         "authorized_type":0
      },
      {
         "authorized_id":2001,
         "authorized_role":"manager",
         "authorized_type":0
      },
      {
         "authorized_id":2002,
         "authorized_role":"manager",
         "authorized_type":0
      },
            {
         "authorized_id":3001,
         "authorized_role":"QA",
         "authorized_type":0
      },
      {
         "authorized_id":3002,
         "authorized_role":"QA",
         "authorized_type":0
      },
   ],

```

"authentication\_worker" is a nested field.Firstly, I query the index with some user provided basic conditions. For this part I use `should` of a bool query to implement it, it works well, here for simple I just use `match_all` to represent it. Thus we focus on the scond part where I need to narrow the hits with some user provided logic, it can be 0 or at most 4 logic condition, each of them will apply to the nested authentication\_worker field

1.Given a list of authorized\_id. A document will be a hit if there is at least one record of its nested `authentication_worker field` match

```auto
(authentication_worker.authorized_type==0) &&  
(authentication_worker.authorized_id in the given list) 

```

2.Given a list of authorized\_id. A document will be a hit if there is at least one record of its nested `authentication_worker field` match

```auto
(authentication_worker.authorized_type==0) && 
(authentication_worker.authorized_role=="developer") &&
(authentication_worker.authorized_id in the given list)

```

1. Given a list of authorized\_id. A document will be a hit if there is at least one record of its nested `authentication_worker field` match

```auto
(authentication_worker.authorized_type==0) && 
(authentication_worker.authorized_role=="manager") &&
(authentication_worker.authorized_id in the given list)

```

4.Given a list of authorized\_id. A document will be a hit if there is at least one record of its nested `authentication_worker field` match

```auto
(authentication_worker.authorized_type==0) && 
(authentication_worker.authorized_role=="QA") &&
(authentication_worker.authorized_id in the given list)

```

I use `filter` to do it with the same bool query

```auto
"query": {
    "bool" : {
        "must" : {
        "match_all": {}
        },
        "filter": {
          // focus here
        }
    }
}

```

The issue here is, any one of the 4 condition works well if I only use it only. But use using any 2 or more of them, I will get empty hits.

e.g.

```auto
{
  "bool": {
    "must" : {
        "match_all": {}
    },
    "filter": [
      {
        "nested": {
          "query": {
            "bool": {
              "must": [
                {
                  "bool": {
                    "must": [
                      {
                        "term": {
                          "authentication_worker.authorized_type": {
                            "value": 0 
                          }
                        }
                      },
                      {
                        "terms": {
                          "authentication_worker.authorized_id": [
                            1001,
                            1002,
                            1003
                          ] 
                        }
                      },
                      {
                        "term": {
                          "authentication_worker.authorized_role": {
                            "value": "developer" 
                          }
                        }
                      }
                    ]  
                  }
                },
                {
                  "bool": {
                    "must": [
                      {
                        "term": {
                          "authentication_worker.authorized_type": {
                            "value": 0 
                          }
                        }
                      },
                      {
                        "terms": {
                          "authentication_worker.authorized_id": [
                            3001,
                            3002,
                            3003
                          ] 
                        }
                      },
                      {
                        "term": {
                          "authentication_worker.authorized_role": {
                            "value": "QA" 
                          }
                        }
                      }
                    ] 
                  }
                }
              ] 
            }
          },
          "path": "authentication_worker",
        }
      }
    ]
  }
}

```

Expected : the above example document with "id":123000 should return in hits result.  
Actual hits: empty.

I expect Elasticsearch to run each 3-'must' in an atomic group. In total, there are 2 groups. The returned document should match both of them.

Note, both group use the same field with different values. Seems Elasticsearch run all of 6 `must` together and can not figure out the logic?  
If so, how I should express my logic to let Elasticsearch understand it correctly?

Thank you!

[How to express the logic to let nested field match multiple groups condition](https://discuss.elastic.co/t/how-to-express-the-logic-to-let-nested-field-match-multiple-groups-condition/307614)

---

<div class="post-metadata">

**Author:** ![Bruce\_Zu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bruce_zu/32/107211_2.png) [@Bruce\_Zu](https://discuss.elastic.co/u/Bruce_Zu)\
**Post date:** [June 21, 2022, 7:11am UTC](https://discuss.elastic.co/t/how-to-express-the-logic-to-let-nested-field-match-multiple-groups-condition/307614/2 "2022-06-21T07:11:54Z")

</div>

Fixed by: move the outer layer multi must out of nested query.

---

<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 19, 2022, 7:12am UTC](https://discuss.elastic.co/t/how-to-express-the-logic-to-let-nested-field-match-multiple-groups-condition/307614/3 "2022-07-19T07:12:20Z")

</div>

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