public static QueryProcessResult Process(string query) { if (String.IsNullOrEmpty(query)) { return new QueryProcessResult { IsValid = true } } ; GroupNode result; try { var parser = new QueryParser(); result = parser.Parse(query); } catch (Exception ex) { return(new QueryProcessResult { Message = ex.Message }); } var validator = new QueryProcessorVisitor(new HashSet <string> { "hidden", "fixed", "type" }); result.Accept(validator); string expandedQuery = validator.UsesDataFields ? GenerateQueryVisitor.Run(result) : query; return(new QueryProcessResult { IsValid = true, UsesPremiumFeatures = validator.UsesPremiumFeatures, ExpandedQuery = expandedQuery }); }
public async Task CanProcessQueryAsync(string query, string expected, bool isValid, bool usesPremiumFeatures) { var context = new ElasticQueryVisitorContext(); IQueryNode result; try { result = await _parser.ParseAsync(query, QueryType.Query, context).AnyContext(); } catch (Exception ex) { _logger.Error(ex, $"Error parsing query: {query}. Message: {ex.Message}"); if (isValid) { throw; } return; } // NOTE: we have to do this because we don't have access to the right query parser instance. result = await EventFieldsQueryVisitor.RunAsync(result, context); Assert.Equal(expected, await GenerateQueryVisitor.RunAsync(result, context)); var info = await _validator.ValidateQueryAsync(result); _logger.Info(() => $"UsesPremiumFeatures: {info.UsesPremiumFeatures} IsValid: {info.IsValid} Message: {info.Message}"); Assert.Equal(isValid, info.IsValid); Assert.Equal(usesPremiumFeatures, info.UsesPremiumFeatures); }
public static QueryProcessResult Process(string query) { if (String.IsNullOrWhiteSpace(query)) { return new QueryProcessResult { IsValid = true } } ; GroupNode result; try { var parser = new QueryParser(); result = parser.Parse(query); } catch (Exception ex) { return(new QueryProcessResult { Message = ex.Message }); } var validator = new QueryProcessorVisitor(_freeFields); result.Accept(validator); var expandedQuery = validator.UsesDataFields ? GenerateQueryVisitor.Run(result) : query; return(new QueryProcessResult { IsValid = true, UsesPremiumFeatures = validator.UsesPremiumFeatures, ExpandedQuery = expandedQuery }); }
public async Task CanParseFieldRegex() { string query = @"myfield:/\(\[A-Za-z\]+\).*?/"; #if ENABLE_TRACING var tracer = new LoggingTracer(_logger, reportPerformance: true); #else var tracer = NullTracer.Instance; #endif var parser = new LuceneQueryParser { Tracer = tracer }; try { var result = await parser.ParseAsync(query); _logger.LogInformation(await DebugQueryVisitor.RunAsync(result)); string generatedQuery = await GenerateQueryVisitor.RunAsync(result); Assert.Equal(query, generatedQuery); var groupNode = result as GroupNode; Assert.NotNull(groupNode); var leftNode = groupNode.Left as TermNode; Assert.NotNull(leftNode); Assert.Equal("myfield", leftNode.Field); Assert.True(leftNode.IsRegexTerm); } catch (FormatException ex) { var cursor = ex.Data["cursor"] as Cursor; throw new FormatException($"[{cursor.Line}:{cursor.Column}] {ex.Message}", ex); } }
public async Task CanUseForwardSlashes() { string query = @"hey/now"; #if ENABLE_TRACING var tracer = new LoggingTracer(_logger, reportPerformance: true); #else var tracer = NullTracer.Instance; #endif var parser = new LuceneQueryParser { Tracer = tracer }; try { var result = await parser.ParseAsync(query); _logger.LogInformation(await DebugQueryVisitor.RunAsync(result)); string generatedQuery = await GenerateQueryVisitor.RunAsync(result); Assert.Equal(query, generatedQuery); } catch (FormatException ex) { _logger.LogInformation(tracer.ToString()); var cursor = ex.Data["cursor"] as Cursor; throw new FormatException($"[{cursor.Line}:{cursor.Column}] {ex.Message}", ex); } }
public async Task CanParseEscapedQuery(string escaped) { // https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_reserved_characters // + - && || ! ( ) { } [ ] ^ " ~ * ? : \ / string query = @"\" + escaped; #if ENABLE_TRACING var tracer = new LoggingTracer(_logger, reportPerformance: true); #else var tracer = NullTracer.Instance; #endif var parser = new LuceneQueryParser { Tracer = tracer }; try { _logger.LogInformation($"Attempting: {escaped}"); var result = await parser.ParseAsync(query); _logger.LogInformation(await DebugQueryVisitor.RunAsync(result)); string generatedQuery = await GenerateQueryVisitor.RunAsync(result); Assert.Equal(query, generatedQuery); } catch (FormatException ex) { var cursor = ex.Data["cursor"] as Cursor; throw new FormatException($"[{cursor.Line}:{cursor.Column}] {ex.Message}", ex); } }
private async Task ParseAndValidateQuery(string query, string expected, bool isValid) { #if ENABLE_TRACING var tracer = new LoggingTracer(_logger, reportPerformance: true); #else var tracer = NullTracer.Instance; #endif var parser = new LuceneQueryParser { Tracer = tracer }; IQueryNode result; try { result = await parser.ParseAsync(query); } catch (FormatException ex) { Assert.False(isValid, ex.Message); return; } string nodes = await DebugQueryVisitor.RunAsync(result); _logger.LogInformation(nodes); string generatedQuery = await GenerateQueryVisitor.RunAsync(result); Assert.Equal(expected, generatedQuery); }
public void CanGenerateQuery(string query) { var parser = new QueryParser(); var result = parser.Parse(query); Debug.WriteLine(DebugQueryVisitor.Run(result)); var generatedQuery = GenerateQueryVisitor.Run(result); Assert.Equal(query, generatedQuery); }
public Task BuildAsync <T>(QueryBuilderContext <T> ctx) where T : class, new() { var searchQuery = ctx.GetSourceAs <ISearchQuery>(); if (searchQuery == null) { return(Task.CompletedTask); } if (!String.IsNullOrEmpty(searchQuery.Filter)) { var result = _parser.Parse(searchQuery.Filter); searchQuery.Filter = GenerateQueryVisitor.Run(AliasedQueryVisitor.Run(result, _aliasMap, ctx), ctx); ctx.Filter &= new QueryFilter { Query = new QueryStringQuery { Query = searchQuery.Filter, DefaultOperator = Operator.And, AnalyzeWildcard = false }.ToContainer() }; } if (!String.IsNullOrEmpty(searchQuery.Criteria)) { var result = _parser.Parse(searchQuery.Criteria); searchQuery.Criteria = GenerateQueryVisitor.Run(AliasedQueryVisitor.Run(result, _aliasMap, ctx), ctx); ctx.Query &= new QueryStringQuery { Query = searchQuery.Criteria, DefaultOperator = searchQuery.DefaultCriteriaOperator == SearchOperator.Or ? Operator.Or : Operator.And, AnalyzeWildcard = true }; } if (!String.IsNullOrEmpty(searchQuery.Sort)) { var result = _parser.Parse(searchQuery.Sort); var opt = ctx.GetOptionsAs <IElasticQueryOptions>(); TermToFieldVisitor.Run(result, ctx); AliasedQueryVisitor.Run(result, _aliasMap, ctx); var fields = GetReferencedFieldsQueryVisitor.Run(result); // TODO: Check referenced fields against opt.AllowedSortFields var sort = GetSortFieldsVisitor.Run(result, ctx); ctx.Search.Sort(sort); } return(Task.CompletedTask); }
public static async Task <EventStackFilterQueryResult> RunAsync(IQueryNode node, EventStackFilterQueryMode queryMode, IQueryVisitorContext context = null) { var visitor = new EventStackFilterQueryVisitor(queryMode); var stackNode = await visitor.AcceptAsync(node, context).AnyContext(); var result = await GenerateQueryVisitor.RunAsync(stackNode, context).AnyContext(); return(new EventStackFilterQueryResult { Query = result, IsInvertSuccessful = visitor.IsInvertSuccessful, HasStatusOpen = visitor.HasStatusOpen, HasStackSpecificCriteria = visitor.HasStackSpecificCriteria }); }
public async Task CanGenerateSingleQueryAsync() { string query = "datehistogram:(date~2^-5\\:30 min:date max:date)"; var parser = new LuceneQueryParser(); var result = await parser.ParseAsync(query); _logger.LogInformation(await DebugQueryVisitor.RunAsync(result)); string generatedQuery = await GenerateQueryVisitor.RunAsync(result); Assert.Equal(query, generatedQuery); await new AssignAggregationTypeVisitor().AcceptAsync(result, null); _logger.LogInformation(DebugQueryVisitor.Run(result)); }
public Task BuildAsync <T>(QueryBuilderContext <T> ctx) where T : class, new() { string filter = ctx.Source.GetFilterExpression(); string search = ctx.Source.GetSearchExpression(); string sort = ctx.Source.GetSortExpression(); if (!String.IsNullOrEmpty(filter)) { var result = _parser.Parse(filter); filter = GenerateQueryVisitor.Run(FieldResolverQueryVisitor.Run(result, _aliasMap, ctx), ctx); ctx.Filter &= new QueryStringQuery { Query = filter, DefaultOperator = Operator.And, AnalyzeWildcard = false }; } if (!String.IsNullOrEmpty(search)) { var result = _parser.Parse(search); search = GenerateQueryVisitor.Run(FieldResolverQueryVisitor.Run(result, _aliasMap, ctx), ctx); ctx.Query &= new QueryStringQuery { Query = search, DefaultOperator = ctx.Source.GetSearchExpressionDefaultOperator() == SearchOperator.Or ? Operator.Or : Operator.And, AnalyzeWildcard = true }; } if (!String.IsNullOrEmpty(sort)) { var result = _parser.Parse(sort); TermToFieldVisitor.Run(result, ctx); FieldResolverQueryVisitor.Run(result, _aliasMap, ctx); var fields = GetReferencedFieldsQueryVisitor.Run(result); // TODO: Check referenced fields against opt.AllowedSortFields var sortFields = GetSortFieldsVisitor.Run(result, ctx).ToList(); ctx.Search.Sort(sortFields); } return(Task.CompletedTask); }
public async Task CanUseForwardSlashes() { string query = @"hey/now"; var tracer = new StringBuilderTrace(); var parser = new LuceneQueryParser { //Tracer = tracer }; try { var result = await parser.ParseAsync(query); _logger.LogInformation(await DebugQueryVisitor.RunAsync(result)); string generatedQuery = await GenerateQueryVisitor.RunAsync(result); Assert.Equal(query, generatedQuery); } catch (FormatException ex) { _logger.LogInformation(tracer.ToString()); var cursor = ex.Data["cursor"] as Cursor; throw new FormatException($"[{cursor.Line}:{cursor.Column}] {ex.Message}", ex); } }
public async Task CanGenerateQueryAsync(string query, string expected, bool isValid) { var parser = new LuceneQueryParser(); Log.MinimumLevel = LogLevel.Information; IQueryNode result; try { result = await parser.ParseAsync(query); } catch (Exception ex) { Assert.False(isValid, ex.Message); return; } string nodes = await DebugQueryVisitor.RunAsync(result); _logger.LogInformation(nodes); string generatedQuery = await GenerateQueryVisitor.RunAsync(result); Assert.Equal(expected, generatedQuery); }
public async Task CanParseEscapedQuery(string escaped) { // https://lucene.apache.org/core/2_9_4/queryparsersyntax.html#Escaping%20Special%20Characters // + - && || ! ( ) { } [ ] ^ " ~ * ? : \ string query = @"\" + escaped; var tracer = new StringBuilderTrace(); var parser = new LuceneQueryParser { //Tracer = tracer }; try { _logger.LogInformation($"Attempting: {escaped}"); var result = await parser.ParseAsync(query); _logger.LogInformation(await DebugQueryVisitor.RunAsync(result)); string generatedQuery = await GenerateQueryVisitor.RunAsync(result); Assert.Equal(query, generatedQuery); } catch (FormatException ex) { _logger.LogInformation(tracer.ToString()); var cursor = ex.Data["cursor"] as Cursor; throw new FormatException($"[{cursor.Line}:{cursor.Column}] {ex.Message}", ex); } }