当我使用 Lucene 为我的实体建立索引时,我习惯于将我所有的索引属性放在一个名为“all”的字段中,以对我的“所有”实体类型执行搜索。
现在,使用 NHibernate.Search,我找不到如何执行此操作。我试过这个:
[Indexed(Index = "MyIndex")]
public class Post
{
[DocumentId]
public virtual int Id { get; set; }
[IndexedEmbedded]
public virtual Author Author { get; set; }
[IndexedEmbedded]
public virtual IEnumerable<Category> Categories { get; set; }
[Field(Index.Tokenized, Store = Store.Yes)]
[Field(Name = "All", Index = Index.Tokenized, Store = Store.Yes)]
public virtual string Name { get; set; }
[Field(Name = "All", Index = Index.Tokenized, Store = Store.Yes)]
[Field(Index.Tokenized, Store = Store.Yes)]
public virtual string Body { get; set; }
}
但是我在 ScopedAnalyzer.cs 第 26 行中抛出了一个异常:“字典中已经存在键”:
scopedAnalyzers.Add(scope, analyzer);
其中“范围”是索引字段的名称(此处为“全部”)。如果我像这样检查
if( !scopedAnalyzers.ContainsKey( scope ) )
它会工作得很好:我将为每个“帖子”文档设置 2 个字段,一个包含正文,一个包含名称。 但是,我并不容易修改 NHibernate.Search 源代码。
有人对如何在一个字段中索引不同的属性有什么建议吗?
最佳答案
如果您在使用 Field 属性来指定您需要的内容时遇到问题,您可以创建一个类桥,它允许您创建自己的文档。
只需在类定义上使用 ClassBridgeAttribute 并指定实现 IFieldBridge 接口(interface)的类型。这将使您能够完全控制 Lucene 文档的构建方式。
更多信息可以在 http://docs.jboss.org/hibernate/stable/search/reference/en/html/search-mapping-bridge.html 找到见 4.2.2.3。类桥部分
关于hibernate-search - (N)Hibernate.Search : Index different properties in one field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1548946/