views:

1041

answers:

3

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

A: 

Use a QueryWrapperFilter.

Gandalf
can you give an example? In Lucene.Net QueryFilter only accepts LunceneQueries, not nhib queries. How do i create the query (in a generic way if possible, not just for `age >= 18`)? thanks
Andrew Bullock
A: 

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.

Yuval F
A: 

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.

Andrew Bullock