繁体   English   中英

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

[英]Solr query NOT on nested child documents

如何查询父文档,子文档没有特定的字段值?

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

    {
        "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

这样会给我两本书。

但是,如果我想让所有书籍都没有“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 *]

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

如果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"'}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM