Exemple #1
0
        private string CreateUsersIndex(int size, int nationalitiesCount)
        {
            var indexName = "index_" + Guid.NewGuid();

            Client.CreateIndex(indexName, x => x.Mappings(
                                   m => m.Map <User>(t => t
                                                     .Properties(prop => prop.Text(str => str.Name(s => s.Nationality).Fielddata()))
                                                     .Properties(prop => prop.Text(str => str.Name(s => s.Name).Fielddata()))
                                                     .Properties(prop => prop.Text(str => str.Name(s => s.Email).Fielddata()))
                                                     )));
            var users = new List <User>();

            for (int i = 0; i < size; i++)
            {
                var user = new User
                {
                    Id          = Guid.NewGuid(),
                    Name        = "User" + i,
                    Nationality = "Nationality" + i % nationalitiesCount,
                    Active      = i % 3 == 0,
                    Age         = (i + 1) % 10
                };

                users.Add(user);
            }
            Client.Bulk(x => x.CreateMany(users).Index(indexName));
            Client.Flush(indexName);
            return(indexName);
        }
Exemple #2
0
        public void TopHits_Sorted_SettingSize()
        {
            Client.DeleteIndex(Infer.Index <User>());
            Client.CreateIndex(Infer.Index <User>());
            for (int i = 0; i < 100; i++)
            {
                var user = new User
                {
                    Name        = "User" + i,
                    Nationality = "Nationality" + i % 10,
                    Age         = (i + 1) % 10
                };

                Client.Index(user);
            }
            Client.Flush(Infer.Index <User>());

            var result = Client.Search <User>(search => search.Aggregations(agg => agg
                                                                            // get 40 first users, sort by name. for each user retrieve name and email
                                                                            .SortedTopHits(40, x => x.Name, SortType.Ascending, x => x.Name, y => y.Email)
                                                                            .SortedTopHits(40, x => x.Name, SortType.Descending, x => x.Name, y => y.Email)
                                                                            .SumBy(x => x.Age)
                                                                            .GroupBy(b => b.Nationality))
                                              );

            var userByNationality = result.Aggs.GetGroupBy <User>(x => x.Nationality).ToList();

            Check.That(userByNationality).HasSize(10);
            var firstNotionality = userByNationality.Single(x => x.Key == "nationality0");

            var ascendingHits = firstNotionality.GetSortedTopHits <User>(x => x.Name, SortType.Ascending).ToList();

            Check.That(ascendingHits).HasSize(10);
            Check.That(ascendingHits[0].Name).IsNotNull();

            Check.That(firstNotionality.GetSum <User, int>(x => x.Age)).Equals(10);
            Check.That(ascendingHits[0].Name).Equals("User0");
            Check.That(ascendingHits[1].Name).Equals("User10");
            Check.That(ascendingHits[2].Name).Equals("User20");
            Check.That(ascendingHits[3].Name).Equals("User30");

            var descendingHits = firstNotionality.GetSortedTopHits <User>(x => x.Name, SortType.Descending).ToList();

            Check.That(descendingHits).HasSize(10);
            Check.That(descendingHits[0].Name).IsNotNull();

            Check.That(descendingHits[0].Name).Equals("User90");
            Check.That(descendingHits[1].Name).Equals("User80");
            Check.That(descendingHits[2].Name).Equals("User70");
            Check.That(descendingHits[3].Name).Equals("User60");
        }
Exemple #3
0
        public void TopHits_In_Double_GroupBy()
        {
            Client.DeleteIndex(Infer.Index <User>());
            Client.CreateIndex(Infer.Index <User>());
            for (int i = 0; i < 250; i++)
            {
                var user = new User
                {
                    Name        = "User" + i,
                    Nationality = "Nationality" + i % 2,
                    Active      = i % 3 == 0
                };

                Client.Index(user);
            }
            Client.Flush(Infer.Index <User>());

            var result = Client.Search <User>(search => search.Aggregations(agg => agg
                                                                            .TopHits(40, x => x.Name)
                                                                            .GroupBy(b => b.Active)
                                                                            .GroupBy(b => b.Nationality))
                                              );

            var userByNationality = result.Aggs.GetGroupBy <User>(x => x.Nationality).ToList();

            Check.That(userByNationality).HasSize(2);
            foreach (var nationality in userByNationality)
            {
                var byActive = nationality.GetGroupBy <User>(x => x.Active).ToList();

                var activeUsers  = byActive[0];
                var inactiveUser = byActive[1];


                var hits = activeUsers.GetTopHits <User>().ToList();
                Check.That(hits).HasSize(40);
                Check.That(hits[0].Name).IsNotNull();

                hits = inactiveUser.GetTopHits <User>().ToList();

                Check.That(hits).HasSize(40);
                Check.That(hits[0].Name).IsNotNull();
            }
        }
Exemple #4
0
        public void Terms_Aggregation_Big_Size()
        {
            Client.DeleteIndex(Infer.Index <User>());
            Client.CreateIndex(Infer.Index <User>());
            for (int i = 0; i < 200; i++)
            {
                var user = new User
                {
                    Name        = "User" + i,
                    Nationality = "Nationality" + i % 20
                };


                Client.Index(user);
            }
            Client.Flush(Infer.Index <User>());

            var result = Client.Search <User>(sc => sc.Aggregations(agg => agg.DistinctBy(x => x.Nationality)));

            var nationalities = result.Aggs.GetDistinct <User, string>(x => x.Nationality).ToList();

            Check.That(nationalities).IsNotNull();
            Check.That(nationalities).HasSize(20);
        }
Exemple #5
0
        private string CreateUsersIndex(int size, int nationalitiesCount)
        {
            var indexName = "index_" + Guid.NewGuid();
            Client.CreateIndex(indexName);
            var users = new List<User>();
            for (int i = 0; i < size; i++)
            {
                var user = new User
                {
                    Name = "User" + i,
                    Nationality = "Nationality" + i % nationalitiesCount,
                    Active = i%3 == 0,
                    Age =  (i + 1) % 10
                };

                users.Add(user);
            }
            Client.Bulk(x => x.CreateMany(users).Index(indexName));
            Client.Flush(indexName);
            return indexName;
        }