使用 NHibernate Search 进行多态查询

发布于 2025-01-03 05:19:41 字数 927 浏览 4 评论 0原文

我有多个实体存储在一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时常饿 2025-01-10 05:19:41

CreateFullTextQuery 的重载允许您指定要搜索的类型:

fullTextSession.CreateFullTextQuery(query, typeof(EntityA), typeof(EntityB)).List<ISearchable>();

必须指定所有类型有点笨拙,但它们加载得很好。我剩下的唯一问题是,我假设默认情况下可以进行所有字段搜索是不正确的,因此它需要在所有可搜索实体的所有属性上构建一个 MultiFieldQueryParser

private static Query ParseQuery(string query, IFullTextSession searchSession)
{
    var parser = new MultiFieldQueryParser(GetAllFieldNames(searchSession), new StandardAnalyzer());
    return parser.Parse(query);
}

private static string[] GetAllFieldNames(IFullTextSession searchSession)
{
    var reader =
        searchSession.SearchFactory.ReaderProvider.OpenReader(
            searchSession.SearchFactory.GetDirectoryProviders(typeof (Company)));
    var fieldNames = reader.GetFieldNames(IndexReader.FieldOption.ALL);
    return fieldNames.Cast<string>().ToArray();
}

An overload of CreateFullTextQuery exists that allows you to specify the types to search:

fullTextSession.CreateFullTextQuery(query, typeof(EntityA), typeof(EntityB)).List<ISearchable>();

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:

private static Query ParseQuery(string query, IFullTextSession searchSession)
{
    var parser = new MultiFieldQueryParser(GetAllFieldNames(searchSession), new StandardAnalyzer());
    return parser.Parse(query);
}

private static string[] GetAllFieldNames(IFullTextSession searchSession)
{
    var reader =
        searchSession.SearchFactory.ReaderProvider.OpenReader(
            searchSession.SearchFactory.GetDirectoryProviders(typeof (Company)));
    var fieldNames = reader.GetFieldNames(IndexReader.FieldOption.ALL);
    return fieldNames.Cast<string>().ToArray();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文