/// <inheritdoc/> public void Visit(LuceneBoolQuery boolQueryWrapper) { VerifyValid(boolQueryWrapper); var boolClause = new BoolQuery(); var must = new List <IQuery>(); var should = new List <IQuery>(); var mustNot = new List <IQuery>(); var clauses = ((BooleanQuery)boolQueryWrapper.LuceneQuery).GetClauses(); foreach (BooleanClause clause in clauses) { // based on the the current clause, instansiate the correct Lucene Query object var luceneQuery = VisitableLuceneQueryFactory.Make(clause.Query); luceneQuery.Accept(this); switch (clause.Occur) { case Occur.MUST: must.Add(luceneQuery.ESQuery); break; case Occur.SHOULD: should.Add(luceneQuery.ESQuery); break; case Occur.MUST_NOT: mustNot.Add(luceneQuery.ESQuery); break; } } boolClause.Must = must; boolClause.Should = should; boolClause.MustNot = mustNot; boolQueryWrapper.ESQuery = boolClause; }
/// <summary> /// Given a clause which has a Lucene based phrase, creates a KQL query. /// </summary> /// <param name="queryStringClause">The given clausse.</param> /// <returns>A KQL query string.</returns> private string CreateKqlFromLucenePhrase(QueryStringClause queryStringClause) { // we need to parse the phrase using var analyzer = new WhitespaceAnalyzer(); var queryParser = new QueryParser( Lucene.Net.Util.Version.LUCENE_30, queryStringClause.Default, analyzer) { AllowLeadingWildcard = queryStringClause.Wildcard, LowercaseExpandedTerms = false, }; // escaping special charachters from the pharse before parsing. // we would call QueryParser.Escape() method, but it escapes all charachters and // in our case we only have to worry about backslash. // implementation is based on: https://github.com/apache/lucenenet/blob/0eaf76540b8de326d1aa9ca24f4b5d6425a9ae38/src/Lucene.Net.QueryParser/Classic/QueryParserBase.cs var escapedPhrase = queryStringClause.Phrase.Replace(@"\", @"\\\", StringComparison.OrdinalIgnoreCase); // we parse and get the Lucene.Net query model var query = queryParser.Parse(escapedPhrase); // We make our own 'visitable' Lucence.Net query model var luceneQuery = VisitableLuceneQueryFactory.Make(query); // Visit var luceneVisitor = new LuceneVisitor(); luceneQuery.Accept(luceneVisitor); dynamic esQuery = luceneQuery.ESQuery; esQuery.Accept(this); return(esQuery.KustoQL); }