R – Parse a search string (into NHibernate Criterias )

criterianhibernatesearch

I would like to implement an advanced search for my project.
The search right now uses all the strings the user enters and makes one big disjunction with criteria API.

This works fine, but now I would like to implement more features: AND, OR and brackets()

I have got a hard time parsing the string – and building criterias from the string. I have found this Stackoverflow question, but it didn't really help (he didn't make it clear what he wanted).

I found another article, but this supports much more and spits out sql statements.
Another thing I've heard mention a lot is Lucene – but I'm not sure if this really would help me.


I've been searching around a little bit and I've found the Lucene.Net WhitespaceAnalyzer and the QueryParser.

It changes the search A AND B OR C into something like +A +B C, which is a good step in the correct direction (plus it handles brackets).
The next step would be to get the converted string into a set of conjunctions and disjunctions.

The Java example I found was using the query builder which I couldn't find in NHibernate.

Any more ideas ?

Best Solution

Guess you haven't heard about Nhibernate Search till now

Nhibernate Search uses lucene underneath and gives u all the options of using AND, OR, grammar.
All you have to do is attribute your entities for indexing and Nhibernate will index it at a predefined location.
Next time you can search this index with the power that lucene exposes and then get your domain level entity objects in return.

using (IFullTextSession s = Search.CreateFullTextSession(sf.OpenSession(new SearchInterceptor()))) {

        QueryParser qp = new QueryParser("id", new StopAnalyzer());

        IQuery NHQuery = s.CreateFullTextQuery(qp.Parse("Summary:series"), typeof(Book));

        IList result = NHQuery.List();

Powerful, isn’t it?