Wrong Nhibernate.Search query results
I am querying lucene index via nhibernate.search using code below:
var fts = NHibernate.Search.Search.CreateFullTextSession(this._session);
var luceneQuery = "Search:name~0.7 AND Moderated:true NOT PlaceType:WrongType";
var places = fts.CreateFullTextQuery<Place>(luceneQuery)
.List<Place>();
The problem is that query returns all types of Places, including WrongType. When I try to run the same query against the same index in Luke everything is ok, Places of type WrongType are not returned.
Search field is conca开发者_开发问答tenation of many fields in Place object. I am using Moderated and PlaceType fields to filter out some records, as I have discovered, that in this way original sorting order (by score) from Lucene query is preserved.
How can I exclude Places by PlaceType from results using NHibernate.Search?
Ok, so I have found solution.
I have indexed all fields using WhiteSpaceAnalyzer. It seems that NHibernate.Search is using StandardAnalyzer by default, regardless from the fact, that I have set global AnalyzerClass to WhiteSpaceAnalyzer. After parsing the query it looked like that:
"+Search:name~0.7 +Moderated:true -PlaceType:wrongtype"
which didn't work, because values in PlaceType field were not lowercased.
Changing the code in the question to something like that:
var fts = NHibernate.Search.Search.CreateFullTextSession(this._session);
var queryParser = new QueryParser("text", new WhitespaceAnalyzer());
var luceneQuery = "Search:name~0.7 AND Moderated:true NOT PlaceType:WrongType";
var query = queryParser.Parse(luceneQuery);
var places = fts.CreateFullTextQuery(query, typeof(Place))
.List<Place>();
solved the situation.
精彩评论