# Access nested field inside function query with script score

**URL:** <https://discuss.elastic.co/t/access-nested-field-inside-function-query-with-script-score/19484>\
**Category:** Elasticsearch\
**Created:** [August 27, 2014, 7:54am UTC](https://discuss.elastic.co/t/access-nested-field-inside-function-query-with-script-score/19484 "2014-08-27T07:54:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Srinivasan\_Ramaswamy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/srinivasan_ramaswamy/32/74106_2.png) [@Srinivasan\_Ramaswamy](https://discuss.elastic.co/u/Srinivasan_Ramaswamy)\
**Post date:** [August 27, 2014, 7:54am UTC](https://discuss.elastic.co/t/access-nested-field-inside-function-query-with-script-score/19484/1 "2014-08-27T07:54:25Z")

</div>

I have the following document schema

authorid: 1  
athorName: joshua bloch  
books: [  
{  
bookId: 100  
bookName: effective java  
sales: 200  
},  
{  
bookId: 101  
bookName: Java Puzzlers  
sales: 100  
}  
]  
sellerTags: ["java book", "effective java", "joshua bloch"]

'books' is a nested field in the above schema. I am trying to access that  
nested field in a script inside a function query, but something is going  
wrong.

{  
"explain": true,  
"query": {  
"function\_score": {  
"boost\_mode": "replace",  
"query": {  
"match": { "sellerTags":"java book"}  
},  
"script\_score": {  
"params": {  
"param1": 2  
},  
"script": "doc['books.sales'].value \* param1 "  
}  
}  
}  
}

Though each document returned has sales in the index, I get a score of 0  
for both the documents i have (I have got one more document that matches  
the query). If it is helpful here is a part of explain string that shows  
that script is making the score as 0

"details": [  
{  
"value": 0,  
"description": "script score function: composed  
of:",  
"details": [  
{  
"value": 3.5981894,  
"description": "sum of:",  
"details": [...PerFieldSimilarity...]  
} ]

Can some one point out the mistake in my query. Why do i get the score as 0  
and how to fix it ?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/e9fd5575-cd66-4fe0-a788-b6c9b1e98ed0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/e9fd5575-cd66-4fe0-a788-b6c9b1e98ed0%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Srinivasan\_Ramaswamy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/srinivasan_ramaswamy/32/74106_2.png) [@Srinivasan\_Ramaswamy](https://discuss.elastic.co/u/Srinivasan_Ramaswamy)\
**Post date:** [August 27, 2014, 4:25pm UTC](https://discuss.elastic.co/t/access-nested-field-inside-function-query-with-script-score/19484/2 "2014-08-27T16:25:26Z")

</div>

I am able to access fields at the document level, the problem is only when  
i try to access nested fields with doc['nested\_field'] in the script.  
Anyone has a solution for this ?

On Wednesday, August 27, 2014 12:54:26 AM UTC-7, Srinivasan Ramaswamy wrote:

> I have the following document schema
> 
> authorid: 1  
> athorName: joshua bloch  
> books: [  
> {  
> bookId: 100  
> bookName: effective java  
> sales: 200  
> },  
> {  
> bookId: 101  
> bookName: Java Puzzlers  
> sales: 100  
> }  
> ]  
> sellerTags: ["java book", "effective java", "joshua bloch"]
> 
> 'books' is a nested field in the above schema. I am trying to access that  
> nested field in a script inside a function query, but something is going  
> wrong.
> 
> {  
> "explain": true,  
> "query": {  
> "function\_score": {  
> "boost\_mode": "replace",  
> "query": {  
> "match": { "sellerTags":"java book"}  
> },  
> "script\_score": {  
> "params": {  
> "param1": 2  
> },  
> "script": "doc['books.sales'].value \* param1 "  
> }  
> }  
> }  
> }
> 
> Though each document returned has sales in the index, I get a score of 0  
> for both the documents i have (I have got one more document that matches  
> the query). If it is helpful here is a part of explain string that shows  
> that script is making the score as 0
> 
> "details": [  
> {  
> "value": 0,  
> "description": "script score function: composed  
> of:",  
> "details": [  
> {  
> "value": 3.5981894,  
> "description": "sum of:",  
> "details": [...PerFieldSimilarity...]  
> } ]
> 
> Can some one point out the mistake in my query. Why do i get the score as  
> 0 and how to fix it ?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/6137afb4-4dcf-4a89-ae3b-ef1278970153%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6137afb4-4dcf-4a89-ae3b-ef1278970153%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Srinivasan\_Ramaswamy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/srinivasan_ramaswamy/32/74106_2.png) [@Srinivasan\_Ramaswamy](https://discuss.elastic.co/u/Srinivasan_Ramaswamy)\
**Post date:** [August 28, 2014, 2:51am UTC](https://discuss.elastic.co/t/access-nested-field-inside-function-query-with-script-score/19484/3 "2014-08-28T02:51:56Z")

</div>

It turns out, I need to set a property "include\_in\_parent": true in the  
mapping to access the field in the script.

"books": {  
"type": "nested",  
"include\_in\_parent": true,  
"properties": { ..... }  
}

On Wednesday, August 27, 2014 9:25:26 AM UTC-7, Srinivasan Ramaswamy wrote:

> I am able to access fields at the document level, the problem is only when  
> i try to access nested fields with doc['nested\_field'] in the script.  
> Anyone has a solution for this ?
> 
> On Wednesday, August 27, 2014 12:54:26 AM UTC-7, Srinivasan Ramaswamy  
> wrote:
> 
> > I have the following document schema
> > 
> > authorid: 1  
> > athorName: joshua bloch  
> > books: [  
> > {  
> > bookId: 100  
> > bookName: effective java  
> > sales: 200  
> > },  
> > {  
> > bookId: 101  
> > bookName: Java Puzzlers  
> > sales: 100  
> > }  
> > ]  
> > sellerTags: ["java book", "effective java", "joshua bloch"]
> > 
> > 'books' is a nested field in the above schema. I am trying to access that  
> > nested field in a script inside a function query, but something is going  
> > wrong.
> > 
> > {  
> > "explain": true,  
> > "query": {  
> > "function\_score": {  
> > "boost\_mode": "replace",  
> > "query": {  
> > "match": { "sellerTags":"java book"}  
> > },  
> > "script\_score": {  
> > "params": {  
> > "param1": 2  
> > },  
> > "script": "doc['books.sales'].value \* param1 "  
> > }  
> > }  
> > }  
> > }
> > 
> > Though each document returned has sales in the index, I get a score of 0  
> > for both the documents i have (I have got one more document that matches  
> > the query). If it is helpful here is a part of explain string that shows  
> > that script is making the score as 0
> > 
> > "details": [  
> > {  
> > "value": 0,  
> > "description": "script score function:  
> > composed of:",  
> > "details": [  
> > {  
> > "value": 3.5981894,  
> > "description": "sum of:",  
> > "details": [...PerFieldSimilarity...]  
> > } ]
> > 
> > Can some one point out the mistake in my query. Why do i get the score as  
> > 0 and how to fix it ?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/1db5847d-8169-426f-bc0c-8a5c289de87b%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1db5847d-8169-426f-bc0c-8a5c289de87b%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 1:05am UTC](https://discuss.elastic.co/t/access-nested-field-inside-function-query-with-script-score/19484/4 "2017-07-06T01:05:53Z")

</div>


