public void SelectProjectionCanCreateMixedFacets()
        {
            var query = Robots.GroupBy(r => 2)
                        .Select(g => new
            {
                AverageEnergyUse = g.Average(a => a.EnergyUse),
                CountHighEnergy  = g.Count(a => a.EnergyUse > 50.0)
            });

            var translation = ElasticQueryTranslator.Translate(Mapping, query.Expression);

            Assert.Contains("AnonymousType", Assert.IsType <ListTermlessFacetsElasticMaterializer>(translation.Materializer).ElementType.FullName);
            Assert.Equal("count", translation.SearchRequest.SearchType);
            Assert.Equal(2, translation.SearchRequest.Facets.Count);

            {
                var termsStatsFacet = translation.SearchRequest.Facets.OfType <StatisticalFacet>().Single();
                Assert.Contains("energyUse", termsStatsFacet.Fields);
                Assert.Null(termsStatsFacet.Filter);
            }

            {
                var termsFacet = translation.SearchRequest.Facets.OfType <FilterFacet>().Single();
                var range      = Assert.IsType <RangeCriteria>(termsFacet.Filter);
                Assert.Equal("energyUse", range.Field);
                Assert.Equal(1, range.Specifications.Count);
                var specification = range.Specifications[0];
                Assert.Equal(RangeComparison.GreaterThan, specification.Comparison);
                Assert.Equal(50.0, specification.Value);
            }
        }
        public void SelectProjectionCanUseTupleCreateWithCountPredicate()
        {
            var query = Robots.GroupBy(g => 1)
                        .Select(g => Tuple.Create(g.Key, g.Count(r => r.EnergyUse > 1.5), g.Count(r => r.EnergyUse <= 2.0)));

            TestProjectionWithCountPredicate(query.Expression);
        }
Esempio n. 3
0
        public void SelectCountPredicateCreatesTermsFacetWithFilter()
        {
            var query = Robots.GroupBy(r => r.Zone).Select(g => g.Count(r => r.Cost > 5m));

            var translation = ElasticQueryTranslator.Translate(Mapping, "a", query.Expression);

            Assert.Equal(typeof(int), Assert.IsType <ListTermFacetsElasticMaterializer>(translation.Materializer).ElementType);
            Assert.Equal("count", translation.SearchRequest.SearchType);
            Assert.Equal(1, translation.SearchRequest.Facets.Count);

            var facet = Assert.IsType <TermsFacet>(translation.SearchRequest.Facets[0]);

            Assert.Equal(1, facet.Fields.Count);
            Assert.Equal("a.zone", facet.Fields[0]);

            var filter = Assert.IsType <RangeCriteria>(facet.Filter);

            Assert.Equal("a.cost", filter.Field);
            Assert.Equal(1, filter.Specifications.Count);

            var specification = filter.Specifications[0];

            Assert.Equal("gt", specification.Name);
            Assert.Equal(5m, specification.Value);
        }
Esempio n. 4
0
        public void SelectProjectionCreatesMultipleFacets()
        {
            var expectedFields = new[] { "cost", "energyUse", "started" };
            var query          = Robots.GroupBy(r => r.Zone)
                                 .Select(g => new
            {
                Count            = g.Count(),
                SumCost          = g.Sum(a => a.Cost),
                AverageEnergyUse = g.Average(a => a.EnergyUse),
                MinStarted       = g.Min(a => a.Started),
            });

            var translation = ElasticQueryTranslator.Translate(Mapping, "", query.Expression);

            Assert.Contains("AnonymousType", Assert.IsType <ListTermFacetsElasticMaterializer>(translation.Materializer).ElementType.FullName);
            Assert.Equal("count", translation.SearchRequest.SearchType);
            Assert.Equal(expectedFields.Length + 1, translation.SearchRequest.Facets.Count);

            foreach (var expectedField in expectedFields)
            {
                var facet = translation.SearchRequest.Facets.OfType <TermsStatsFacet>().Single(s => s.Value == expectedField);
                Assert.Equal("zone", facet.Key);
                Assert.Null(facet.Filter);
            }

            var countTerms = translation.SearchRequest.Facets.OfType <TermsFacet>().Single();

            Assert.Contains("zone", countTerms.Fields);
            Assert.Null(countTerms.Filter);
        }
        public void SelectProjectionCanUseAnonymousConstructorWithCountPredicate()
        {
            var query = Robots.GroupBy(g => 1)
                        .Select(g => new { g.Key, High = g.Count(r => r.EnergyUse > 1.5), Low = g.Count(r => r.EnergyUse <= 2.0) });

            TestProjectionWithCountPredicate(query.Expression);
        }
Esempio n. 6
0
        public void SelectStillUsesTypeCriteria()
        {
            var query = Robots.GroupBy(r => r.Zone).Select(g => g.Average(r => r.EnergyUse));

            var translation = ElasticQueryTranslator.Translate(CouchMapping, "p", query.Expression);

            var typeCriteria = Assert.IsType <ExistsCriteria>(translation.SearchRequest.Filter);

            Assert.Equal("doc.id", typeCriteria.Field);
        }
        public void LongCountTranslatesToFacetWhenGroupBy()
        {
            var first = Robots.GroupBy(g => 1).Select(a => a.LongCount());

            var request = ElasticQueryTranslator.Translate(CouchMapping, "prefix", first.Expression).SearchRequest;

            Assert.Equal("count", request.SearchType);
            Assert.IsType <ExistsCriteria>(request.Filter);

            var facet = Assert.Single(request.Facets);

            Assert.IsType <FilterFacet>(facet);
        }
        public void SelectLongCountCreatesFilterFacetWithMatchAll()
        {
            var query = Robots.GroupBy(r => 1).Select(g => g.LongCount());

            var translation = ElasticQueryTranslator.Translate(Mapping, query.Expression);

            Assert.Equal(typeof(long), Assert.IsType <ListTermlessFacetsElasticMaterializer>(translation.Materializer).ElementType);
            Assert.Equal("count", translation.SearchRequest.SearchType);
            Assert.Equal(1, translation.SearchRequest.Facets.Count);

            var facet = Assert.IsType <FilterFacet>(translation.SearchRequest.Facets[0]);

            Assert.IsType <MatchAllCriteria>(facet.Filter);
        }
Esempio n. 9
0
        public void SelectSumCreatesTermsStatsFacet()
        {
            var query = Robots.GroupBy(r => r.Zone).Select(g => g.Sum(r => r.Cost));

            var translation = ElasticQueryTranslator.Translate(Mapping, "", query.Expression);

            Assert.Equal(typeof(decimal), Assert.IsType <ListTermFacetsElasticMaterializer>(translation.Materializer).ElementType);
            Assert.Equal("count", translation.SearchRequest.SearchType);
            Assert.Equal(1, translation.SearchRequest.Facets.Count);
            var facet = Assert.IsType <TermsStatsFacet>(translation.SearchRequest.Facets[0]);

            Assert.Null(facet.Filter);
            Assert.Equal("zone", facet.Key);
            Assert.Equal("cost", facet.Value);
        }
        public void SelectLongCountCreatesTermsFacet()
        {
            var query = Robots.GroupBy(r => r.Zone).Select(g => g.LongCount());

            var translation = ElasticQueryTranslator.Translate(Mapping, query.Expression);

            Assert.Equal(typeof(long), Assert.IsType <ListTermFacetsElasticMaterializer>(translation.Materializer).ElementType);
            Assert.Equal("count", translation.SearchRequest.SearchType);
            Assert.Equal(1, translation.SearchRequest.Facets.Count);

            var facet = Assert.IsType <TermsFacet>(translation.SearchRequest.Facets[0]);

            Assert.Equal(1, facet.Fields.Count);
            Assert.Equal("zone", facet.Fields[0]);
            Assert.Null(facet.Filter);
        }
Esempio n. 11
0
        public void SelectAnonymousKeySumCreatesTermsStatsFacet()
        {
            var query = Robots.GroupBy(r => r.Zone).Select(g => new { g.Key, Total = g.Sum(r => r.Cost) });

            var translation = ElasticQueryTranslator.Translate(Mapping, "", query.Expression);

            var materializer = Assert.IsType <ListTermFacetsElasticMaterializer>(translation.Materializer);

            Assert.Contains("AnonymousType", materializer.ElementType.Name);
            Assert.Equal("count", translation.SearchRequest.SearchType);
            Assert.Equal(1, translation.SearchRequest.Facets.Count);
            var facet = Assert.IsType <TermsStatsFacet>(translation.SearchRequest.Facets[0]);

            Assert.Null(facet.Filter);
            Assert.Equal("zone", facet.Key);
            Assert.Equal("cost", facet.Value);
        }
Esempio n. 12
0
        public void SelectAverageWithTakeCreatesTermsStatsFacetWithSize()
        {
            var query = Robots.GroupBy(r => r.Zone).Select(g => g.Average(r => r.EnergyUse)).Take(5);

            var translation = ElasticQueryTranslator.Translate(CouchMapping, "p", query.Expression);

            Assert.Equal(typeof(double), Assert.IsType <ListTermFacetsElasticMaterializer>(translation.Materializer).ElementType);
            Assert.Equal("count", translation.SearchRequest.SearchType);
            Assert.Equal(1, translation.SearchRequest.Facets.Count);

            var facet = Assert.IsType <TermsStatsFacet>(translation.SearchRequest.Facets[0]);

            Assert.IsType <ExistsCriteria>(translation.SearchRequest.Filter);
            Assert.Equal("p.zone", facet.Key);
            Assert.Equal("p.energyUse", facet.Value);
            Assert.Equal(5, facet.Size);
        }
        public void SelectCountPredicateCreatesFilterFacet()
        {
            var query = Robots.GroupBy(r => 1).Select(g => g.Count(r => r.EnergyUse > 5.0));

            var translation = ElasticQueryTranslator.Translate(Mapping, query.Expression);

            Assert.Equal(typeof(int), Assert.IsType <ListTermlessFacetsElasticMaterializer>(translation.Materializer).ElementType);
            Assert.Equal("count", translation.SearchRequest.SearchType);
            Assert.Equal(1, translation.SearchRequest.Facets.Count);

            var facet         = Assert.IsType <FilterFacet>(translation.SearchRequest.Facets[0]);
            var rangeCriteria = Assert.IsType <RangeCriteria>(facet.Filter);

            Assert.Equal("energyUse", rangeCriteria.Field);
            Assert.Equal(1, rangeCriteria.Specifications.Count);
            Assert.Equal(RangeComparison.GreaterThan, rangeCriteria.Specifications[0].Comparison);
            Assert.Equal(5.0, rangeCriteria.Specifications[0].Value);
        }
        public void SelectProjectionCombinesSameFieldToSingleFacet()
        {
            var query = Robots.GroupBy(r => "a")
                        .Select(g => new
            {
                AverageEnergy = g.Average(a => a.EnergyUse),
                MaxEnergy     = g.Max(a => a.EnergyUse)
            });

            var translation = ElasticQueryTranslator.Translate(Mapping, query.Expression);

            Assert.Contains("AnonymousType", Assert.IsType <ListTermlessFacetsElasticMaterializer>(translation.Materializer).ElementType.FullName);
            Assert.Equal(1, translation.SearchRequest.Facets.Count);
            var facet = Assert.IsType <StatisticalFacet>(translation.SearchRequest.Facets[0]);

            Assert.Null(facet.Filter);
            Assert.Equal(1, facet.Fields.Count);
            Assert.Equal("energyUse", facet.Fields[0]);
        }
        public void SelectProjectionCanUseTupleCreate()
        {
            var query = Robots.GroupBy(g => 1).Select(g => Tuple.Create(g.Key, g.Count()));

            TestProjectionWithKeyAndCount(query.Expression);
        }
Esempio n. 16
0
        public static void GroupByCannotBeExpression()
        {
            var ex = Assert.Throws <NotSupportedException>(() => Translate(Robots.GroupBy(r => r.Id / 2)));

            Assert.Contains("GroupBy must be either a", ex.Message);
        }
        public void SelectProjectionCanUseAnonymousConstructor()
        {
            var query = Robots.GroupBy(g => 1).Select(g => new { g.Key, Count = g.Count() });

            TestProjectionWithKeyAndCount(query.Expression);
        }