Skip to content

Instantly share code, notes, and snippets.

@junjun-zhang
Last active March 26, 2017 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junjun-zhang/b28c62f9273f6ba8a7073a33a95e20bf to your computer and use it in GitHub Desktop.
Save junjun-zhang/b28c62f9273f6ba8a7073a33a95e20bf to your computer and use it in GitHub Desktop.
Unable to retrieve field value from Doc Values in nested function_score query
# Elasticsearch version: 5.1.1
DELETE /test-store
PUT /test-store
PUT /test-store/transaction/_mapping
{
"transaction": {
"properties": {
"customer_id": {
"type": "text"
},
"reward_factor": {
"type": "float"
},
"product": {
"type": "nested",
"properties": {
"category": {
"type": "text"
},
"name": {
"type": "text"
},
"quantity": {
"type": "long"
},
"price": {
"type": "float"
}
}
}
}
}
}
PUT /test-store/transaction/1/_create
{
"customer_id": "customer-1",
"reward_factor": 1.1,
"product": [
{
"name": "product-1",
"category": "tech",
"price": 102.5,
"quantity": 3
},
{
"name": "product-2",
"category": "hardware",
"price": 550.82,
"quantity": 2
}
]
}
PUT /test-store/transaction/2/_create
{
"customer_id": "customer-2",
"reward_factor": 1.05,
"product": [
{
"name": "product-1",
"category": "tech",
"price": 72.5,
"quantity": 2
},
{
"name": "product-2",
"category": "hardware",
"price": 300.2,
"quantity": 1
}
]
}
# Query using function_score to sort transactions by 'reward_factor' adjusted purchase total.
# Result shows all transactions have a _score of '0', further investigation indicates
# "doc['reward_factor'].value" is always '0' which is incorrect
GET /test-store/transaction/_search
{
"query": {
"nested": {
"path": "product",
"score_mode": "sum",
"query": {
"function_score": {
"query": {
"match_all": {}
},
"boost_mode": "replace",
"script_score": {
"script": "doc['product.price'].value * doc['product.quantity'].value * doc['reward_factor'].value"
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment