NHProf in the real world: NHibernate Search integration
I had a problem in one of my applications, a Lucene search was returning more results than I wanted it to, and I wasn’t sure what was going on.
I pre marked the problematic line, but at the time, I had no idea why it is returning me all those extra results that had nothing to do with my queries. But since NH Prof gave me the full Lucene query text, I could take this and plug this into Luke.
Luke is the preferred tool to working with Lucene indexes, and running the query allowed me to play around and figure out what my problem was:
I was specifying that everything that is active MUST be returned, and everything that matches the query should be returned. Obviously, anything that is active (regardless of the rest of the query), must be returned.
In effect, this is similar to this SQL query:
select * from Entries
where IsActive = 1 or ( ... )
Once I figured that one out, it was pretty easy to fix the query to use MUST_NOT IsActive == False. But it would have been much harder without NH Prof.
Comments
Ah, nice trap for SQL-thinkers
But... wait a moment... isn't it a SOFT DELETE what we have here?
I don't know how not having NH Prof would've made this much harder... I debug-log my Lucene queries. It's not having Luke that would make things much harder here.
Thanks. Somehow missed this tool.
Rafal,
Nope, IsActive is literally that, is an entry active or not.
A better name might be IsPublished, an inactive entry indicates that the entry is in draft mode.
I'll have to give NH Prof a try for NH Search but I thought the same thing Niklas did. I've been using the output from query.ToString( ) in Luke to do similar debugging. It would be nicer to get that without having to hook into the debugger though.
Ayende,
I'm not sure how you can do it with creating 'TermQuery' objects, but if you manually write a lucene query in text form:
+IsActive:True AND (Title:weber^2.5 Body:weber^1.5 Categories.Name:weber)
This should mean:
IsActive must be true, and (Title field is like weber OR body is like weber OR categories.Name contains weber)
So you can use brackets in the queries (like in SQL) to specify what you really mean.
I am sure you can do the same with the TermQuery objects, I have not personally looked into that myself.
Slightly offtopic, but in Luke, be sure you select the same 'analyzer' that you used to create the index (eg, StandardAnalyzer instead of KeywordAnalyzer), otherwise when you query the index with Luke (using a different analyzer) you may get strange results - that's got me before.
Comment preview