# How to get all nested docs by doc values

**URL:** <https://discuss.elastic.co/t/how-to-get-all-nested-docs-by-doc-values/59951>\
**Category:** Elasticsearch\
**Created:** [September 7, 2016, 8:48am UTC](https://discuss.elastic.co/t/how-to-get-all-nested-docs-by-doc-values/59951 "2016-09-07T08:48:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdgiauzn](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@jdgiauzn](https://discuss.elastic.co/u/jdgiauzn)\
**Post date:** [September 7, 2016, 8:48am UTC](https://discuss.elastic.co/t/how-to-get-all-nested-docs-by-doc-values/59951/1 "2016-09-07T08:48:43Z")

</div>

Hi, all, i have mapping like this: `{id: 1, name: 'x', types: [{price: 1, status: true}, {price: 2, status: false}]}`. types is nested.

Now, i want to custom score with script, in the script, i can use `_source['types']` to get all types as array, but i can't use `doc['types']` to get all types, how to do ?

thanks

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [September 7, 2016, 9:15am UTC](https://discuss.elastic.co/t/how-to-get-all-nested-docs-by-doc-values/59951/2 "2016-09-07T09:15:13Z")

</div>

The reason you can't do `doc['types']` is that there is not a field int he index called `types`.

This might take a bit to explain so bear with me.

When you index a document with nested objects the document is split into multiple documents in the index, so if you index the following document:

```auto
PUT /my_index/blogpost/1
{
  "title": "Nest eggs",
  "body": "Making your money work...",
  "tags": ["cash", "shares"],
  "comments": [ 
    {
      "name": "John Smith",
      "comment": "Great article",
      "age": 28,
      "stars": 4,
      "date": "2014-09-01"
    },
    {
      "name": "Alice White",
      "comment": "More like this please",
      "age": 31,
      "stars": 5,
      "date": "2014-10-22"
    }
  ]
}

```

In the index it conceptually looks like this:

```auto
# First nested object
{ 
  "comments.name": [john, smith],
  "comments.comment": [article, great],
  "comments.age": [28],
  "comments.stars": [4],
  "comments.date": [2014-09-01]
}
# Second nested object
{ 
  "comments.name": [alice, white],
  "comments.comment": [like, more, please, this],
  "comments.age": [31],
  "comments.stars": [5],
  "comments.date": [2014-10-22]
}
# The root or parent document
{ 
  "title": [eggs, nest],
  "body": [making, money, work, your],
  "tags": [cash, shares]
}

```

So the nested objects in `comments` are split out into their own special documents which reside next to the root document. This is what makes nested operations work.

So as you can see, neither the root document or the nested documents have a field called `comments` but the nested documents do have fields called `comments.name`, `comments.comment`, `comments.age` etc.

In order to get the DocValues value for your nested documents you will need to use the specific fields inside your `types` objects in your custom score script (i.e. `doc['types.price']` or `doc['status']` in your example doc above)

you can read more about nested objects in this section of the Elasticsearch: The Definitive Guide book: [https://www.elastic.co/guide/en/elasticsearch/guide/2.x/nested-objects.html](https://www.elastic.co/guide/en/elasticsearch/guide/2.x/nested-objects.html)

---

<div class="post-metadata">

**Author:** ![jdgiauzn](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@jdgiauzn](https://discuss.elastic.co/u/jdgiauzn)\
**Post date:** [September 7, 2016, 9:33am UTC](https://discuss.elastic.co/t/how-to-get-all-nested-docs-by-doc-values/59951/3 "2016-09-07T09:33:33Z")

</div>

Thanks very much for the reply.

I had tried `doc['types.price']` before this thread. it gave me empty content, nothing.

I dont know why

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [September 7, 2016, 9:34am UTC](https://discuss.elastic.co/t/how-to-get-all-nested-docs-by-doc-values/59951/4 "2016-09-07T09:34:27Z")

</div>

Can you paste the request and script that uses `doc['types.price']` that you are trying to run?

---

<div class="post-metadata">

**Author:** ![jdgiauzn](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@jdgiauzn](https://discuss.elastic.co/u/jdgiauzn)\
**Post date:** [September 7, 2016, 1:28pm UTC](https://discuss.elastic.co/t/how-to-get-all-nested-docs-by-doc-values/59951/5 "2016-09-07T13:28:07Z")

</div>

@colings86 Hi, check this [http://sense.qbox.io/gist/393ab3e0b2a1b945f15c24ad4b75b8d17fd4be84](http://sense.qbox.io/gist/393ab3e0b2a1b945f15c24ad4b75b8d17fd4be84)

thanks

---

<div class="post-metadata">

**Author:** ![jdgiauzn](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@jdgiauzn](https://discuss.elastic.co/u/jdgiauzn)\
**Post date:** [September 7, 2016, 1:30pm UTC](https://discuss.elastic.co/t/how-to-get-all-nested-docs-by-doc-values/59951/6 "2016-09-07T13:30:27Z")

</div>

Elasticsearch version: 2.2.1

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [September 7, 2016, 1:45pm UTC](https://discuss.elastic.co/t/how-to-get-all-nested-docs-by-doc-values/59951/7 "2016-09-07T13:45:15Z")

</div>

Ok, because you are trying to sort on nested documents you will need to use the `nested_path` option. Information on how to use it including an example can be found here: [https://www.elastic.co/guide/en/elasticsearch/reference/2.4/search-request-sort.html#nested-sorting](https://www.elastic.co/guide/en/elasticsearch/reference/2.4/search-request-sort.html#nested-sorting)

Sorry I didn't mention this earlier, it took me a little too long to understand what you are doing 🙂

---

<div class="post-metadata">

**Author:** ![jdgiauzn](https://avatars.discourse-cdn.com/v4/letter/j/898d66/32.png) [@jdgiauzn](https://discuss.elastic.co/u/jdgiauzn)\
**Post date:** [September 8, 2016, 9:44am UTC](https://discuss.elastic.co/t/how-to-get-all-nested-docs-by-doc-values/59951/8 "2016-09-08T09:44:14Z")

</div>

It is great, I dont even know we could use that in custom script sort.

Thank you

---

<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 5, 2017, 10:21pm UTC](https://discuss.elastic.co/t/how-to-get-all-nested-docs-by-doc-values/59951/9 "2017-07-05T22:21:51Z")

</div>


