Solr查询不在嵌套的子文档上

时间:2016-01-18 17:36:15

标签: solr lucene nested

如何查询父文档,子文档具有某个字段值?

例如为: 我们假设我们有以下数据结构:

    {
        "type_s": "book",
        "id_l": 4294967298,
        "title_s": "The Little Mermaid"
        {
            "type_s": "review",
            "id_l": "4294967451",       
            "reviewer_s": "Freeman, Gordon",        
            "comment_s": "Great book!"      
        },
        {
            "type_s": "review",
            "id_l": "4294967452",       
            "reviewer_s": "Denton, J.C.",       
            "comment_s": "My daughter loved it!"        
        }
    },
{
        "type_s": "book",
        "id_l": 4294967298,
        "title_s": "Lion King"
        {
            "type_s": "review",
            "id_l": "4294967457",       
            "reviewer_s": "Woods, Susanne",     
            "comment_s": "One of the best!"     
        },
        {
            "type_s": "review",
            "id_l": "4294967458",       
            "reviewer_s": "Denver, Michel",     
            "comment_s": "Liked the ending!"        
        }
    },
    {
        "type_s": "book",
        "id_l": 4294967298,
        "title_s": "7 dwarves"
        {
            "type_s": "review",
            "id_l": "4294967453",       
            "reviewer_s": "Freeman, Gordon",        
            "comment_s": "Great book!"      
        },
        {
            "type_s": "review",
            "id_l": "4294967454",       
            "reviewer_s": "Delacroix, Marie",       
            "comment_s": "Too many dwarves!"        
        }
    }

如果我想通过" Freeman"让所有书籍都有评论,我会这样做:

&fq={!parent which='type_s:book'}type_s:review AND reviewer_s:Freeman

结果会给我两本书。

但是,如果我想让所有书籍都没有得到#34; Freeman"

的评论,我该怎么办呢?

我试过这样的

&fq={!parent which='type_s:book'}type_s:review AND reviewer_s:(NOT Freeman)

这给了我0结果

和这个

&fq={!parent which='type_s:book'}type_s:review AND NOT reviewer_s:Freeman)

这给了我所有的父文件。

以下更有希望,它给了我一些结果(在我的实际用例中)

&fq={!parent which='type_s:book'}type_s:review AND -reviewer_s:["" TO *]

请注意,我也尝试使用单引号中的搜索词进行查询。

1 个答案:

答案 0 :(得分:3)

如果solr在elasticsearch中具有 include_in_parent 等功能,则可以实现。但是,如果您以不同的方式略微索引数据,您将能够实现您想要的效果。您需要将审阅者索引为父文档中的多值字段(实际上,当您使用include_in_parent时,elasticsearch会在幕后做这件事:)

[{                                                                                                                                                                                                        
    "type_s": "book",                                                                                                                                                                                     
    "id": 4294967298,                                                                                                                                                                                     
    "title_s": "The Little Mermaid",                                                                                                                                                                      
    "reviewers_ms": ["Freeman, Gordon", "Denton, J.C."],                                                                                                                                                  
    ...                                                                                                                                                                                                   
}, {                                                                                                                                                                                                      
    "type_s": "book",                                                                                                                                                                                     
    "id": 4294967299,                                                                                                                                                                                     
    "title_s": "Lion King",                                                                                                                                                                               
    "reviewers_ms": ["Woods, Susanne", "Denver, Michel"],                                                                                                                                                 
    ...                                                                                                                                                                                                   
}, {                                                                                                                                                                                                      
    "type_s": "book",                                                                                                                                                                                     
    "id": 4294967300,                                                                                                                                                                                     
    "title_s": "7 dwarves",                                                                                                                                                                               
    "reviewers_ms": ["Freeman, Gordon", "Delacroix, Marie"],                                                                                                                                              
    ...                                                                                                                                                                                                   
}]

如果你过滤了以下内容,那么你会得到想要的结果:

type_s:book AND -reviewers_ms:"Freeman, Gordon"

更新

我找到了一种无需特殊索引即可解决问题的方法。这个过滤器对我有用:

type_s:book AND -{!parent which='type_s:book' v='reviewer_s:"Freeman, Gordon"'}

此语法还应修复您在评论

中提到的问题
  

但是,如果我有另一个字段,请说" reviewer_type"在里面   嵌套文档,我想以组合方式过滤?

type_s:book AND -{!parent which='type_s:book' v='reviewer_s:"Freeman, Gordon" AND type_s:"review"'}