如何过滤嵌套数组?

时间:2013-05-30 14:57:25

标签: arrays filter nested elasticsearch

我在ES中使用嵌套文档。我在一个实体中嵌套了数组,我希望能够使用嵌套查询过滤数组(不仅仅是过滤器根/父文档,还有嵌套数组)。

嵌套过滤器可以过滤根文档。例如,如果我有索引'博客'并输入'博客'并在博客文档中我有属性'comments'(评论数组), 映射中的{“type”:“nested”},例如,我想过滤那些评论标题为'today'的博客(例如匹配查询),但在每个博客中我都只想显示包含的评论'今天​​'的称号。

我知道如何嵌套过滤器。父子关系也不利于此目的,因为作为父文件的一部分,没有儿童文件作为回应。

有没有办法直接在elasticsearch中执行此操作,而无需向应用程序添加额外的逻辑,这可能会影响性能? 或者是否计划在下一版本或类似的东西中将其作为嵌套查询/过滤器中的新选项添加​​?

由于

...添加

例如,看看这个文档:

 .....
"blogs" :[
    {
         "id":1000,
         "content":"hello world!!!",
         "author:"evan",
         "createTime":"2013-05-31",
          "comments" : [
                   {"author":"jack", id:"1", "content":"test test","createTime":"2013-05-31"},
                   {"author":"tom",  id:"2", "content":"test test","createTime":"2013-05-30"},
                   {"author":"oliver", id:"3", "content":"test test","createTime":"2013-05-30"},
                   {"author":"Jonesie", id:"4", "content":"test test","createTime":"2013-05-29"}
           ]
    },
    {
         "id":1001,
         "content":"test data",
         "author:"jack",
         "createTime":"2013-05-30",
          "comments" : [
                   {"author":"Zach", id:"11", "content":"test data 1","createTime":"2013-05-31"},
                   {"author":"tom",  id:"21", "content":"test data 2","createTime":"2013-05-31"},
                   {"author":"oliver", id:"31", "content":"test test 3","createTime":"2013-05-28"},
                   {"author":"Jonesie", id:"41", "content":"test test 4","createTime":"2013-05-27"}
           ]
    }
]
.....

我想运行查询:

按blogs.createTime =='2013-05-31过滤'并过滤blogs.comments.createTime =='2013-05-31'

期待:

 .....
"blogs" :[
    {
         "id":1000
         "content":"hello world!!!",
         "author::"evan",
         "createTime":"2013-05-31"
          "comments" : [
                   {"author":"jack", id:"1", "content":"test test","createTime":"2013-05-31"}
           ]
    } 
]
....

感谢

1 个答案:

答案 0 :(得分:1)

简而言之:你做不到。您需要在自己的代码中过滤嵌套文档。

这是一个众所周知的问题:

“将匹配的嵌套文档公开给查询并排序” https://github.com/elasticsearch/elasticsearch/issues/1383

“每次点击返回匹配的嵌套内部对象。”

https://github.com/elasticsearch/elasticsearch/issues/3022