How to get all nested docs by doc values

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

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:

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:

# 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

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

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

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

thanks

Elasticsearch version: 2.2.1

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

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

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

Thank you