Search string parsing (in NHibernate criteria)

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 clause with the criteria API.

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

It's hard for me to parse a line - and build criteria from a line. I found https://stackoverflow.com/a/167408/ ... but it didn’t help (he didn’t make it clear what he needed).

I found another article , but it supports much more and spits out SQL expressions. Another thing I've heard a lot about is Lucene, but I'm not sure if this will really help me.


I searched a bit and I found Lucene.Net WhitespaceAnalyzer and QueryParser.

It changes the search A AND B OR C to something like + A + BC, which is a good step in the right direction (plus it processes the brackets). The next step would be to convert the converted string to a set of alliances and clauses.

The Java example I found used a query builder that I could not find in NHibernate.

Any ideas?

+3
source share
2 answers

What I'm basically doing right now is parsing the input string using the Lucene.Net parsing API. This gives me a single 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 parse this line manually, creating clauses and alliances.

+1
source

, Nhibernate Search

Nhibernate Search lucene u AND, OR, .
, , - , Nhibernate .
, lucene, .

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();

, ?

+3

Source: https://habr.com/ru/post/1717407/


All Articles