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

时间:2021-06-03 03:07:50

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. Here are two tests, one with the criteria API, one with the classic query schema:

我一直在寻找NHibernate.Search.Tests项目以了解如何使用Criteria API(我觉得查看测试代码以获得工作示例非常有用)并且我注意到使用全文搜索的方式完全不同。这是两个测试,一个使用标准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的FullText搜索与分页和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. So what i did was the following:

到目前为止,分页和全文搜索一直运行良好。最近,我们不得不使用条件API为查询添加特定的过滤器。所以我做的是以下内容:

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. That's where my problems appear because the FullTextQueryImpl uses the Name, and the Criteria uses the Fullname. Here's a constructor code for the CriteriaImpl class

快速浏览一下FullTextQueryImpl源文件(方法GetLoader),可以看出NHibernate.Search查询对象的类型名称与Criteria对象的EntityOrClassName属性之间存在比较。这就是我出现问题的地方,因为FullTextQueryImpl使用Name,而Criteria使用Fullname。这是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? Does it work as expected in C#, because i'm having a weird feeling that it could be vb.net related?

我在全文搜索和Criteria API的混合中错过了一些巨大的东西,还是其他的东西?它是否在C#中按预期工作,因为我有一种奇怪的感觉,它可能与vb.net相关?

Thank you for reading,

谢谢你的阅读,

Samy

1 个解决方案

#1


Looks like this has been resolved with revision 1611 of NHibernate.Search :

看起来这已经通过NHibernate的修订版1611解决了。搜索:

Revision: 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/

#1


Looks like this has been resolved with revision 1611 of NHibernate.Search :

看起来这已经通过NHibernate的修订版1611解决了。搜索:

Revision: 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/