views:

264

answers:

2

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 ?

+3  A: 

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?

Cherian
I am not totally sure if I want to go this way, because our users are having quite complex requests (defining search sets and saving them) - but thanks a lot. It looks great.
bernhardrusch
+1  A: 

What I am basically doing right now is parsing the input string with the Lucene.Net parse API. This gives me a uniform and simplified syntax. (Pseudocode)

using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;

void Function Search (string queryString)
{
    Analyzer analyzer = new WhitespaceAnalyzer();
    QueryParser luceneParser = new QueryParser("name", analyzer);
    Query luceneQuery = luceneParser.Parse(queryString);
    string[] words = luceneQuery.ToString().Split(' ');

    foreach (string word in words)
    {
        //Parsing the lucene.net string
    }
}

After that I am parsing this string manually, creating the disjunctions and conjunctions.

bernhardrusch