示例#1
0
        public void WhereWithCollectionContainsMethod()
        {
            var products = new ProductTest[]
            {
                new ProductTest(Guid.NewGuid(), "Product A", 99),
                new ProductTest(Guid.NewGuid(), "Product B", 150),
                new ProductTest(Guid.NewGuid(), "Product C", 200),
                new ProductTest(Guid.NewGuid(), "Product D", 300)
            };

            var productList = GenerateData(1000, products);

            AddData(productList.ToArray());

            var client = ObterCliente();

            var query = ElasticSearchQueryFactory.CreateQuery <ProductTest>(client);

            var productsIds = products.Select(s => s.ProductId).ToList();

            query = query.Where(w => productsIds.Contains(w.ProductId));

            var result = query.ToList();

            Assert.NotEmpty(result);
            Assert.True(result.All(a => products.Any(p => p.ProductId == a.ProductId)));
        }
示例#2
0
        public void AggregationCount()
        {
            var produto1 = new ProductTest(Guid.NewGuid(), "ProductTest", 9.9M);
            var produto2 = new ProductTest(Guid.NewGuid(), "ProductTest 2", 5M);

            AddData(produto1, produto2);

            var client = ObterCliente();

            var query = ElasticSearchQueryFactory.CreateQuery <ProductTest>(client);
            var count = query.Count();

            Assert.Equal(2, count);
        }
示例#3
0
        public void AggregationSum()
        {
            var produto1 = new ProductTest(Guid.NewGuid(), "ProductTest", 9.9M);
            var produto2 = new ProductTest(Guid.NewGuid(), "ProductTest 2", 5M);

            AddData(produto1, produto2);

            var client = ObterCliente();

            var query      = ElasticSearchQueryFactory.CreateQuery <ProductTest>(client);
            var totalPrice = query.Sum(s => s.Price);

            Assert.Equal(produto1.Price + produto2.Price, totalPrice);
        }
示例#4
0
        public void LessThanInvalid()
        {
            var produto = new ProductTest(Guid.NewGuid(), "ProductTest", 9.9M);

            AddData(produto);

            var client = ObterCliente();

            var query = ElasticSearchQueryFactory.CreateQuery <ProductTest>(client);

            query = query.Where(w => w.Price < 1);

            var result = query.ToList();

            Assert.Empty(result);
        }
示例#5
0
        public void AggregationCountWithCoditions()
        {
            var produto1 = new ProductTest(Guid.NewGuid(), "ProductTest", 25.5M);
            var produto2 = new ProductTest(Guid.NewGuid(), "ProductTest 2", 5M);
            var produto3 = new ProductTest(Guid.NewGuid(), "ProductTest 3", 6M);
            var produto4 = new ProductTest(Guid.NewGuid(), "ProductTest 4", 7M);
            var produto5 = new ProductTest(Guid.NewGuid(), "ProductTest 5", 8M);

            AddData(produto1, produto2, produto3, produto4, produto5);

            var client = ObterCliente();

            var query = ElasticSearchQueryFactory.CreateQuery <ProductTest>(client);
            var count = query.Where(w => w.Price < 10M).Count();

            Assert.Equal(4, count);
        }
示例#6
0
        public void Exists()
        {
            var product = new ProductTest(Guid.NewGuid(), null, 99);

            var productList = GenerateData(1000, product);

            AddData(productList.ToArray());
            var client = ObterCliente();

            var query = ElasticSearchQueryFactory.CreateQuery <ProductTest>(client);

            query = query.Where(w => w.Exists(t => t.NameAsText));

            var result = query.ToList();

            Assert.NotEmpty(result);
            Assert.DoesNotContain(result, f => f.ProductId == product.ProductId);
        }
示例#7
0
        public void MultiMatchDeny()
        {
            var product = new ProductTest(Guid.NewGuid(), "Product of category", 99);

            var productList = GenerateData(1000, product);

            AddData(productList.ToArray());
            var client = ObterCliente();

            var query = ElasticSearchQueryFactory.CreateQuery <ProductTest>(client);

            query = query.Where(w => !w.MultiMatch("category", t => t.NameAsText, t => t.Name));

            var result = query.ToList();

            Assert.NotEmpty(result);
            Assert.DoesNotContain(result, f => f.ProductId == product.ProductId);
        }
示例#8
0
        public void LessThanDeny()
        {
            var produto = new ProductTest(Guid.NewGuid(), "ProductTest", 9.9M);

            AddData(produto);

            var client = ObterCliente();

            var pId = produto.ProductId;

            var query = ElasticSearchQueryFactory.CreateQuery <ProductTest>(client);

            query = query.Where(w => !(w.Price < 50));

            var result = query.ToList();

            Assert.Empty(result);
            Assert.DoesNotContain(result, f => f.ProductId == pId);
        }
示例#9
0
        public void GreaterThanValid()
        {
            var produto = new ProductTest(Guid.NewGuid(), "ProductTest", 9.9M);

            AddData(produto);

            var client = ObterCliente();

            var pId = produto.ProductId;

            var query = ElasticSearchQueryFactory.CreateQuery <ProductTest>(client);

            query = query.Where(w => w.Price > 1);

            var result = query.ToList();

            Assert.NotEmpty(result);
            Assert.Contains(result, f => f.ProductId == pId);
        }
示例#10
0
        public void EqualsInvalid()
        {
            var produto = new ProductTest(Guid.NewGuid(), "ProductTest", 9.9M);

            AddData(produto);

            var client = ObterCliente();

            var pId   = produto.ProductId;
            var pNome = produto.Name;

            var query = ElasticSearchQueryFactory.CreateQuery <ProductTest>(client);

            query = query.Where(w => w.ProductId == Guid.Empty);

            var result = query.ToList();

            Assert.Empty(result);
        }
示例#11
0
        public void OrderByDescendingAndWhere()
        {
            var product = new ProductTest(Guid.NewGuid(), "Product A", 150);

            var productList = GenerateData(1000, product);

            AddData(productList.ToArray());
            var client = ObterCliente();


            var query = ElasticSearchQueryFactory.CreateQuery <ProductTest>(client);

            query = query.Where(w => w.Price == 150).OrderByDescending(o => o.Name).ThenByDescending(o => o.Price);

            var result = query.ToList();

            Assert.Contains(result, f => f.ProductId == product.ProductId);
            Assert.Equal(product.Price, result.First().Price);
        }
示例#12
0
        public void MatchPhrase()
        {
            var product = new ProductTest(Guid.NewGuid(), "Product of category", 99);

            var productList = GenerateData(1000, product);

            AddData(productList.ToArray());
            var client = ObterCliente();

            var query = ElasticSearchQueryFactory.CreateQuery <ProductTest>(client);

            query = query.Where(w => w.NameAsText.MatchPhrase("of category"));

            var result = query.ToList();

            Assert.NotEmpty(result);
            Assert.Contains(result, f => f.ProductId == product.ProductId);
            Assert.Equal(99, result.First().Price);
        }
示例#13
0
        public void StartsWithInvalid()
        {
            var produto = new ProductTest(Guid.NewGuid(), "ProductTest", 9.9M);

            var productList = GenerateData(1000, produto);

            AddData(productList.ToArray());

            var client = ObterCliente();

            var pId   = produto.ProductId;
            var pNome = produto.Name;

            var query = ElasticSearchQueryFactory.CreateQuery <ProductTest>(client);

            query = query.Where(w => w.Name.StartsWith("zzzzzzzzzzz"));

            var result = query.ToList();

            Assert.Empty(result);
        }
示例#14
0
        public void LinqSum()
        {
            var produto1 = new ProductTest(Guid.NewGuid(), "ProductTest", 9.9M);
            var produto2 = new ProductTest(Guid.NewGuid(), "ProductTest 2", 5M);

            AddData(produto1, produto2);

            var client = ObterCliente();

            var query  = ElasticSearchQueryFactory.CreateQuery <ProductTest>(client);
            var result = (from o in query
                          group o by o.Name into g
                          select new
            {
                Produto = g.Key,
                Total = g.Sum(o => o.Price),
                Min = g.Min(o => o.Price),
                Avg = g.Average(o => o.Price)
            }).ToList();

            Assert.Equal(produto1.Price + produto2.Price, result.Sum(s => s.Total));
        }
示例#15
0
        public void StartsWithDeny()
        {
            var produto = new ProductTest(Guid.NewGuid(), "TestProduct", 9.9M);

            var productList = GenerateData(1000, produto);

            AddData(productList.ToArray());

            var client = ObterCliente();

            var pId   = produto.ProductId;
            var pNome = produto.Name;

            var query = ElasticSearchQueryFactory.CreateQuery <ProductTest>(client);

            query = query.Where(w => !w.Name.StartsWith("Test"));

            var result = query.ToList();

            Assert.NotEmpty(result);
            Assert.DoesNotContain(result, f => f.ProductId == pId);
        }