# Need inverted nested mapping

**URL:** <https://discuss.elastic.co/t/need-inverted-nested-mapping/288936>\
**Category:** Elasticsearch\
**Created:** [November 10, 2021, 11:40pm UTC](https://discuss.elastic.co/t/need-inverted-nested-mapping/288936 "2021-11-10T23:40:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![elastic\_yogi](https://avatars.discourse-cdn.com/v4/letter/e/71c47a/32.png) [@elastic\_yogi](https://discuss.elastic.co/u/elastic_yogi)\
**Post date:** [November 10, 2021, 11:40pm UTC](https://discuss.elastic.co/t/need-inverted-nested-mapping/288936/1 "2021-11-10T23:40:40Z")

</div>

How do I get search results that have the nested relationships inverted? Do I need to create another index with the nested relationships inverted? Or can I do this in another way with a different type of query and/or modifying the current index?

e.g. our index:

```auto
Doc1:
  id: 1
  someText: "a"
  someField1: [val1:{somefield2: "1"}, val2: {somefield2: "2"}, val3: {somefield2: "3"}]
Doc2:
  id: 2
  someText: "a"
  someField1: [val1:{somefield2: "1"}]
Doc3:
  id: 3
  someText: ""
  someField1: []

```

If I search for someText="a", we get the document ID's of Doc1,Doc2 (1,2), and then query postgres, our consistent data store, for subfields.

However, now I want to search for someText="a" and instead of getting the document ID's, I want to get all the val's associated with those docs, which are val1, val2, val3.

e.g. search for someText = "a" should return:

```auto
val1:
  doc1, doc2
val2:
  doc1

```

Should I create another index with this relationship inverted? The index would be:

```auto
val1:
  {somefield2: "1"}
  [doc1: {someText: "a"}, doc2: {someText: "a"}]
val2:
  {somefield2: "2"}
  [doc1: {someText: "a"}]
val3:
  {somefield2: "3"}
  [doc1: {someText: ""}]

```

Would something like a [reverse nested aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-reverse-nested-aggregation.html) work?

---

<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:** [December 8, 2021, 11:40pm UTC](https://discuss.elastic.co/t/need-inverted-nested-mapping/288936/2 "2021-12-08T23:40:51Z")

</div>

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