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);
            }
        }
Beispiel #2
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);
            }
        }
        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);
            }
        }
Beispiel #4
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);
        }