使用 NHibernate Search 进行多态查询
我有多个实体存储在一个 NHibernate 搜索索引中,希望能够一次查询所有这些实体。该用例是一个返回混合结果的简单搜索页面。因此,例如,代码可能如下所示:
public interface ISearchable {}
[Indexed(Index = "TheIndex")]
public class SearchableEntityA : ISearchable
{
// Some [Field]s
}
[Indexed(Index = "TheIndex")]
public class SearchableEntityB : ISearchable
{
// Some other [Field]s
}
这所有索引都很好,当然可以在原始 NHibernate 中查询,如下所示:
session.CreateCriteria<ISearchable>().List<ISearchable>();
我在 ISearchable 上有一些字段,但 NHibernate 映射中没有具体引用这些字段。
我的希望是我可以说:
var query = "some keyword";
fullTextSession.CreateFullTextQuery<ISearchable>(query).List<ISearchable>();
并检索 ISearchables 列表,其中包含来自各种不同实体的结果。然而,实际情况是它会抛出NHibernate.HibernateException: Not amappedentity: NetComposites.Model.ISearchable
。
那么,使用 NHibernate Search 实现类似多态查询的最简单方法是什么?
I have multiple entities stored in a single NHibernate Search index, in the hope that I'd be able to query over all of them at once. The use case is a simple search page which returns mixed results. So, for example, the code could look like this:
public interface ISearchable {}
[Indexed(Index = "TheIndex")]
public class SearchableEntityA : ISearchable
{
// Some [Field]s
}
[Indexed(Index = "TheIndex")]
public class SearchableEntityB : ISearchable
{
// Some other [Field]s
}
This all indexes fine, and of course is queryable in raw NHibernate like so:
session.CreateCriteria<ISearchable>().List<ISearchable>();
I have some fields on ISearchable, but these aren't specifically referenced in NHibernate mappings.
My hope was that I could just say:
var query = "some keyword";
fullTextSession.CreateFullTextQuery<ISearchable>(query).List<ISearchable>();
And retrieve a list of ISearchables, containing results from various different entities. However, the reality is that it throws NHibernate.HibernateException: Not a mapped entity: NetComposites.Model.ISearchable
.
So, what's the simplest way to achieve something resembling polymorphic queries with NHibernate Search?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CreateFullTextQuery 的重载允许您指定要搜索的类型:
必须指定所有类型有点笨拙,但它们加载得很好。我剩下的唯一问题是,我假设默认情况下可以进行所有字段搜索是不正确的,因此它需要在所有可搜索实体的所有属性上构建一个
MultiFieldQueryParser
:An overload of
CreateFullTextQuery
exists that allows you to specify the types to search:It's a little clunky having to specify all the types, but they load fine. The only remaining problem I have is that my assumption that you could just do an all fields search by default was incorrect, so it requires building a
MultiFieldQueryParser
over all properties of all searchable entities: