# How to retrieve a field value from inner\_hits filtered object

**URL:** <https://discuss.elastic.co/t/how-to-retrieve-a-field-value-from-inner-hits-filtered-object/63813>\
**Category:** Elasticsearch\
**Created:** [October 25, 2016, 12:50am UTC](https://discuss.elastic.co/t/how-to-retrieve-a-field-value-from-inner-hits-filtered-object/63813 "2016-10-25T00:50:50Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![Elasticsircher](https://avatars.discourse-cdn.com/v4/letter/e/7ab992/32.png) [@Elasticsircher](https://discuss.elastic.co/u/Elasticsircher)\
**Post date:** [June 15, 2017, 9:34pm UTC](https://discuss.elastic.co/t/how-to-retrieve-a-field-value-from-inner-hits-filtered-object/63813/3 "2017-06-15T21:34:42Z")

</div>

I was struggling with this as well as I needed to boost my query score as a function of a field within inner hits. Per the following post, inner hits are not accessible within scripts:

> [@Access to inner\_hits from painless script during sorting](https://discuss.elastic.co/t/access-to-inner-hits-from-painless-script-during-sorting/86926):
>
> 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" : "firstNa…

Bummer.

However, I managed to find a solution to my problem by using a nested query with a function score query inside it computing the score contribution of the nested hit.

For example, if you have the following mapping:

```auto
{
    "properties": {
        "field": {
            "type": "nested",
            "properties": {
                 "nested_field": {
                     "type": "text"
                 },
                 "nested_field_score": {
                     "type": "float"
                 }
            }
        }
    }
}

```

Then you can do the following nested query:

```auto
{
    "query": {
        "nested": {
            "path": "field",
            "query": {
                "function_score": {
                    "query": {
                        "match": {
                            "field.nested_field": "<match text>"
                        }
                    }
                },
                "script_score": {
                    "script": "doc['field.nested_field_score'].value"
                }
            }
        }
    }
}

```

You can then wrap this in a bool query and match non-nested fields along-side nested fields, each contributing a computed score.

This doesn't solve all issues with not having access to inner hits but worked well in my case.

---

_[View the full topic](https://discuss.elastic.co/t/how-to-retrieve-a-field-value-from-inner-hits-filtered-object/63813)._
