您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

elasticsearch-将嵌套字段与文档中的另一个字段进行比较

elasticsearch-将嵌套字段与文档中的另一个字段进行比较

对于嵌套搜索,您要搜索没有父对象的嵌套对象。不幸的是,没有可以与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"
}

可以 通过在objects上循环来以低于性能的方式执行此操作(而不是天生就让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过滤器最有可能胜过没有过滤器的情况。

其他 2022/1/1 18:19:06 有556人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶