# Tips for combining nested with unnested behaviour

**URL:** <https://discuss.elastic.co/t/tips-for-combining-nested-with-unnested-behaviour/179792>\
**Category:** Elasticsearch\
**Created:** [May 6, 2019, 3:02pm UTC](https://discuss.elastic.co/t/tips-for-combining-nested-with-unnested-behaviour/179792 "2019-05-06T15:02:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cherry-wave](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cherry-wave/32/32696_2.png) [@cherry-wave](https://discuss.elastic.co/u/cherry-wave)\
**Post date:** [May 6, 2019, 3:02pm UTC](https://discuss.elastic.co/t/tips-for-combining-nested-with-unnested-behaviour/179792/1 "2019-05-06T15:02:28Z")

</div>

I think I have a very specific problem here and I was just looking for some data constellations/queries to might help =) Btw. still using ES 5.6.4 here =/

I decided to use nested document because we have multiple variations of entries but tagged with "owners" to decide who can see which documents:

```
DELETE test

PUT /test
{
    "mappings": {
        "entry" : {
            "properties" : {
                "variations" : {
                    "type" : "nested"
                }
            }
        }
    }
}

POST test/entry
{
  "variations": [
    {
      "owner": "group",
      "data": {
        "firstName": "John",
        "lastName": "Doe"
      }
    },
    {
      "owner": "user1",
      "data": {
        "lastName": "Shepard"
      }
    }
  ]
}

GET test/_search
{
  "query": {
    "nested": {
      "path": "variations",
      "query": {
        "bool": {
          "must": [
            {
              "bool": {
                "should": [
                  {
                    "terms": {
                      "variations.owner": [
                        "group",
                        "user1"
                      ]
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

```

The problem is now, if I do a must search on multiple fields, of course elastic search treats the variations as separated documents and won't return the document if the following search is executed (I removed the owner-bool for readability):

```
GET test/_search
{
  "query": {
    "nested": {
      "path": "variations",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "variations.data.firstname": "John"
              }
            },
            {
              "match": {
                "variations.data.lastName": "Shepard"
              }
            }
          ]
        }
      }
    }
  }
}

```

But now, after reading the documentation of nested mapping again, I am totally unsure if this is even in any case the right thing to use when I need to have an owner per "variation"?

---

<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 3, 2019, 3:02pm UTC](https://discuss.elastic.co/t/tips-for-combining-nested-with-unnested-behaviour/179792/2 "2019-06-03T15:02:31Z")

</div>

Closed on June 3, 2019, 3:02pm UTC

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