上QQ阅读APP看书,第一时间看更新
How to do it...
If we take the order example that we have seen in the previous recipes, we can add an alias for the price value to cost in the item subfield.
This process can be achieved executing the following actions:
- To add this alias, we need to have a mapping that's similar to the following:
PUT test/_mapping
{
"properties": {
"id": {"type": "keyword"},
"date": {"type": "date"},
"customer_id": {"type": "keyword"},
"sent": {"type": "boolean"},
"item": {
"type": "object",
"properties": {
"name": {"type": "keyword"},
"quantity": {"type": "long"},
"cost": {
"type": "alias",
"path": "item.price"
},
"price": {"type": "double"},
"vat": {"type": "double"}
}
}
}
}
- We can now index a record as follows:
PUT test/_doc/1?refresh
{
"id": "1",
"date": "2018-11-16T20:07:45Z",
"customer_id": "100",
"sent": true,
"item": [
{
"name": "tshirt",
"quantity": 10,
"price": 4.3,
"vat": 8.5
}
]
}
- We can search it using the cost alias like so:
GET test/_search
{
"query": {
"term": {
"item.cost": 4.3
}
}
}
The result will be the saved document.