public async Task CanSkipIncludes()
        {
            var parser = new LuceneQueryParser();
            var result = await parser.ParseAsync("outter @include:other @skipped:(other stuff @include:other)");

            var includes = new Dictionary <string, string> {
                { "other", "field:value" },
                { "nested", "field:value @include:other" }
            };
            var resolved = await IncludeVisitor.RunAsync(result, includes, shouldSkipInclude : (n, ctx) => true);

            Assert.Equal("outter @include:other @skipped:(other stuff @include:other)", resolved.ToString());

            resolved = await IncludeVisitor.RunAsync(result, includes, shouldSkipInclude : ShouldSkipInclude);

            Assert.Equal("outter (field:value) @skipped:(other stuff @include:other)", resolved.ToString());

            resolved = await IncludeVisitor.RunAsync(result, includes, shouldSkipInclude : (n, ctx) => !ShouldSkipInclude(n, ctx));

            Assert.Equal("outter (field:value) @skipped:(other stuff (field:value))", resolved.ToString());

            var nestedResult = await parser.ParseAsync("outter @skipped:(other stuff @include:nested)");

            resolved = await IncludeVisitor.RunAsync(nestedResult, includes, shouldSkipInclude : ShouldSkipInclude);

            Assert.Equal("outter @skipped:(other stuff @include:nested)", resolved.ToString());
        }
        public async Task <string> GetEventFilterAsync(string query, IQueryVisitorContext context = null)
        {
            context = context ?? new ElasticQueryVisitorContext();
            var result = await _parser.ParseAsync(query, context);

            await _eventQueryVisitor.AcceptAsync(result, context);

            return(result.ToString());
        }
Exemplo n.º 3
0
        public static async Task <EventStackFilterQueryResult> RunAsync(string query, EventStackFilterQueryMode queryMode, IQueryVisitorContext context = null)
        {
            var parser = new LuceneQueryParser();
            var result = await parser.ParseAsync(query, context).AnyContext();

            return(await RunAsync(result, queryMode, context).AnyContext());
        }
 public async Task CanUseAliasMapForTopLevelAliasAsync() {
     var parser = new LuceneQueryParser();
     var result = await parser.ParseAsync("field1:value");
     var aliasMap = new FieldMap { { "field1", "field2" } };
     var aliased = await FieldResolverQueryVisitor.RunAsync(result, aliasMap);
     Assert.Equal("field2:value", aliased.ToString());
 }
        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);
            }
        }
Exemplo n.º 6
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);
            }
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
0
        public async Task VerifyRootNestedAliasAsync()
        {
            var client  = GetClient();
            var visitor = new AliasMappingVisitor(client.Infer);
            var walker  = new MappingWalker(visitor);

            walker.Accept(new TypeMappingDescriptor <Employee>().Properties(p => p
                                                                            .Object <object>(o => o.Name("data").Properties(p1 => p1
                                                                                                                            .Keyword(f => f.Name("Profile_URL").RootAlias("url"))))));

            var map = visitor.RootAliasMap;

            Assert.Equal(2, map.Count);
            Assert.True(map.ContainsKey("url"));
            Assert.Equal("data.Profile_URL", map["url"].Name);
            Assert.False(map["url"].HasChildMappings);

            Assert.True(map.ContainsKey("data"));
            Assert.Equal("data", map["data"].Name);
            Assert.False(map["data"].HasChildMappings);

            var parser = new LuceneQueryParser();
            var result = await parser.ParseAsync("url:test");

            var aliased = await AliasedQueryVisitor.RunAsync(result, map);

            Assert.Equal("data.Profile_URL:test", aliased.ToString());
        }
 public async Task AliasMapShouldBeAppliedToAllLevels2Async() {
     var parser = new LuceneQueryParser();
     var result = await parser.ParseAsync("(field1.nested:value field1.another:blah)");
     var aliasMap = new FieldMap {
         { "field1.nested", "field2.other" }
     };
     var aliased = await FieldResolverQueryVisitor.RunAsync(result, aliasMap);
     Assert.Equal("(field2.other:value field1.another:blah)", aliased.ToString());
 }
 public async Task AliasMapShouldBeAppliedToAllLevels8Async() {
     var parser = new LuceneQueryParser();
     var result = await parser.ParseAsync("field1.nested.childproperty:value");
     var aliasMap = new FieldMap {
         { "field1.nested.childproperty", "field2.other.childproperty" }
     };
     var aliased = await FieldResolverQueryVisitor.RunAsync(result, aliasMap);
     Assert.Equal("field2.other.childproperty:value", aliased.ToString());
 }
 public async Task AliasMapShouldAllowDeepAliasesAsync() {
     var parser = new LuceneQueryParser();
     var result = await parser.ParseAsync("level1.level2.level3.level4:value");
     var aliasMap = new FieldMap {
         { "level1.level2.level3.level4", "alias1.alias2.level3.level4" }
     };
     var aliased = await FieldResolverQueryVisitor.RunAsync(result, aliasMap);
     Assert.Equal("alias1.alias2.level3.level4:value", aliased.ToString());
 }
Exemplo n.º 13
0
        public async Task CanUseResolverAsync()
        {
            var parser = new LuceneQueryParser();
            var result = await parser.ParseAsync("field1.nested:value");

            var aliased = await AliasedQueryVisitor.RunAsync(result, f => f == "field1.nested"?new GetAliasResult { Name = "field2.nested" } : null);

            Assert.Equal("field2.nested:value", aliased.ToString());
        }
 public async Task CanApplyRootLevelAliasMapOnNestedTermAsync() {
     var parser = new LuceneQueryParser();
     var result = await parser.ParseAsync("field1.nested.morenested:value");
     var aliasMap = new FieldMap {
         { "field1.nested.morenested", "field2.nested.morenested" }
     };
     var aliased = await FieldResolverQueryVisitor.RunAsync(result, aliasMap);
     Assert.Equal("field2.nested.morenested:value", aliased.ToString());
 }
 public async Task AliasMapShouldWorkOnGroupsAsync() {
     var parser = new LuceneQueryParser();
     var result = await parser.ParseAsync("(field1.nested:value OR field1.thing:yep) another:works");
     var aliasMap = new FieldMap {
         { "field1.nested", "field2.other" },
         { "field1.thing", "field2.nice" }
     };
     var aliased = await FieldResolverQueryVisitor.RunAsync(result, aliasMap);
     Assert.Equal("(field2.other:value OR field2.nice:yep) another:works", aliased.ToString());
 }
        public async Task CanExpandIncludesAsync()
        {
            var parser = new LuceneQueryParser();
            var result = await parser.ParseAsync("@include:other");

            var includes = new Dictionary <string, string> {
                { "other", "field:value" }
            };
            var resolved = await IncludeVisitor.RunAsync(result, includes);

            Assert.Equal("(field:value)", resolved.ToString());
        }
Exemplo n.º 17
0
        public async Task AliasMapShouldBeAppliedToAllLevels3Async()
        {
            var parser = new LuceneQueryParser();
            var result = await parser.ParseAsync("field1.nested:value");

            var aliasMap = new AliasMap {
                { "nested", "field2" }
            };
            var aliased = await AliasedQueryVisitor.RunAsync(result, aliasMap);

            Assert.Equal("field1.nested:value", aliased.ToString());
        }
        public async Task CanExpandIncludesWithOtherCriteriaAndGroupingAsync()
        {
            var parser = new LuceneQueryParser();
            var result = await parser.ParseAsync("field1:value1 OR (@include:other field2:value2)");

            var includes = new Dictionary <string, string> {
                { "other", "field:value" }
            };
            var resolved = await IncludeVisitor.RunAsync(result, includes);

            Assert.Equal("field1:value1 OR ((field:value) field2:value2)", resolved.ToString());
        }
Exemplo n.º 19
0
        public async Task AliasMapShouldBeAppliedToAllLevels7Async()
        {
            var parser = new LuceneQueryParser();
            var result = await parser.ParseAsync("field1.nested.childproperty:value");

            var aliasMap = new AliasMap {
                { "field1", new AliasMapValue {
                      Name = "field2", ChildMap = { { "nested", "other" } }
                  } }
            };
            var aliased = await AliasedQueryVisitor.RunAsync(result, aliasMap);

            Assert.Equal("field2.other.childproperty:value", aliased.ToString());
        }
Exemplo n.º 20
0
        public async Task CanApplyRootLevelAliasMapOnNestedTermAsync()
        {
            var parser = new LuceneQueryParser();
            var result = await parser.ParseAsync("field1.nested.morenested:value");

            var aliasMap = new AliasMap {
                { "field1", new AliasMapValue {
                      Name = "field2", ChildMap = { { "stuff", "other" } }
                  } }
            };
            var aliased = await AliasedQueryVisitor.RunAsync(result, aliasMap);

            Assert.Equal("field2.nested.morenested:value", aliased.ToString());
        }
Exemplo n.º 21
0
        public async Task AliasMapShouldAllowDeepAliasesAsync()
        {
            var parser = new LuceneQueryParser();
            var result = await parser.ParseAsync("level1.level2.level3:(level4:value)");

            var aliasMap = new AliasMap {
                { "level1", new AliasMapValue {
                      Name = "alias1", ChildMap = { { "level2", "alias2" }, { "level4", "alias4" } }
                  } }
            };
            var aliased = await AliasedQueryVisitor.RunAsync(result, aliasMap);

            Assert.Equal("alias1.alias2.level3:(level4:value)", aliased.ToString());
        }
        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));
        }
Exemplo n.º 23
0
        public async Task AliasMapShouldWorkOnGroupsAsync()
        {
            var parser = new LuceneQueryParser();
            var result = await parser.ParseAsync("field1:(nested:value OR thing:yep) another:works");

            var aliasMap = new AliasMap {
                {
                    "field1",
                    new AliasMapValue {
                        Name = "field2", ChildMap = { { "nested", "other" }, { "thing", "nice" } }
                    }
                }
            };
            var aliased = await AliasedQueryVisitor.RunAsync(result, aliasMap);

            Assert.Equal("field2:(other:value OR nice:yep) another:works", aliased.ToString());
        }
        public override async Task VisitAsync(TermNode node, IQueryVisitorContext context)
        {
            if (node.Field != "@include" || (_shouldSkipInclude != null && _shouldSkipInclude(node, context)))
            {
                return;
            }

            var includeResolver = context.GetIncludeResolver();

            if (includeResolver == null)
            {
                return;
            }

            string includedQuery = await includeResolver(node.Term).ConfigureAwait(false);

            if (String.IsNullOrEmpty(includedQuery))
            {
                return;
            }

            var result = (GroupNode)await _parser.ParseAsync(includedQuery).ConfigureAwait(false);

            result.HasParens = true;
            await VisitAsync(result, context).ConfigureAwait(false);

            var parent = node.Parent as GroupNode;

            if (parent == null)
            {
                return;
            }

            if (parent.Left == node)
            {
                parent.Left = result;
            }
            else if (parent.Right == node)
            {
                parent.Right = result;
            }
        }
Exemplo n.º 25
0
        public async Task VerifyComplexNestedAliasAsync()
        {
            var client  = GetClient();
            var visitor = new AliasMappingVisitor(client.Infer);
            var walker  = new MappingWalker(visitor);

            walker.Accept(new TypeMappingDescriptor <Employee>().Properties(p => p
                                                                            .Object <object>(o1 => o1.Name("data").Properties(p1 => p1
                                                                                                                              .Object <object>(o2 => o2.Name("@request_info").Properties(p2 => p2
                                                                                                                                                                                         .Keyword(f => f.Name("user_agent").RootAlias("useragent"))
                                                                                                                                                                                         .Text(f4 => f4.Name("@browser_major_version").RootAlias("browser.major")
                                                                                                                                                                                               .Fields(sf => sf.Keyword(s => s.Name("keyword").IgnoreAbove(256))))
                                                                                                                                                                                         .Object <object>(o3 => o3.Name("data").Properties(p3 => p3
                                                                                                                                                                                                                                           .Keyword(f => f.Name("@is_bot").RootAlias("bot"))))))))));

            var map = visitor.RootAliasMap;

            Assert.Equal(4, map.Count);
            Assert.True(map.ContainsKey("useragent"));
            Assert.Equal("data.@request_info.user_agent", map["useragent"].Name);
            Assert.False(map["useragent"].HasChildMappings);

            Assert.True(map.ContainsKey("browser.major"));
            Assert.Equal("data.@request_info.@browser_major_version", map["browser.major"].Name);
            Assert.False(map["browser.major"].HasChildMappings);

            Assert.True(map.ContainsKey("bot"));
            Assert.Equal("data.@request_info.data.@is_bot", map["bot"].Name);
            Assert.False(map["bot"].HasChildMappings);

            Assert.True(map.ContainsKey("data"));
            Assert.Equal("data", map["data"].Name);
            Assert.True(map["data"].HasChildMappings);

            var parser = new LuceneQueryParser();
            var result = await parser.ParseAsync("useragent:test browser.major:10 bot:true");

            var aliased = await AliasedQueryVisitor.RunAsync(result, map);

            Assert.Equal("data.@request_info.user_agent:test data.@request_info.@browser_major_version:10 data.@request_info.data.@is_bot:true", aliased.ToString());
        }
        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);
        }
Exemplo n.º 28
0
        public async Task VerifySimpleAliasAsync()
        {
            var client  = GetClient();
            var visitor = new AliasMappingVisitor(client.Infer);
            var walker  = new MappingWalker(visitor);

            walker.Accept(new TypeMappingDescriptor <Employee>().Properties(p => p
                                                                            .Keyword(f => f.Name(e => e.Id).Alias("employee_id"))));

            var map = visitor.RootAliasMap;

            Assert.Single(map);
            Assert.True(map.ContainsKey("employee_id"));
            Assert.Equal("id", map["employee_id"].Name);
            Assert.False(map["employee_id"].HasChildMappings);

            var parser = new LuceneQueryParser();
            var result = await parser.ParseAsync("employee_id:1234");

            var aliased = await AliasedQueryVisitor.RunAsync(result, map);

            Assert.Equal("id:1234", aliased.ToString());
        }
        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);
            }
        }