# Access to inner\_hits from painless script during sorting

**URL:** <https://discuss.elastic.co/t/access-to-inner-hits-from-painless-script-during-sorting/86926>\
**Category:** Elasticsearch\
**Created:** [May 24, 2017, 8:55am UTC](https://discuss.elastic.co/t/access-to-inner-hits-from-painless-script-during-sorting/86926 "2017-05-24T08:55:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lcokru](https://avatars.discourse-cdn.com/v4/letter/l/bbce88/32.png) [@lcokru](https://discuss.elastic.co/u/lcokru)\
**Post date:** [May 24, 2017, 8:55am UTC](https://discuss.elastic.co/t/access-to-inner-hits-from-painless-script-during-sorting/86926/1 "2017-05-24T08:55:45Z")

</div>

I am using nested query in Elasticsearch search query. Something like this:  
"query": {  
"bool": {  
"must": [  
{  
"bool": {  
"should": [  
{  
"bool": {  
"should": [  
{  
"match": {  
"id": {  
"query": "department1"  
}  
}  
},  
{  
"match": {  
"id": {  
"query": "company1"  
}  
}  
}  
]  
}  
}  
]  
}  
},  
{  
"bool": {  
"should": [  
{  
"bool": {  
"should": [  
{  
"nested" : {  
"path" : "peoples",  
"ignore\_unmapped" : "true",  
"query" : {  
"bool" : {  
"must" : [  
{  
"match" : {  
"peoples.firstName" : "firstName12"  
}  
}  
]  
}  
},  
"inner\_hits": { }  
}  
},  
{  
"nested" : {  
"path" : "entities",  
"ignore\_unmapped" : "true",  
"query" : {  
"bool" : {  
"must" : [  
{  
"match" : {  
"entities.firstName" : "firstName13"  
}  
}  
]  
}  
},  
"inner\_hits": { }  
}  
}  
]  
}  
}  
]  
}  
}  
]  
}  
}

**And then I want to access to inner\_hits data from sorting script:**

```
  "sort": [
    {
      "_script": {
        "type": "string",
        "order": "asc",
        "script": {
          "lang": "painless",
          "inline": " **inner_hits**"
        }
      }
    }
  ]

```

I **s it possible?**

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [May 26, 2017, 7:49am UTC](https://discuss.elastic.co/t/access-to-inner-hits-from-painless-script-during-sorting/86926/2 "2017-05-26T07:49:58Z")

</div>

It is not possible, in any scripting language (not just painless). Inner hits are completely separate documents within lucene, but scripts operate on a single document at a time.

---

<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 23, 2017, 7:50am UTC](https://discuss.elastic.co/t/access-to-inner-hits-from-painless-script-during-sorting/86926/3 "2017-06-23T07:50:50Z")

</div>

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