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 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 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 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 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 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 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 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 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 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 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 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 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);
        }
Exemple #15
0
        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 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);
        }
Exemple #17
0
        public async Task BuildAsync <T>(QueryBuilderContext <T> ctx) where T : class, new()
        {
            var aggregationQuery = ctx.GetSourceAs <IAggregationQuery>();

            if (String.IsNullOrEmpty(aggregationQuery?.Aggregations))
            {
                return;
            }

            var opt = ctx.GetOptionsAs <IElasticQueryOptions>();

            if (opt?.AllowedAggregationFields?.Count > 0 && !(await GetAggregationFieldsAsync(aggregationQuery.Aggregations).AnyContext()).All(f => opt.AllowedAggregationFields.Contains(f)))
            {
                throw new InvalidOperationException("All aggregation fields must be allowed.");
            }

            var result = await _parser.BuildAggregationsAsync(aggregationQuery.Aggregations, ctx).AnyContext();

            ctx.Search.Aggregations(result);
        }
        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);
        }