public async Task CanExpandElasticIncludesAsync() { var client = new ElasticClient(new ConnectionSettings().DisableDirectStreaming().PrettyJson()); var aliases = new FieldMap { { "field", "aliased" }, { "included", "aliasedincluded" } }; var processor = new ElasticQueryParser(c => c.SetLoggerFactory(Log).UseIncludes(GetIncludeAsync).UseFieldMap(aliases)); var result = await processor.BuildQueryAsync("@include:other"); var actualResponse = client.Search <MyType>(d => d.Index("stuff").Query(q => result)); string actualRequest = actualResponse.GetRequest(); _logger.LogInformation("Actual: {Request}", actualRequest); var expectedResponse = client.Search <MyType>(d => d.Index("stuff").Query(f => f.Bool(b => b.Filter(f1 => f1.Term("aliasedincluded", "value"))))); string expectedRequest = expectedResponse.GetRequest(); _logger.LogInformation("Expected: {Request}", expectedRequest); Assert.Equal(expectedRequest, actualRequest); Assert.Equal(expectedResponse.Total, actualResponse.Total); result = await processor.BuildQueryAsync("@include:other"); actualResponse = client.Search <MyType>(d => d.Index("stuff").Query(q => result)); actualRequest = actualResponse.GetRequest(); _logger.LogInformation("Actual: {Request}", actualRequest); _logger.LogInformation("Expected: {Request}", expectedRequest); Assert.Equal(expectedRequest, actualRequest); Assert.Equal(expectedResponse.Total, actualResponse.Total); }
public void ProcessHistogramIntervalAggregations() { var client = GetClient(); var index = CreateRandomIndex <MyType>(client); client.IndexMany(new[] { new MyType { Field1 = "value1" } }, index); client.Indices.Refresh(index); var processor = new ElasticQueryParser(c => c.SetLoggerFactory(Log).UseMappings(client, index)); var aggregations = processor.BuildAggregationsAsync("histogram:(field1~0.1)").Result; var actualResponse = client.Search <MyType>(d => d.Index(index).Aggregations(aggregations)); string actualRequest = actualResponse.GetRequest(); _logger.LogInformation("Actual: {Request}", actualRequest); var expectedResponse = client.Search <MyType>(d => d.Index(index).Aggregations(a => a .Histogram("histogram_field1", t => t .Field("field1.keyword") .Interval(0.1) .MinimumDocumentCount(0) ))); string expectedRequest = expectedResponse.GetRequest(); _logger.LogInformation("Expected: {Request}", expectedRequest); Assert.Equal(expectedRequest, actualRequest); Assert.Equal(expectedResponse.Total, actualResponse.Total); }
public void ProcessTermTopHitsAggregations() { var client = GetClient(); var index = CreateRandomIndex <MyType>(client); client.IndexMany(new[] { new MyType { Field1 = "value1" } }, index); client.Indices.Refresh(index); var processor = new ElasticQueryParser(c => c.SetLoggerFactory(Log).UseMappings(client, index)); var aggregations = processor.BuildAggregationsAsync("terms:(field1~1000^2 tophits:(_~1000 @include:myinclude))").Result; var actualResponse = client.Search <MyType>(d => d.Index(index).Aggregations(aggregations)); string actualRequest = actualResponse.GetRequest(); _logger.LogInformation("Actual: {Request}", actualRequest); var expectedResponse = client.Search <MyType>(d => d.Index(index).Aggregations(a => a .Terms("terms_field1", t => t .Field("field1.keyword") .Size(1000) .MinimumDocumentCount(2) .Aggregations(a1 => a1.TopHits("tophits", t2 => t2.Size(1000).Source(s => s.Includes(i => i.Field("myinclude"))))) .Meta(m => m.Add("@field_type", "keyword"))))); string expectedRequest = expectedResponse.GetRequest(); _logger.LogInformation("Expected: {Request}", expectedRequest); Assert.Equal(expectedRequest, actualRequest); Assert.Equal(expectedResponse.Total, actualResponse.Total); }
public void ProcessSortedTermAggregations() { var client = GetClient(); var index = CreateRandomIndex <MyType>(client); client.IndexMany(new[] { new MyType { Field1 = "value1" } }, index); client.Indices.Refresh(index); var processor = new ElasticQueryParser(c => c.SetLoggerFactory(Log).UseMappings(client, index)); var aggregations = processor.BuildAggregationsAsync("terms:(field1 -cardinality:field4)").Result; var actualResponse = client.Search <MyType>(d => d.Index(index).Aggregations(aggregations)); string actualRequest = actualResponse.GetRequest(); _logger.LogInformation("Actual: {Request}", actualRequest); var expectedResponse = client.Search <MyType>(d => d.Index(index).Aggregations(a => a .Terms("terms_field1", t => t .Field("field1.keyword") .Order(o => o.Descending("cardinality_field4")) .Aggregations(a2 => a2 .Cardinality("cardinality_field4", c => c.Field("field4"))) .Meta(m => m.Add("@field_type", "keyword"))))); string expectedRequest = expectedResponse.GetRequest(); _logger.LogInformation("Expected: {Request}", expectedRequest); Assert.Equal(expectedRequest, actualRequest); Assert.True(actualResponse.IsValid); Assert.True(expectedResponse.IsValid); Assert.Equal(expectedResponse.Total, actualResponse.Total); }
public void CanSpecifyDefaultValuesAggregations() { var client = GetClient(); var index = CreateRandomIndex <MyType>(client); client.IndexMany(new[] { new MyType { Field1 = "test" }, new MyType { Field4 = 1 } }, index); client.Indices.Refresh(index); var processor = new ElasticQueryParser(c => c.SetLoggerFactory(Log).UseMappings(client, index)); var aggregations = processor.BuildAggregationsAsync("min:field4~0 max:field4~0 avg:field4~0 sum:field4~0 cardinality:field4~0").Result; var actualResponse = client.Search <MyType>(d => d.Index(index).Aggregations(aggregations)); string actualRequest = actualResponse.GetRequest(); _logger.LogInformation("Actual: {Request}", actualRequest); var expectedResponse = client.Search <MyType>(d => d.Index(index).Aggregations(a => a .Sum("sum_field4", c => c.Field("field4").Missing(0).Meta(m => m.Add("@field_type", "integer"))) .Cardinality("cardinality_field4", c => c.Field("field4").Missing(0)) .Average("avg_field4", c => c.Field("field4").Missing(0).Meta(m => m.Add("@field_type", "integer"))) .Max("max_field4", c => c.Field("field4").Missing(0).Meta(m => m.Add("@field_type", "integer"))) .Min("min_field4", c => c.Field("field4").Missing(0).Meta(m => m.Add("@field_type", "integer"))))); string expectedRequest = expectedResponse.GetRequest(); _logger.LogInformation("Expected: {Request}", expectedRequest); Assert.Equal(expectedRequest, actualRequest); Assert.True(actualResponse.IsValid); Assert.True(expectedResponse.IsValid); Assert.Equal(expectedResponse.Total, actualResponse.Total); }
public async Task GeoGridDoesNotResolveLocationForAggregation() { string index = nameof(MyType).ToLower(); var client = GetClient(); await client.DeleteIndexAsync(index); await client.RefreshAsync(index); var mapping = new TypeMappingDescriptor <MyType>() .Properties(p => p.GeoPoint(g => g.Name(f => f.Field1).RootAlias("geo"))); var visitor = new AliasMappingVisitor(client.Infer); var walker = new MappingWalker(visitor); walker.Accept(mapping); var processor = new ElasticQueryParser( c => c .UseGeo(l => "someinvalidvaluehere") .UseAliases(visitor.RootAliasMap) .UseMappings <MyType>(m => mapping, () => client.GetMapping(new GetMappingRequest(index, index)).Mapping)); await processor.BuildAggregationsAsync("geogrid:geo~3"); }
public void CanResolveIncludeToCustomFilterContainingIgnoredInclude() { var client = GetClient(); var index = CreateRandomIndex <MyType>(client); client.Index(new MyType { Id = "1" }, i => i.Index(index)); var processor = new ElasticQueryParser(c => c .SetLoggerFactory(Log) .UseIncludes(include => ResolveIncludeAsync("test", include, "@custom:(one @include:3)"), ShouldSkipInclude, 0) .AddVisitor(new CustomFilterVisitor(), 1)); var result = processor.BuildQueryAsync("@include:test").Result; var actualResponse = client.Search <MyType>(d => d.Index(index).Query(q => result)); string actualRequest = actualResponse.GetRequest(); _logger.LogInformation("Actual: {Request}", actualRequest); var expectedResponse = client.Search <MyType>(d => d .Index(index).Query(f => f.Bool(b => b.Filter(filter => filter.Terms(m => m.Field("id").Terms("1", "3")))))); string expectedRequest = expectedResponse.GetRequest(); _logger.LogInformation("Expected: {Request}", expectedRequest); Assert.Equal(expectedRequest, actualRequest); Assert.Equal(expectedResponse.Total, actualResponse.Total); }
public async Task ProcessSingleAggregationWithAlias() { var client = GetClient(); var index = CreateRandomIndex <MyType>(client); client.IndexMany(new[] { new MyType { Field2 = "field2" } }, index); await client.Indices.RefreshAsync(index); var aliasMap = new FieldMap { { "alias2", "field2" } }; var processor = new ElasticQueryParser(c => c.SetLoggerFactory(Log).UseMappings(client, index).UseFieldMap(aliasMap)); var aggregations = await processor.BuildAggregationsAsync("missing:alias2"); var actualResponse = client.Search <MyType>(d => d.Index(index).Aggregations(aggregations)); string actualRequest = actualResponse.GetRequest(); _logger.LogInformation("Actual: {Request}", actualRequest); var expectedResponse = client.Search <MyType>(d => d.Index(index).Aggregations(a => a .Missing("missing_alias2", t => t.Field("field2.keyword")))); string expectedRequest = expectedResponse.GetRequest(); _logger.LogInformation("Expected: {Request}", expectedRequest); Assert.Equal(expectedRequest, actualRequest); Assert.True(actualResponse.IsValid); Assert.True(expectedResponse.IsValid); Assert.Equal(expectedResponse.Total, actualResponse.Total); }
public void ProcessTermAggregations() { var client = GetClient(); var index = CreateRandomIndex <MyType>(client); client.IndexMany(new[] { new MyType { Field1 = "value1" } }, index); client.Indices.Refresh(index); var processor = new ElasticQueryParser(c => c.SetLoggerFactory(Log).UseMappings(client, index)); var aggregations = processor.BuildAggregationsAsync("terms:(field1 @exclude:myexclude @include:myinclude @include:otherinclude @missing:mymissing @exclude:otherexclude @min:1)").Result; var actualResponse = client.Search <MyType>(d => d.Index(index).Aggregations(aggregations)); string actualRequest = actualResponse.GetRequest(); _logger.LogInformation("Actual: {Request}", actualRequest); var expectedResponse = client.Search <MyType>(d => d.Index(index).Aggregations(a => a .Terms("terms_field1", t => t .Field("field1.keyword") .MinimumDocumentCount(1) .Include(new[] { "otherinclude", "myinclude" }) .Exclude(new[] { "otherexclude", "myexclude" }) .Missing("mymissing") .Meta(m => m.Add("@field_type", "keyword"))))); string expectedRequest = expectedResponse.GetRequest(); _logger.LogInformation("Expected: {Request}", expectedRequest); Assert.Equal(expectedRequest, actualRequest); Assert.Equal(expectedResponse.Total, actualResponse.Total); }
public async Task ProcessAggregationsAsync() { var client = GetClient(); client.DeleteIndex("stuff"); client.Refresh("stuff"); client.CreateIndex("stuff"); client.Map <MyType>(d => d.Dynamic(true).Index("stuff").Properties(p => p.GeoPoint(g => g.Name(f => f.Field3)))); var res = client.IndexMany(new List <MyType> { new MyType { Field1 = "value1", Field4 = 1, Field3 = "51.5032520,-0.1278990", Field5 = DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(5)), Field2 = "field2" }, new MyType { Field1 = "value2", Field4 = 2, Field3 = "51.5032520,-0.1278990", Field5 = DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(4)) }, new MyType { Field1 = "value3", Field4 = 3, Field3 = "51.5032520,-0.1278990", Field5 = DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(3)) }, new MyType { Field1 = "value4", Field4 = 4, Field3 = "51.5032520,-0.1278990", Field5 = DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(2)) }, new MyType { Field1 = "value5", Field4 = 5, Field3 = "51.5032520,-0.1278990", Field5 = DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(1)) } }, "stuff"); client.Refresh("stuff"); var processor = new ElasticQueryParser(c => c.UseMappings <MyType>(client, "stuff").UseGeo(l => "51.5032520,-0.1278990")); var aggregations = await processor.BuildAggregationsAsync("min:field4 max:field4 avg:field4 sum:field4 percentiles:field4~50,100 cardinality:field4 missing:field2 date:field5 histogram:field4 geogrid:field3 terms:field1"); var actualResponse = client.Search <MyType>(d => d.Index("stuff").Aggregations(aggregations)); string actualRequest = actualResponse.GetRequest(); _logger.LogInformation("Actual: {Request}", actualResponse); var expectedResponse = client.Search <MyType>(d => d.Index("stuff").Aggregations(a => a .GeoHash("geogrid_field3", h => h.Field("field3").GeoHashPrecision(GeoHashPrecision.Precision1) .Aggregations(a1 => a1.Average("avg_lat", s => s.Script(ss => ss.Inline("doc['field3'].lat"))).Average("avg_lon", s => s.Script(ss => ss.Inline("doc['field3'].lon"))))) .Terms("terms_field1", t => t.Field("field1.keyword").Meta(m => m.Add("@field_type", "keyword"))) .Histogram("histogram_field4", h => h.Field("field4").Interval(50).MinimumDocumentCount(0)) .DateHistogram("date_field5", d1 => d1.Field("field5").Interval("1d").Format("date_optional_time").MinimumDocumentCount(0)) .Missing("missing_field2", t => t.Field("field2.keyword")) .Cardinality("cardinality_field4", c => c.Field("field4")) .Percentiles("percentiles_field4", c => c.Field("field4").Percents(50, 100)) .Sum("sum_field4", c => c.Field("field4").Meta(m => m.Add("@field_type", "long"))) .Average("avg_field4", c => c.Field("field4").Meta(m => m.Add("@field_type", "long"))) .Max("max_field4", c => c.Field("field4").Meta(m => m.Add("@field_type", "long"))) .Min("min_field4", c => c.Field("field4").Meta(m => m.Add("@field_type", "long"))))); string expectedRequest = expectedResponse.GetRequest(); _logger.LogInformation("Actual: {Request}", expectedRequest); Assert.Equal(expectedRequest, actualRequest); Assert.True(actualResponse.IsValid); Assert.True(expectedResponse.IsValid); Assert.Equal(expectedResponse.Total, actualResponse.Total); }
public void UseQueryParser(ElasticQueryParser parser) { Unregister <ExpressionQueryBuilder>(); Register(new ParsedExpressionQueryBuilder(parser)); Unregister <AggregationsQueryBuilder>(); Register(new AggregationsQueryBuilder()); }
public async Task CanQueryByAliasAsyncAsync() { string index = nameof(Employee).ToLower(); var client = GetClient(); await client.DeleteIndexAsync(index); await client.RefreshAsync(index); var mapping = new TypeMappingDescriptor <Employee>() .Properties(p => p .Keyword(f => f.Name(e => e.Id).Alias("employee_id")) .Object <object>(o => o.Name("data").Properties(p1 => p1 .Keyword(f => f.Name("Profile_URL").RootAlias("url")) .Keyword(f => f.Name("Security_Access_Code").Alias("code"))))); await client.CreateIndexAsync(index, d => d.Mappings(m => m.Map <Employee>(index, md => mapping))); var response = client.IndexMany(new List <Employee> { new Employee { Id = "ex-001", Data = new Dictionary <string, object> { { "Profile_URL", "/u/ex-001/profile.png" }, { "Security_Access_Code", "1234567890" } } }, }, index); await client.RefreshAsync(index); var visitor = new AliasMappingVisitor(client.Infer); var walker = new MappingWalker(visitor); walker.Accept(mapping); var processor = new ElasticQueryParser(c => c.UseAliases(visitor.RootAliasMap).UseMappings <Employee>(m => mapping, () => client.GetMapping(new GetMappingRequest(index, index)).Mapping)); var result = await processor.BuildQueryAsync("employee_id:ex-001 url:\"/u/ex-001/profile.png\" data.code:1234567890"); var actualResponse = client.Search <Employee>(d => d.Index(index).Query(q => result)); string actualRequest = actualResponse.GetRequest(); _logger.LogInformation("Actual: {Request}", actualResponse); var expectedResponse = client.Search <Employee>(d => d.Index(index).Type(index).Query(q => q .Bool(b => b.Filter( Query <Employee> .Term(f1 => f1.Id, "ex-001") && Query <Employee> .Term(f1 => f1.Data["Profile_URL"], "/u/ex-001/profile.png") && Query <Employee> .Term(f1 => f1.Data["Security_Access_Code"], "1234567890"))) )); string expectedRequest = expectedResponse.GetRequest(); _logger.LogInformation("Actual: {Request}", expectedRequest); Assert.Equal(expectedRequest, actualRequest); Assert.True(actualResponse.IsValid); Assert.True(expectedResponse.IsValid); Assert.Equal(expectedResponse.Total, actualResponse.Total); }
public void CanUseElasticQueryParser() { var sut = new ElasticQueryParser(); var result = sut.Parse("NOT (dog parrot)"); Assert.IsType <GroupNode>(result.Left); Assert.True((result.Left as GroupNode).HasParens); Assert.True((result.Left as GroupNode).IsNegated); }
public void UseQueryParser(Action <ElasticQueryParserConfiguration> configure) { var parser = new ElasticQueryParser(configure); Unregister <SearchQueryBuilder>(); Register(new ParsedSearchQueryBuilder(parser)); Unregister <AggregationsQueryBuilder>(); Register(new AggregationsQueryBuilder(parser)); }
protected virtual ElasticQueryParser CreateQueryParser() { var parser = new ElasticQueryParser(config => { config.UseMappings(this); config.UseNested(); Configuration.ConfigureGlobalQueryParsers(config); ConfigureQueryParser(config); }); return(parser); }
public async Task ProcessAggregationsWithAliasesAsync() { var client = GetClient(); client.DeleteIndex("stuff"); client.Refresh("stuff"); client.CreateIndex("stuff"); client.Map <MyType>(d => d.Dynamic(true).Index("stuff").Properties(p => p .GeoPoint(g => g.Name(f => f.Field3)) .Object <Dictionary <string, object> >(o1 => o1.Name(f1 => f1.Data).Properties(p1 => p1 .Object <object>(o2 => o2.Name("@user").Properties(p2 => p2 .Text(f3 => f3.Name("identity").Fields(f => f.Keyword(k => k.Name("keyword").IgnoreAbove(256)))))))))); var res = client.IndexMany(new List <MyType> { new MyType { Field1 = "value1", Field4 = 1, Field3 = "51.5032520,-0.1278990", Field5 = DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(5)), Field2 = "field2" } }, "stuff"); client.Refresh("stuff"); var aliasMap = new AliasMap { { "user", "*****@*****.**" }, { "alias1", "field1" }, { "alias2", "field2" }, { "alias3", "field3" }, { "alias4", "field4" }, { "alias5", "field5" } }; var processor = new ElasticQueryParser(c => c.UseMappings <MyType>(client, "stuff").UseGeo(l => "51.5032520,-0.1278990").UseAliases(aliasMap)); var aggregations = await processor.BuildAggregationsAsync("min:alias4 max:alias4 avg:alias4 sum:alias4 percentiles:alias4 cardinality:user missing:alias2 date:alias5 histogram:alias4 geogrid:alias3 terms:alias1"); var actualResponse = client.Search <MyType>(d => d.Index("stuff").Aggregations(aggregations)); string actualRequest = actualResponse.GetRequest(); _logger.LogInformation("Actual: {Request}", actualResponse); var expectedResponse = client.Search <MyType>(d => d.Index("stuff").Aggregations(a => a .GeoHash("geogrid_alias3", h => h.Field("field3").GeoHashPrecision(GeoHashPrecision.Precision1) .Aggregations(a1 => a1.Average("avg_lat", s => s.Script(ss => ss.Inline("doc['field3'].lat"))).Average("avg_lon", s => s.Script(ss => ss.Inline("doc['field3'].lon"))))) .Terms("terms_alias1", t => t.Field("field1.keyword").Meta(m => m.Add("@field_type", "keyword"))) .Histogram("histogram_alias4", h => h.Field("field4").Interval(50).MinimumDocumentCount(0)) .DateHistogram("date_alias5", d1 => d1.Field("field5").Interval("1d").Format("date_optional_time").MinimumDocumentCount(0)) .Missing("missing_alias2", t => t.Field("field2.keyword")) .Cardinality("cardinality_user", c => c.Field("*****@*****.**")) .Percentiles("percentiles_alias4", c => c.Field("field4")) .Sum("sum_alias4", c => c.Field("field4").Meta(m => m.Add("@field_type", "long"))) .Average("avg_alias4", c => c.Field("field4").Meta(m => m.Add("@field_type", "long"))) .Max("max_alias4", c => c.Field("field4").Meta(m => m.Add("@field_type", "long"))) .Min("min_alias4", c => c.Field("field4").Meta(m => m.Add("@field_type", "long"))))); string expectedRequest = expectedResponse.GetRequest(); _logger.LogInformation("Actual: {Request}", expectedRequest); Assert.Equal(expectedRequest, actualRequest); Assert.True(actualResponse.IsValid); Assert.True(expectedResponse.IsValid); Assert.Equal(expectedResponse.Total, actualResponse.Total); }
public async Task CanUseAliasMapForTopLevelAlias2Async() { string filter = "program:postgrad"; var aliasMap = new FieldMap { { "program", "programName" } }; var p = new ElasticQueryParser(c => c.UseFieldMap(aliasMap)); IQueryContainer query = await p.BuildQueryAsync(filter); var term = query.Bool.Filter.Single() as IQueryContainer; Assert.NotNull(term.Term); Assert.Equal("programName", term.Term.Field.Name); Assert.Equal("postgrad", term.Term.Value); }
protected virtual ElasticQueryParser CreateQueryParser() { var parser = new ElasticQueryParser(config => { config.SetLoggerFactory(Configuration.LoggerFactory); config.UseFieldResolver(_fieldResolver.Value); config.UseNested(); config.UseMappings(_mappingResolver.Value); Configuration.ConfigureGlobalQueryParsers(config); ConfigureQueryParser(config); }); return(parser); }
public Task GeoGridDoesNotResolveLocationForAggregation() { var client = GetClient(); var index = CreateRandomIndex <MyType>(client, d => d.Properties(p => p .GeoPoint(g => g.Name(f => f.Field1)) .FieldAlias(a => a.Name("geo").Path(f => f.Field1)))); var processor = new ElasticQueryParser(c => c .UseGeo(l => "someinvalidvaluehere") .UseMappings(client, index)); return(processor.BuildAggregationsAsync("geogrid:geo~3")); }
public void CanUseElasticQueryParserWithVisitor() { var testQueryVisitor = new TestQueryVisitor(); var sut = new ElasticQueryParser(c => c.AddQueryVisitor(testQueryVisitor)); var context = new ElasticQueryVisitorContext(); var result = sut.Parse("NOT (dog parrot)", context) as GroupNode; Assert.Equal(2, testQueryVisitor.GroupNodeCount); Assert.IsType <GroupNode>(result.Left); Assert.True((result.Left as GroupNode).HasParens); Assert.True((result.Left as GroupNode).IsNegated); }
public async Task ProcessAnalyzedAggregationWithAliasAsync() { var client = GetClient(); var index = CreateRandomIndex <MyType>(client, d => d.Dynamic().Properties(p => p .Text(f => f.Name(n => n.Field1) .Fields(k => k.Keyword(m => m.Name("keyword")))) .FieldAlias(f => f.Name("heynow").Path(k => k.Field1)) .GeoPoint(g => g.Name(f => f.Field3)))); client.IndexMany(new[] { new MyType { Field1 = "value1", Field4 = 1, Field3 = "51.5032520,-0.1278990", Field5 = DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(5)), Field2 = "field2" }, new MyType { Field1 = "value2", Field4 = 2, Field3 = "51.5032520,-0.1278990", Field5 = DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(4)) }, new MyType { Field1 = "value3", Field4 = 3, Field3 = "51.5032520,-0.1278990", Field5 = DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(3)) }, new MyType { Field1 = "value4", Field4 = 4, Field3 = "51.5032520,-0.1278990", Field5 = DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(2)) }, new MyType { Field1 = "value5", Field4 = 5, Field3 = "51.5032520,-0.1278990", Field5 = DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(1)) } }, index); await client.Indices.RefreshAsync(index); var fieldMap = new FieldMap { { "heynow2", "field1" } }; var processor = new ElasticQueryParser(c => c.SetLoggerFactory(Log).UseMappings(client, index).UseFieldMap(fieldMap)); var aggregations = await processor.BuildAggregationsAsync("terms:heynow"); var actualResponse = client.Search <MyType>(d => d.Index(index).Aggregations(aggregations)); string actualRequest = actualResponse.GetRequest(); _logger.LogInformation("Actual: {Request}", actualRequest); var expectedResponse = client.Search <MyType>(d => d.Index(index).Aggregations(a => a .Terms("terms_heynow", c => c.Field("field1.keyword").Meta(m => m.Add("@field_type", "text"))))); string expectedRequest = expectedResponse.GetRequest(); _logger.LogInformation("Expected: {Request}", expectedRequest); Assert.Equal(expectedRequest, actualRequest); Assert.True(actualResponse.IsValid); Assert.True(expectedResponse.IsValid); Assert.Equal(expectedResponse.Total, actualResponse.Total); }
public void CanResolveMultipleCustomFilters() { var client = GetClient(); var index = CreateRandomIndex <MyType>(client); client.Index(new MyType { Id = "1" }, i => i.Index(index)); var processor = new ElasticQueryParser(c => c .SetLoggerFactory(Log) .AddVisitor(new IncludeVisitor()) .AddVisitor(new CustomFilterVisitor())); var result = processor.BuildQueryAsync("@custom:(one) OR (field1:Test @custom:(two))").Result; var actualResponse = client.Search <MyType>(d => d.Index(index).Query(q => result)); string actualRequest = actualResponse.GetRequest(); _logger.LogInformation("Actual: {Request}", actualRequest); var expectedResponse = client.Search <MyType>(d => d.Index(index) .Query(f => f .Bool(b => b .Filter(filter => filter .Bool(b1 => b1 .Should( s1 => s1.Terms(m => m.Field("id").Terms("1")), s2 => s2.Bool(b2 => b2 .Must( m2 => m2.Terms(t1 => t1.Field("id").Terms("2")), m2 => m2.Term(t1 => t1.Field("field1").Value("Test")) ) ) ) ) ) ) ) ); string expectedRequest = expectedResponse.GetRequest(); _logger.LogInformation("Expected: {Request}", expectedRequest); Assert.Equal(expectedRequest, actualRequest); Assert.Equal(expectedResponse.Total, actualResponse.Total); }
public void ProcessDateHistogramAggregations() { var client = GetClient(); client.DeleteIndex("stuff"); client.Refresh("stuff"); client.CreateIndex("stuff"); client.Map <MyType>(d => d.Dynamic(true).Index("stuff")); var res = client.IndexMany(new List <MyType> { new MyType { Field5 = SystemClock.UtcNow } }, "stuff"); client.Refresh("stuff"); var processor = new ElasticQueryParser(c => c.UseMappings <MyType>(client, "stuff")); var aggregations = processor.BuildAggregationsAsync("date:(field5^1h @missing:\"0001-01-01T00:00:00\" min:field5^1h max:field5^1h)").Result; var actualResponse = client.Search <MyType>(d => d.Index("stuff").Aggregations(aggregations)); string actualRequest = actualResponse.GetRequest(); _logger.LogInformation("Actual: {Request}", actualResponse); var expectedResponse = client.Search <MyType>(d => d.Index("stuff").Aggregations(a => a .DateHistogram("date_field5", d1 => d1 .Field("field5").Meta(m => m.Add("@timezone", "1h")) .Interval("1d") .Format("date_optional_time") .MinimumDocumentCount(0) .TimeZone("+01:00") .Missing(DateTime.MinValue) .Aggregations(a1 => a1 .Min("min_field5", s => s.Field(f => f.Field5).Meta(m => m.Add("@field_type", "date").Add("@timezone", "1h"))) .Max("max_field5", s => s.Field(f => f.Field5).Meta(m => m.Add("@field_type", "date").Add("@timezone", "1h"))))))); string expectedRequest = expectedResponse.GetRequest(); _logger.LogInformation("Actual: {Request}", expectedRequest); Assert.Equal(expectedRequest, actualRequest); Assert.True(actualResponse.IsValid, actualResponse.DebugInformation); Assert.True(expectedResponse.IsValid); Assert.Equal(expectedResponse.Total, actualResponse.Total); }
public async Task ProcessNestedAggregationsWithAliasesAsync() { var client = GetClient(); client.DeleteIndex("stuff"); client.Refresh("stuff"); client.CreateIndex("stuff"); client.Map <MyType>(d => d.Dynamic(true).Index("stuff").Properties(p => p .GeoPoint(g => g.Name(f => f.Field3)) .Object <Dictionary <string, object> >(o1 => o1.Name(f1 => f1.Data).Properties(p1 => p1 .Object <object>(o2 => o2.Name("@user").Properties(p2 => p2 .Text(f3 => f3.Name("identity").Fields(f => f.Keyword(k => k.Name("keyword").IgnoreAbove(256)))))))))); var res = client.IndexMany(new List <MyType> { new MyType { Field1 = "value1" } }, "stuff"); client.Refresh("stuff"); var aliasMap = new AliasMap { { "user", "*****@*****.**" }, { "alias1", "field1" } }; var processor = new ElasticQueryParser(c => c.UseMappings <MyType>(client, "stuff").UseAliases(aliasMap)); var aggregations = await processor.BuildAggregationsAsync("terms:(alias1 cardinality:user)"); var actualResponse = client.Search <MyType>(d => d.Index("stuff").Aggregations(aggregations)); string actualRequest = actualResponse.GetRequest(); _logger.LogInformation("Actual: {Request}", actualResponse); var expectedResponse = client.Search <MyType>(d => d.Index("stuff").Aggregations(a => a .Terms("terms_alias1", t => t.Field("field1.keyword").Meta(m => m.Add("@field_type", "keyword")) .Aggregations(a1 => a1.Cardinality("cardinality_user", c => c.Field("*****@*****.**")))))); string expectedRequest = expectedResponse.GetRequest(); _logger.LogInformation("Actual: {Request}", expectedRequest); Assert.Equal(expectedRequest, actualRequest); Assert.True(actualResponse.IsValid); Assert.True(expectedResponse.IsValid); Assert.Equal(expectedResponse.Total, actualResponse.Total); }
public void ProcessSortedTermAggregations() { var client = GetClient(); client.DeleteIndex("stuff"); client.Refresh("stuff"); client.CreateIndex("stuff"); client.Map <MyType>(d => d.Dynamic(true).Index("stuff")); var res = client.IndexMany(new List <MyType> { new MyType { Field1 = "value1" } }, "stuff"); client.Refresh("stuff"); var processor = new ElasticQueryParser(c => c.UseMappings <MyType>(client, "stuff")); var aggregations = processor.BuildAggregationsAsync("terms:(field1 -cardinality:field4)").Result; var actualResponse = client.Search <MyType>(d => d.Index("stuff").Aggregations(aggregations)); string actualRequest = actualResponse.GetRequest(); _logger.LogInformation("Actual: {Request}", actualResponse); var expectedResponse = client.Search <MyType>(d => d.Index("stuff").Aggregations(a => a .Terms("terms_field1", t => t .Field("field1.keyword") .Order(new TermsOrder { Key = "cardinality_field4", Order = SortOrder.Descending }) .Aggregations(a2 => a2 .Cardinality("cardinality_field4", c => c.Field("field4"))) .Meta(m => m.Add("@field_type", "keyword"))))); string expectedRequest = expectedResponse.GetRequest(); _logger.LogInformation("Actual: {Request}", expectedRequest); Assert.Equal(expectedRequest, actualRequest); Assert.True(actualResponse.IsValid); Assert.True(expectedResponse.IsValid); Assert.Equal(expectedResponse.Total, actualResponse.Total); }
public async Task ProcessSingleAggregationAsync() { var client = GetClient(); var index = CreateRandomIndex <MyType>(client, d => d.Dynamic().Properties(p => p.GeoPoint(g => g.Name(f => f.Field3)))); client.IndexMany(new[] { new MyType { Field1 = "value1", Field4 = 1, Field3 = "51.5032520,-0.1278990", Field5 = DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(5)), Field2 = "field2" }, new MyType { Field1 = "value2", Field4 = 2, Field3 = "51.5032520,-0.1278990", Field5 = DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(4)) }, new MyType { Field1 = "value3", Field4 = 3, Field3 = "51.5032520,-0.1278990", Field5 = DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(3)) }, new MyType { Field1 = "value4", Field4 = 4, Field3 = "51.5032520,-0.1278990", Field5 = DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(2)) }, new MyType { Field1 = "value5", Field4 = 5, Field3 = "51.5032520,-0.1278990", Field5 = DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(1)) } }, index); await client.Indices.RefreshAsync(index); var processor = new ElasticQueryParser(c => c.SetLoggerFactory(Log).UseMappings(client, index).UseGeo(l => "51.5032520,-0.1278990")); var aggregations = await processor.BuildAggregationsAsync("min:field4"); var actualResponse = client.Search <MyType>(d => d.Index(index).Aggregations(aggregations)); string actualRequest = actualResponse.GetRequest(); _logger.LogInformation("Actual: {Request}", actualRequest); var expectedResponse = client.Search <MyType>(d => d.Index(index).Aggregations(a => a .Min("min_field4", c => c.Field("field4").Meta(m => m.Add("@field_type", "long"))))); string expectedRequest = expectedResponse.GetRequest(); _logger.LogInformation("Expected: {Request}", expectedRequest); Assert.Equal(expectedRequest, actualRequest); Assert.True(actualResponse.IsValid); Assert.True(expectedResponse.IsValid); Assert.Equal(expectedResponse.Total, actualResponse.Total); }
public void CanSpecifyDefaultValuesAggregations() { var client = GetClient(); client.DeleteIndex("stuff"); client.Refresh("stuff"); client.CreateIndex("stuff"); client.Map <MyType>(d => d.Dynamic(true).Index("stuff")); var res = client.IndexMany(new List <MyType> { new MyType { Field1 = "test" }, new MyType { Field4 = 1 } }, "stuff"); client.Refresh("stuff"); var processor = new ElasticQueryParser(c => c.UseMappings <MyType>(client, "stuff")); var aggregations = processor.BuildAggregationsAsync("min:field4~0 max:field4~0 avg:field4~0 sum:field4~0 cardinality:field4~0").Result; var actualResponse = client.Search <MyType>(d => d.Index("stuff").Aggregations(aggregations)); string actualRequest = actualResponse.GetRequest(); _logger.LogInformation("Actual: {Request}", actualResponse); var expectedResponse = client.Search <MyType>(d => d.Index("stuff").Aggregations(a => a .Sum("sum_field4", c => c.Field("field4").Missing(0).Meta(m => m.Add("@field_type", "long"))) .Cardinality("cardinality_field4", c => c.Field("field4").Missing(0)) .Average("avg_field4", c => c.Field("field4").Missing(0).Meta(m => m.Add("@field_type", "long"))) .Max("max_field4", c => c.Field("field4").Missing(0).Meta(m => m.Add("@field_type", "long"))) .Min("min_field4", c => c.Field("field4").Missing(0).Meta(m => m.Add("@field_type", "long"))))); string expectedRequest = expectedResponse.GetRequest(); _logger.LogInformation("Actual: {Request}", expectedRequest); Assert.Equal(expectedRequest, actualRequest); Assert.True(actualResponse.IsValid); Assert.True(expectedResponse.IsValid); Assert.Equal(expectedResponse.Total, actualResponse.Total); }
public void ProcessTermAggregations() { var client = GetClient(); client.DeleteIndex("stuff"); client.Refresh("stuff"); client.CreateIndex("stuff"); client.Map <MyType>(d => d.Dynamic(true).Index("stuff")); var res = client.IndexMany(new List <MyType> { new MyType { Field1 = "value1" } }, "stuff"); client.Refresh("stuff"); var processor = new ElasticQueryParser(c => c.UseMappings <MyType>(client, "stuff")); var aggregations = processor.BuildAggregationsAsync("terms:(field1 @exclude:myexclude @include:myinclude @missing:mymissing @min:1)").Result; var actualResponse = client.Search <MyType>(d => d.Index("stuff").Aggregations(aggregations)); string actualRequest = actualResponse.GetRequest(); _logger.LogInformation("Actual: {Request}", actualResponse); var expectedResponse = client.Search <MyType>(d => d.Index("stuff").Aggregations(a => a .Terms("terms_field1", t => t .Field("field1.keyword") .MinimumDocumentCount(1) .Include("myinclude") .Exclude("myexclude") .Missing("mymissing") .Meta(m => m.Add("@field_type", "keyword"))))); string expectedRequest = expectedResponse.GetRequest(); _logger.LogInformation("Actual: {Request}", expectedRequest); Assert.Equal(expectedRequest, actualRequest); Assert.Equal(expectedResponse.Total, actualResponse.Total); }
public void ProcessHistogramIntervalAggregations() { var client = GetClient(); client.DeleteIndex("stuff"); client.Refresh("stuff"); client.CreateIndex("stuff"); client.Map <MyType>(d => d.Dynamic(true).Index("stuff")); var res = client.IndexMany(new List <MyType> { new MyType { Field1 = "value1" } }, "stuff"); client.Refresh("stuff"); var processor = new ElasticQueryParser(c => c.UseMappings <MyType>(client, "stuff")); var aggregations = processor.BuildAggregationsAsync("histogram:(field1~0.1)").Result; var actualResponse = client.Search <MyType>(d => d.Index("stuff").Aggregations(aggregations)); string actualRequest = actualResponse.GetRequest(); _logger.LogInformation("Actual: {Request}", actualResponse); var expectedResponse = client.Search <MyType>(d => d.Index("stuff").Aggregations(a => a .Histogram("histogram_field1", t => t .Field("field1.keyword") .Interval(0.1) .MinimumDocumentCount(0) ))); string expectedRequest = expectedResponse.GetRequest(); _logger.LogInformation("Actual: {Request}", expectedRequest); Assert.Equal(expectedRequest, actualRequest); Assert.Equal(expectedResponse.Total, actualResponse.Total); }
public PersistentEventQueryValidatorTests(ITestOutputHelper output) : base(output) { _parser = GetService <ExceptionlessElasticConfiguration>().Events.Event.QueryParser; _validator = GetService <PersistentEventQueryValidator>(); }