[英]Lucene document structure to correctly group a collection of keys
我有一個這樣的Java模型(省略了一些字段):
@Searchable(root=true)
class Person {
@SearchableProperty
String sex;
@SearchableProperty
String name;
}
class Parent extends Person {
@SearchableComponent
List<Person> children;
}
此模型為Anakin人員創建具有以下數據的lucene文檔:
$/person/sex:male
$/person/name:anakin
$/person/children/sex:male
$/person/children/name:luke
$/person/children/sex:female
$/person/children/name:leia
假設這只是許多文檔之一,我可以這樣搜索:
尋找名字開頭為且有一個男孩的人
$/person/name:an* AND $/person/children/sex:male
尋找有男嬰和女嬰的人
$/person/children/sex:male AND $/person/children/sex:female
試圖找到一個有特定名字和性別的孩子時遇到麻煩
$/person/children/sex:male AND $/person/children/name:leia
這將返回結果,我明白了為什么。 我希望這不會返回任何結果。 我的問題是如何區分或關聯這些嵌套屬性,以使查詢返回有效數據?
我考慮過:
將孩子存儲為單獨的文檔,盡管這樣做會使我失去以上面編寫的方式進行搜索的功能。
在查詢中以某種方式使用id
字段將這些字段分組。 我一直無法提出一種“正確”的方法。 我考慮過的變體:
$/person/children/1/name:luke $/person/children/name:luke1 or $/person/children/name:1luke
我對Compass並不熟悉,但是在Lucene級別上,您可以使用BlockJoinQuery將子文檔嵌套到父文檔中並進行查詢。
Mike McCandless在Lucene 3.4 [1]中有一篇關於使用BlockJoinQuery的優秀博客文章。 那應該給您基本概念。 但是,在Lucene 4中,API進行了更改,並且現在位於org.apache.search.lucene.join
包下。 Javadoc [2]中有一個代碼示例。
[1] http://blog.mikemccandless.com/2012/01/searching-relational-content-with.html
[2] http://lucene.apache.org/core/4_10_1/join/org/apache/lucene/search/join/package-summary.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.