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 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);
            }
        }
Exemple #4
0
        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);
            }
        }
Exemple #5
0
        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);
        }
Exemple #6
0
        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 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);
            }
        }