how do i filter my lucene search results?

邮差的信 提交于 2019-12-12 16:08:55

问题


Say my requirement is

"search for all users by name, who are over 18"

If i were using SQL, i might write something like:

Select * from [Users]
Where ([firstname] like '%' + @searchTerm + '%' OR 
       [lastname] like '%' + @searchTerm + '%')
    AND [age] >= 18

However, im having difficulty translating this into lucene.net.

This is what i have so far:

var parser = new MultiFieldQueryParser({ "firstname", "lastname"}, new StandardAnalyser());
var luceneQuery = parser.Parse(searchterm)

var query = FullTextSession.CreateFullTextQuery(luceneQuery, typeof(User));

var results = query.List<User>();

How do i add in the "where age >= 18" bit?

I've heard about .SetFilter(), but this only accepts LuceneQueries, and not IQueries. If SetFilter is the right thing to use, how do I make the appropriate filter? If not, what do I use and how do i do it?

Thanks!

P.S. This is a vastly simplified version of what I'm trying to do for clarity, my WHERE clause is actually a lot more complicated than shown here. In reality i need to check if ids exist in subqueries and check a number of unindexed properties. Any solutions given need to support this.

Thanks


回答1:


For the age field you need a range search, written in the syntax of Lucene something like:

age:[18 TO 100]

As Gandalf said, you can use a QueryWrapperFilter. I am not sure this exists in Nhibernate Search. Similarily, you can use "AND" to further constrain your query. I am not sure what you can do about unindexed properties.




回答2:


Use a QueryWrapperFilter.




回答3:


In the end i did away with NHibernate.Search and simply talked directly to lucene to get the IDs, then passed these into an HQL where clause, much simple, and more efficient.

Edit: There is a restriction in NH.Search that prevents this from working. It can be simply patched but once you've read through the NH.S code you realise how awfully inefficient it is. Going straight to Lucene is the best option.



来源:https://stackoverflow.com/questions/1271234/how-do-i-filter-my-lucene-search-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!