简体   繁体   English

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

[英]Solr query NOT on nested child documents

How can I query for parent documents, with child documents NOT having a certain field value? 如何查询父文档,子文档没有特定的字段值?

Eg: Let's say we have the following data structure: 例如:假设我们有以下数据结构:

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

If i want to get all books having a review by "Freeman", I would do like this: 如果我想让所有书籍都有“Freeman”的评论,我会这样做:

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

This would give me two books as result. 这样会给我两本书。

But how would I do if I wanted to get all books NOT having a review by "Freeman"? 但是,如果我想让所有书籍都没有“Freeman”的评论,我该怎么办?

I've tried like this 我试过这样的

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

this gives me 0 results 这给了我0个结果

and this 和这个

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

this gives me all parent documents. 这给了我所有的父文件。

Somewhat more promising is the following, which gives me some results (in my real use case) 以下更有希望,它给了我一些结果(在我的实际用例中)

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

Note that I tried the queries also with the searched term in single quotes. 请注意,我也使用单引号中的搜索词来尝试查询。

It would be achievable if solr would have functionality like include_in_parent in elasticsearch . 如果solr在elasticsearch中具有类似include_in_parent的功能,那么这是可以实现的。 However if you index your data slightly in different way you will be able to achieve what you want. 但是,如果您以不同的方式略微索引数据,您将能够实现您想要的效果。 You need to index reviewers as multivalued fields in parent documents (actually elasticsearch does this exast thing behind the scenes when you use 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"],                                                                                                                                              
    ...                                                                                                                                                                                                   
}]

And then you'll get wanted result if you filter by: 然后,如果您按以下过滤,您将得到想要的结果:

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

Update 更新

I've found a way to solve a problem without special indexing. 我找到了一种无需特殊索引即可解决问题的方法。 This filter worked for me: 这个过滤器对我有用:

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

this syntax should also fix your problem that you've mentioned in comment 此语法还应修复您在评论中提到的问题

However, what if I have another field, say "reviewer_type" in the nested documents, and I'd like to filter in a combined way? 但是,如果我有另一个字段,在嵌套文档中说“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