Nhibernate.Search, Lucene, and the Criteria API: types mismatch - nhibernate

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:
[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)
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.
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:
Create the Nhibernate.Search query object using the following
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
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?
Thank you for reading,
Samy

Looks like this has been resolved with revision 1611 of NHibernate.Search :
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.
Modified : /trunk/src/NHibernate.Search/src/NHibernate.Search/Query/FullTextQueryImpl.cs
svn : https://nhcontrib.svn.sourceforge.net/svnroot/nhcontrib/trunk/src/NHibernate.Search/

Related

LINQ retrieving a record based on date

I am trying to learn a bit about ling. in the code below, I'm trying to retrieve records that have an activeuntil date which after the current date.
Dim context As DynamicsCRMEntities = New DynamicsCRMEntities()
Dim CustomerNoticeQuery = From NewsArticles In context.BusinessUnitNewsArticles Where NewsArticles.NewsArticle Like "customer" Select NewsArticles.ArticleTitle, NewsArticles.NewsArticle, NewsArticles.ActiveUntil
For Each result In CustomerNoticeQuery
If Date.Now.Date >= result.ActiveUntil Then
CustomerNotice.Text = result.NewsArticle.ToString
End If
Next
I keep running into this error but cannot get my head around it
System.NotSupportedException: 'LINQ to Entities does not recognize the method 'Boolean LikeString(System.String, System.String, Microsoft.VisualBasic.CompareMethod)' method, and this method cannot be translated into a store expression.'
The Like operator you are using there is VB-specific and, as the error message says, is not supported by EF. You need to use standard .NET functionality that the LINQ to Entities provider understands. If you want to do a partial match on Strings, that means using String.Contains:
Dim CustomerNoticeQuery = From NewsArticles In context.BusinessUnitNewsArticles
Where NewsArticles.NewsArticle.Contains("customer")
Select NewsArticles.ArticleTitle, NewsArticles.NewsArticle, NewsArticles.ActiveUntil
You really ought to break your LINQ queries over multiple lines like that too, for the sake of readability.
Note that that addresses your actual issue, which has nothing to do with dates, so the date part to the question was completely irrelevant. That said, why would you do a query with a Where clause and then use a loop to filter the results of that query? Why would you not include the additional filter in the Where clause? I'd also consider using a better subject variable name in the query:
Dim today = Date.Today
Dim CustomerNoticeQuery = From article In context.BusinessUnitNewsArticles
Where article.NewsArticle.Contains("customer")
And article.ActiveUntil < today
Select article.ArticleTitle, article.NewsArticle, article.ActiveUntil

How to exclude results having (or not) a specific value in a collection?

let's say I have an entity more or less like this (pseudo code):
class Contact {
String name;
String surname;
List<Address> addresses;
}
class Address {
String streetName;
String type;
}
* let's say every field is correctly annotated with #Field / #Indexed / #Embeddable etc
Using jpa hibernate-search I can get every contact correctly using full-text-queries and fuzzy, but I cannot find a way to limit the search only to
name or surname or (addresses.streetName but only if addresses.type="XYZ"). I don't want it to search into streetNames when they're not of the type xyz.
org.apache.lucene.search.Query baseQuery = qb
.keyword()
.fuzzy()
.onFields("name", "surname")
.matching(String.join("+", queryStrings))
.createQuery();
org.apache.lucene.search.Query addressQueryRestriction = qb.keyword()
.onField("addresses.type")
.matching("XYZ")
.createQuery();
org.apache.lucene.search.Query addressQuery = qb.fuzzy()
.onFields("addresses.streetName")
.matching(String.join("+", queryStrings))
.createQuery();
org.apache.lucene.search.Query queryAddressComposite = qb
.bool()
.must(addressQuery)
.must(addressQueryRestriction)
.createQuery();
org.apache.lucene.search.Query finalQuery = qb
.bool()
.should(baseQuery)
.should(queryAddressComposite)
.createQuery();
I've been trying a lot by composing alternative queries with .bool().must() / should() / must().not() but without too much success. Especially when a contact has an XYZ address but also others that aren't.
I'm starting to thing it's a logical issue here, as I'm looking into a list, but if you have any idea of what I'm doing wrong please blast me.
If you indexed-embed a list of addresses in your document, and want to apply conditions to each of these addresses, rather than to all of them merged together, you need to index each object as a nested document and then use a "nested" predicate.
The concept of nested documents exists in Hibernate Search 6 (still in Beta), but not in Hibernate Search 5.
I would recommend that you upgrade.
See this section of the Hibernate Search 6 documentation for more information.

Linq Query Check with List (of string)

How to check list of string values from Linq query?
_extnsn is a list of string with values (.bmp, .jpg, .tga, ...).
I want to get these type of files with Linq query so I tried like that:
dim _FileCOllections = From _file In _dirInfo.GetFiles("*.*", SearchOption.TopDirectoryOnly)
Where _extnsn.All(Function(xf) _file.Extension.ToLower = xf.ToLower)
Order By _file.FullName Ascending
Select _file
Return value is nothing.
Apparently you have a DirectoryInfo object and a sequence of file extensions as string. You want a query that keeps only those files in your directory that have an extension that matches one of the file extensions.
DirectoryInfo _dirInfo = ...
IEnumerable<string> fileExtensions = ...
IEnumerable<FileInfo> filesWithDesiredExtensions = _dirInfo.EnumerateFiles()
.Where(fileInfo => fileExtensions.Contains(fileInfo.Extension));
Oops. I see you want it in VB! Sorry, maybe this might be of any use for C# readers
By the way, did you see I used DirectoryInfo.EnumerateFiles instead of GetFiles? This has the advantage that if you only want FirstOrDefault, or maybe Take(2), you don't have to fetch all FileInfos. Just an optimization
Are you sure you want to use _extnsn.All? To me it sounds like you meant _extnsn.Any. – Micha Wiedenmann
i just changed _exntsn.All to _exntsn.Any
Dim _exntsn As New List(Of String) From {".jpg", ".png", ".tga", ".tif"}
_FileCOllections = From _file In _dirInfo.GetFiles("*.*", SearchOption.TopDirectoryOnly)
Where _exntsn.Any(Function(k) k.ToLower = _file.Extension.ToLower)
Order By _file.FullName Ascending
Select _file
It's working fine for me, i can get these particular extension image collections ... thanks .. Micha Wiedenmann and all.

FindAll() in Linq Query List

I have a Linq Query made into a list called "ticket query"
I want to search ticket query for all the records that have specific data
I tried using FindAll() but it gives me an error
Argument matching parameter 'match' cannot convert from
'VB$AnonymousDelegate_1(Of JobPartForm,Nullable(Of Boolean))' to
'Predicate(Of JobPartForm)'.
I can't do the findall directly in the query because its being called at a separate time
is there another way to accomplish this, or am I using find all wrong?
ticketquery = (From ticket In dbContext.JobPartForm
Select ticket).ToList()
Dim formticket = ticketquery.FindAll(Function(f As JobPartForm) f.JobNum = ticketnum And f.FormNumber = formnum)
You can do the same using IQueryable<TSource>.Where method:
Dim formticket=dbContext.JobPartForm.Where((Function(f As JobPartForm) f.JobNum = ticketnum And f.FormNumber = formnum)).ToList();
The first thing is try to never call ToList extension method from a DbSet, that will load your entire table to memory, is really inefficient and more when you can filter your data on the server side.

Adding related records in LINQ

Processing an XML file with LINQ to add records into a table in a SQL Server database via a data context. As we are adding records we need to assign the parents in various other tables. Most of the time we can find the existing parent and use it but sometimes we will need to create a new parent.
Thought we could do this like this:
Dim defaultPub As publication
defaultPub = New publication With {.name = e..Value}
Dim pub = _Data.publications.Where(Function(s) s.name = e..Value).DefaultIfEmpty(defaultPub).SingleOrDefault
.publication = pub
So we are trying to find a publication that matches e..Value from our XML, but if we can't find one then we use 'defaultPub'. If it does exist though we don't want to add it again. So maybe this approach is flawed anyway even if it did work...
Anyway it's not currently working, we get this error:
Unsupported overload used for query operator 'DefaultIfEmpty'.
The overload requires a publication and is getting one (I've checked the TypeNames in quickwatch), don't know what is going on here.
What we are really looking for is something like the find_or_create from activerecord for Ruby that LINQ seems to be a copied from.
Thanks in advance,
Dave.
OK, I can just do it like this:
Dim pub = _Data.publications.Where(Function(s) s.name = e.<source>.Value).SingleOrDefault
Dim newPub As publication
If Not IsNothing(pub) AndAlso pub.name <> "" Then
.publication = pub
Else
newPub = New publication With {.name = e.<source>.Value}
.publication = newPub
End If
Thanks Val!