Access nested field inside function query with script score


(Srinivasan Ramaswamy) #1

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/e9fd5575-cd66-4fe0-a788-b6c9b1e98ed0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(Srinivasan Ramaswamy) #2

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/6137afb4-4dcf-4a89-ae3b-ef1278970153%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(Srinivasan Ramaswamy) #3

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/1db5847d-8169-426f-bc0c-8a5c289de87b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(system) #4