对于嵌套搜索,您要搜索没有父对象的嵌套对象。不幸的是,没有可以与nested
对象一起应用的隐藏联接。
至少在当前,这意味着您不会在脚本中同时收到“父”文档和嵌套文档。您可以通过以下两种方式替换脚本并测试结果来确认这一点:
# Parent Document does not exist
"script": {
"script": "doc['primary_content_type_id'].value == 12"
}
# Nested Document should exist
"script": {
"script": "doc['content.content_type_id'].value == 12"
}
您 可以 通过在object
s上循环来以低于性能的方式执行此操作(而不是天生就让ES使用来为您执行此操作nested
)。这意味着您必须将文档和nested
文档重新索引为单个文档才能正常工作。考虑到您尝试使用它的方式,这可能并没有太大不同,甚至可能会表现得更好(特别是在缺少替代方法的情况下)。
# This assumes that your default scripting language is Groovy (default in 1.4)
# Note1: "find" will loop across all of the values, but it will
# appropriately short circuit if it finds any!
# Note2: It would be preferable to use doc throughout, but since we need the
# arrays (plural!) to be in the _same_ order, then we need to parse the
# _source. This inherently means that you must _store_ the _source, which
# is the default. Parsing the _source only happens on the first touch.
"script": {
"script": "_source.content.find { it.content_type_id == _source.primary_content_type_id && ! it.assigned } != null",
"_cache" : true
}
我缓存的结果,因为没有动态发生在这里(例如,不比较日期Now
为实例),所以它是很安全的高速缓存,从而使未来的查找 多 快。默认情况下,大多数过滤器都是缓存的,但是脚本是少数例外之一。
由于 必须 比较两个值以确保找到正确的内部对象,因此您正在重复 一些 工作,但这实际上是不可避免的。拥有term
过滤器最有可能胜过没有过滤器的情况。