简体   繁体   中英

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 concatenation 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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