不幸的是,您不能从根访问嵌套上下文,也不能从嵌套访问根上下文,因为嵌套文档是单独的文档,即使它们存储在父级附近。但是您可以使用 copy_to 字段功能通过不同的映射来解决它。这是一个映射:
{
"mappings": {
"sample": {
"properties": {
"printings": {
"type": "nested",
"properties": {
"prop2": {
"type": "integer",
"copy_to": "child_prop2"
}
}
},
"prop1": {
"type": "integer"
},
"child_prop2": {
"type": "integer"
}
}
}
}
}
在这种情况下,嵌套文档中的值将被复制到父级。您不必显式填写这个新字段,这里是批量索引的 ax 示例:
POST http://localhost:9200/_bulk HTTP/1.1
{"index":{"_index":"my_index","_type":"sample","_id":null}}
{"printings":[{"prop2":1},{"prop2":4}],"prop1":2}
{"index":{"_index":"my_index","_type":"sample","_id":null}}
{"printings":[{"prop2":0},{"prop2":1}],"prop1":2}
{"index":{"_index":"my_index","_type":"sample","_id":null}}
{"printings":[{"prop2":1},{"prop2":0}],"prop1":2}
之后你就可以使用这个查询了
{
"query": {
"script": {
"script": {
"inline": "doc['prop1'].value > (3 * doc['child_prop2'].value)",
"lang": "painless"
}
}
}
}
第一个文档不匹配。第二个将与第一个子文档匹配。第三个将与第二个子文档匹配。