简体   繁体   English

Nhibernate.Search,Lucene和Criteria API:类型不匹配

[英]Nhibernate.Search, Lucene, and the Criteria API: types mismatch

Update 更新

I've been looking around the NHibernate.Search.Tests project to find out how the Criteria API is used (i find it immensely useful to look around the test code to have working examples) and i noticed that the way to use the Fulltext search is radically different. 我一直在寻找NHibernate.Search.Tests项目,以了解如何使用Criteria API(我发现查看测试代码以获取有效的示例非常有用),并且我注意到使用全文本搜索的方式完全不同。 Here are two tests, one with the criteria API, one with the classic query schema: 这是两个测试,一个使用标准API,一个使用经典查询架构:

[Test]
        public void ResultSize()
        {
            IFullTextSession s = Search.CreateFullTextSession(OpenSession());
            ITransaction tx = s.BeginTransaction();
            // snipped the objects creation

            QueryParser parser = new QueryParser("title", new StopAnalyzer());

            Lucene.Net.Search.Query query = parser.Parse("Summary:noword");
            IFullTextQuery hibQuery = s.CreateFullTextQuery(query, typeof(Clock), typeof(Book));
            Assert.AreEqual(0, hibQuery.ResultSize);

// snipped the end of the test
            }

[Test]
        public void UsingCriteriaApi()
        {
            IFullTextSession s = Search.CreateFullTextSession(OpenSession());
            ITransaction tx = s.BeginTransaction();
            // snipped creation
            IList list = s.CreateCriteria(typeof(Clock))
                .Add(SearchRestrictions.Query("Brand:seiko"))
                .List();
            Assert.AreEqual(1, list.Count, "should get result back from query");
         // snipped deletion
        }

The second solution works under vb.net , at the cost of the useful Lucene query (which embarks it's own total of the corresponding rows) and at the cost of the Lucene ordering (or i couldn't find it) 第二种解决方案在vb.net下工作 ,但要付出有用的Lucene查询(它本身是相应行的总数)的代价,而要付出Lucene排序的代价(或者我找不到它)


Hello everyone, 大家好,

yet again, i'm stumped on the path, but this time, i suspect something a bit more sinister than my usual erratic errors ( cue ominous music ) 再一次,我迷失了方向,但是这次,我怀疑有些东西比我通常的不稳定错误( 提示不祥的音乐 )更加险恶

I'm trying to combine FullText search using Lucene.net with paging and the Criteria API. 我试图结合使用Lucene.net与分页和Criteria API进行全文搜索。

So far paging and the Fulltext search have been working flawlessly. 到目前为止,分页和全文本搜索一直在正常工作。 Recently though, we had to use the criteria API to add specific filters to the query. 不过最近,我们不得不使用标准API向查询中添加特定的过滤器。 So what i did was the following: 所以我做了以下事情:

Create the Nhibernate.Search query object using the following 使用以下命令创建Nhibernate.Search查询对象

Private Function GetQuery(ByVal QueryString As String, ByVal Orders() As String) As IFullTextQuery
        Dim ifts As IFullTextSession = Search.CreateFullTextSession(UnitOfWork.CurrentSession)
        Dim analyzer As New SimpleAnalyzer
        Dim parser As New MultiFieldQueryParser(SearchPropertyNames, analyzer)
        Dim queryObj As Lucene.Net.Search.Query = parser.Parse(QueryString)

        Dim nhsQuery As IFullTextQuery = ifts.CreateFullTextQuery(queryObj, New System.Type() {GetType(T)})
        For i As Integer = 0 To Orders.Count - 1
            Orders(i) = Orders(i) & "FS"
        Next
        nhsQuery.SetSort(New Sort(Orders))

then add my Criteria to the query: 然后将我的条件添加到查询中:

Dim crit As ICriteria = ifts.CreateCriteria(GetType(T))
        Dim criterion As ICriterion
        If criteria IsNot Nothing Then
            For Each criterion In criteria
                If (Not criterion Is Nothing) Then
                    crit.Add(criterion)
                End If
            Next
        End If

nhsQuery.SetCriteriaQuery(crit)

but when i list the resulting query, i receive the following exception 但是当我列出结果查询时,我收到以下异常

Criteria query entity should match query entity 条件查询实体应与查询实体匹配

A quick glance in the FullTextQueryImpl source file (method GetLoader ) shows that there is a comparison between the type name given to the NHibernate.Search query object and the EntityOrClassName property for the Criteria object. 快速浏览一下FullTextQueryImpl源文件(方法GetLoader )显示,在给NHibernate.Search查询对象的类型名称和Criteria对象的EntityOrClassName属性之间进行了比较。 That's where my problems appear because the FullTextQueryImpl uses the Name , and the Criteria uses the Fullname . 那是我出现问题的地方,因为FullTextQueryImpl使用Name ,而Criteria使用Fullname Here's a constructor code for the CriteriaImpl class 这是CriteriaImpl类的构造函数代码

Public Sub New(ByVal persistentClass As Type, ByVal session As ISessionImplementor)
    Me.New(persistentClass.FullName, CriteriaSpecification.RootAlias, session)
    Me.persistentClass = persistentClass
End Sub

and here's the comparison: 比较如下:

Dim entityOrClassName As String = DirectCast(Me.criteria, CriteriaImpl).EntityOrClassName
            If ((Me.classes.GetLength(0) = 1) AndAlso (Me.classes(0).Name <> entityOrClassName)) Then
                Throw New SearchException("Criteria query entity should match query entity")
            End If

As a result, the comparison fails and the exception is thrown. 结果,比较失败,并引发异常。 I tried playing around with the aliases to no avail since the comparison is not using the aliases. 我尝试使用别名无济于事,因为比较未使用别名。

Am i missing something huge in my mix of the Fulltext search and the Criteria API, or is it something else? 我在全文搜索和Criteria API的组合中是否缺少巨大的东西,还是其他东西? Does it work as expected in C#, because i'm having a weird feeling that it could be vb.net related? 它是否可以在C#中按预期工作,因为我感到奇怪,它可能与vb.net有关?

Thank you for reading, 感谢您的阅读,

Samy 萨米

Looks like this has been resolved with revision 1611 of NHibernate.Search : 看起来这已通过NHibernate的修订版1611解决了:

Revision: 1611 修订:1611

Message: Fixed a bug where a full class name was being compared against a partial one. 消息:修复了将完整类名与部分类名进行比较的错误。 This was causing LuceneQueryTest.UsingCriteriaApi to fail. 这导致LuceneQueryTest.UsingCriteriaApi失败。

Modified : /trunk/src/NHibernate.Search/src/NHibernate.Search/Query/FullTextQueryImpl.cs 修改:/trunk/src/NHibernate.Search/src/NHibernate.Search/Query/FullTextQueryImpl.cs

svn : https://nhcontrib.svn.sourceforge.net/svnroot/nhcontrib/trunk/src/NHibernate.Search/ svn: https : //nhcontrib.svn.sourceforge.net/svnroot/nhcontrib/trunk/src/NHibernate.Search/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM