具有来自另一个文档的值的 Elasticsearch 脚本字段
Elasticsearch scripted field with value from another document
假设我有两种类型的文档:
- 客户 - 包含区域、语言环境等信息)
- 购买 - 包含有关个人购买的详细信息。
customers/purchases之间存在parent/child关系。
我需要生成一份报告,其中包含符合特定过滤条件的所有购买,但每一行还应包含一些来自客户文档的查找数据(例如:客户的区域设置)。我知道这样做的方法之一是首先将数据展平并将这些字段直接放入采购文档中。我想知道 ElasticSearch 是否有办法为我自动填充这些字段(也许是一些脚本化的字段查找魔法?)。
无法使用任何类型的脚本访问 parent/child
中的字段,因为它是完全不同的文档。从子上下文中访问父上下文会非常昂贵,反之亦然。
Inner hits 将满足您的需求:
PUT test
{
"mappings": {
"Customer": {},
"Purchase": {
"_parent": {
"type": "Customer"
}
}
}
}
PUT test/Customer/1
{
"firstName": "John",
"lastName": "Doe"
}
PUT test/Purchase/2?parent=1
{
"price": 100
}
GET test/Purchase/_search
{
"query":{
"bool":{
"must":[
{
"range":{
"price":{
"gte":10
}
}
},
{
"has_parent":{
"type":"Customer",
"inner_hits":{},
"query":{
"match_all":{}
}
}
}
]
}
}
}
假设我有两种类型的文档:
- 客户 - 包含区域、语言环境等信息)
- 购买 - 包含有关个人购买的详细信息。
customers/purchases之间存在parent/child关系。
我需要生成一份报告,其中包含符合特定过滤条件的所有购买,但每一行还应包含一些来自客户文档的查找数据(例如:客户的区域设置)。我知道这样做的方法之一是首先将数据展平并将这些字段直接放入采购文档中。我想知道 ElasticSearch 是否有办法为我自动填充这些字段(也许是一些脚本化的字段查找魔法?)。
无法使用任何类型的脚本访问 parent/child
中的字段,因为它是完全不同的文档。从子上下文中访问父上下文会非常昂贵,反之亦然。
Inner hits 将满足您的需求:
PUT test
{
"mappings": {
"Customer": {},
"Purchase": {
"_parent": {
"type": "Customer"
}
}
}
}
PUT test/Customer/1
{
"firstName": "John",
"lastName": "Doe"
}
PUT test/Purchase/2?parent=1
{
"price": 100
}
GET test/Purchase/_search
{
"query":{
"bool":{
"must":[
{
"range":{
"price":{
"gte":10
}
}
},
{
"has_parent":{
"type":"Customer",
"inner_hits":{},
"query":{
"match_all":{}
}
}
}
]
}
}
}