public async Task SortByNumberAsync()
        {
            await _employeeRepository.AddAsync(new List <Employee> {
                EmployeeGenerator.Generate(age: 19),
                EmployeeGenerator.Generate(age: 9),
                EmployeeGenerator.Generate(age: 119),
                EmployeeGenerator.Generate(age: 20)
            }, o => o.ImmediateConsistency());

            var searchRepository = (ISearchableReadOnlyRepository <Employee>)_employeeRepository;
            var results          = await searchRepository.SearchAsync(null, sort : "age");

            var employees = results.Documents.ToArray();

            Assert.Equal(4, employees.Length);
            Assert.Equal(9, employees[0].Age);
            Assert.Equal(19, employees[1].Age);
            Assert.Equal(20, employees[2].Age);
            Assert.Equal(119, employees[3].Age);

            results = await searchRepository.SearchAsync(null, sort : "-age");

            employees = results.Documents.ToArray();
            Assert.Equal(4, employees.Length);
            Assert.Equal(119, employees[0].Age);
            Assert.Equal(20, employees[1].Age);
            Assert.Equal(19, employees[2].Age);
            Assert.Equal(9, employees[3].Age);
        }
Beispiel #2
0
        public async Task SearchByQueryWithIncludesAnAliases()
        {
            var employees = EmployeeGenerator.GenerateEmployees(age: 10);
            await _employeeRepository.AddAsync(employees);

            await _client.RefreshAsync();

            var result = await _employeeRepository.SearchAsync(null, null, "@include:myquery");

            Assert.Equal(10, result.Total);
        }
Beispiel #3
0
        public async Task AddAsync()
        {
            var employee = await _employeeRepository.AddAsync(EmployeeGenerator.Generate(name: "  BLAKE  "));

            Assert.NotNull(employee?.Id);

            var result = await _employeeRepository.GetByIdAsync(employee.Id);

            Assert.Equal("blake", result.Name);
        }
Beispiel #4
0
        public async Task CanCacheFindResultAsync()
        {
            var employee = await _employeeRepository.AddAsync(EmployeeGenerator.Generate(age: 20), o => o.ImmediateConsistency());

            var employees = await _employeeRepository.GetAllByAgeAsync(20);

            Assert.Equal(1, employees.Documents.Count);

            string json    = JsonConvert.SerializeObject(employees);
            var    results = JsonConvert.DeserializeObject <FindResults <Employee> >(json);

            Assert.NotNull(results);
            Assert.Equal(1, results.Documents.Count);
        }
        public async Task HandleFailureInReindexScriptAsync()
        {
            var version1Index = new VersionedEmployeeIndex(_configuration, 1);
            await version1Index.DeleteAsync();


            var version22Index = new VersionedEmployeeIndex(_configuration, 22)
            {
                DiscardIndexesOnReindex = false
            };
            await version22Index.DeleteAsync();

            using (new DisposableAction(() => version1Index.DeleteAsync().GetAwaiter().GetResult())) {
                await version1Index.ConfigureAsync();

                IEmployeeRepository version1Repository = new EmployeeRepository(_configuration);

                var utcNow   = SystemClock.UtcNow;
                var employee = await version1Repository.AddAsync(EmployeeGenerator.Generate(createdUtc: utcNow), o => o.ImmediateConsistency());

                Assert.NotNull(employee?.Id);

                using (new DisposableAction(() => version22Index.DeleteAsync().GetAwaiter().GetResult())) {
                    await version22Index.ConfigureAsync();

                    await version22Index.ReindexAsync();

                    var aliasResponse = await _client.Indices.GetAliasAsync(version1Index.Name);

                    Assert.True(aliasResponse.IsValid);
                    Assert.Equal(1, aliasResponse.Indices.Count);
                    Assert.Equal(version1Index.VersionedName, aliasResponse.Indices.First().Key);
                }
            }
        }
        public async Task CanCacheFindResult()
        {
            var employee = await _employeeRepository.AddAsync(EmployeeGenerator.Generate(age: 20));

            await _client.RefreshAsync();

            var employees = await _employeeRepository.GetAllByAgeAsync(20);

            Assert.Equal(1, employees.Documents.Count);

            var json    = JsonConvert.SerializeObject(employees);
            var results = JsonConvert.DeserializeObject <FindResults <Employee> >(json);

            Assert.NotNull(results);
            Assert.Equal(1, results.Documents.Count);
        }
        public async Task CanReindexTimeSeriesIndexWithCorrectMappingsAsync()
        {
            var version1Index = new DailyEmployeeIndex(_configuration, 1);
            await version1Index.DeleteAsync();

            var version2Index = new DailyEmployeeIndex(_configuration, 2)
            {
                DiscardIndexesOnReindex = false
            };
            await version2Index.DeleteAsync();

            using (new DisposableAction(() => version1Index.DeleteAsync().GetAwaiter().GetResult())) {
                await version1Index.ConfigureAsync();

                IEmployeeRepository version1Repository = new EmployeeRepository(version1Index);

                var utcNow   = SystemClock.UtcNow;
                var employee = await version1Repository.AddAsync(EmployeeGenerator.Generate(createdUtc: utcNow), o => o.ImmediateConsistency());

                Assert.NotNull(employee?.Id);

                using (new DisposableAction(() => version2Index.DeleteAsync().GetAwaiter().GetResult())) {
                    await version2Index.ConfigureAsync();

                    await version2Index.ReindexAsync();

                    var existsResponse = await _client.Indices.ExistsAsync(version1Index.GetVersionedIndex(utcNow, 1));

                    _logger.LogRequest(existsResponse);
                    Assert.True(existsResponse.ApiCall.Success);
                    Assert.True(existsResponse.Exists);

                    var indexV1         = version1Index.GetVersionedIndex(utcNow, 1);
                    var mappingResponse = await _client.Indices.GetMappingAsync <Employee>(m => m.Index(indexV1));

                    _logger.LogRequest(mappingResponse);
                    Assert.True(mappingResponse.IsValid);
                    var mappingsV1 = mappingResponse.Indices[indexV1];
                    Assert.NotNull(mappingsV1);
                    string version1Mappings = ToJson(mappingsV1);

                    var indexV2 = version2Index.GetVersionedIndex(utcNow, 2);
                    existsResponse = await _client.Indices.ExistsAsync(indexV2);

                    _logger.LogRequest(existsResponse);
                    Assert.True(existsResponse.ApiCall.Success);
                    Assert.True(existsResponse.Exists);

                    mappingResponse = await _client.Indices.GetMappingAsync <Employee>(m => m.Index(indexV2));

                    _logger.LogRequest(mappingResponse);
                    Assert.True(mappingResponse.IsValid);
                    var mappingsV2 = mappingResponse.Indices[indexV2];
                    Assert.NotNull(mappingsV2);
                    string version2Mappings = ToJson(mappingsV2);
                    Assert.Equal(version1Mappings, version2Mappings);
                }
            }
        }
Beispiel #8
0
        public async Task CanReindexVersionedIndexWithCorrectMappingsAsync()
        {
            var version1Index = new VersionedEmployeeIndex(_configuration, 1);
            await version1Index.DeleteAsync();

            var version2Index = new VersionedEmployeeIndex(_configuration, 2)
            {
                DiscardIndexesOnReindex = false
            };
            await version2Index.DeleteAsync();

            using (new DisposableAction(() => version1Index.DeleteAsync().GetAwaiter().GetResult())) {
                await version1Index.ConfigureAsync();

                var version1Repository = new EmployeeRepository(version1Index.Employee);

                var utcNow   = SystemClock.UtcNow;
                var employee = await version1Repository.AddAsync(EmployeeGenerator.Generate(createdUtc: utcNow), o => o.ImmediateConsistency());

                Assert.NotNull(employee?.Id);

                using (new DisposableAction(() => version2Index.DeleteAsync().GetAwaiter().GetResult())) {
                    await version2Index.ConfigureAsync();

                    await version2Index.ReindexAsync();

                    var existsResponse = await _client.IndexExistsAsync(version1Index.VersionedName);

                    _logger.LogTrace(existsResponse.GetRequest());
                    Assert.True(existsResponse.IsValid);
                    Assert.True(existsResponse.Exists);

                    var mappingResponse = await _client.GetMappingAsync <Employee>(m => m.Index(version1Index.VersionedName));

                    _logger.LogTrace(mappingResponse.GetRequest());
                    Assert.True(mappingResponse.IsValid);
                    Assert.NotNull(mappingResponse.Mappings);

                    existsResponse = await _client.IndexExistsAsync(version2Index.VersionedName);

                    _logger.LogTrace(existsResponse.GetRequest());
                    Assert.True(existsResponse.IsValid);
                    Assert.True(existsResponse.Exists);

                    string version1Mappings = ToJson(mappingResponse.Mappings);
                    mappingResponse = await _client.GetMappingAsync <Employee>(m => m.Index(version1Index.VersionedName));

                    _logger.LogTrace(mappingResponse.GetRequest());
                    Assert.True(mappingResponse.IsValid);
                    Assert.NotNull(mappingResponse.Mappings);
                    Assert.Equal(version1Mappings, ToJson(mappingResponse.Mappings).Replace("-v2", "-v1"));
                }
            }
        }
        public async Task GetNestedAliasedNumberAggregationsWithFilterAsync()
        {
            await _employeeRepository.AddAsync(new Employee {
                Name = "Blake",
                Age  = 30,
                Data = new Dictionary <string, object> {
                    { "@user_meta", new { twitter_id = "blaken", twitter_followers = 1000 } }
                }
            }, o => o.ImmediateConsistency());

            const string aggregations = "min:followers max:followers avg:followers sum:followers cardinality:twitter";
            var          result       = await _employeeRepository.GetCountByQueryAsync(q => q.AggregationsExpression(aggregations));

            Assert.Equal(1, result.Total);
            Assert.Equal(5, result.Aggregations.Count);
            Assert.Equal(1000, result.Aggregations.Min("min_followers").Value);
            Assert.Equal(1000, result.Aggregations.Min("max_followers").Value);
            Assert.Equal(1000, result.Aggregations.Average("avg_followers").Value.GetValueOrDefault());
            Assert.Equal(1000, result.Aggregations.Sum("sum_followers").Value);
            Assert.Equal(1, result.Aggregations.Cardinality("cardinality_twitter").Value);
        }
        public async Task CanReindexSameIndexAsync()
        {
            var index = new EmployeeIndex(_configuration);
            await index.DeleteAsync();

            using (new DisposableAction(() => index.DeleteAsync().GetAwaiter().GetResult())) {
                await index.ConfigureAsync();

                Assert.True((await _client.Indices.ExistsAsync(index.Name)).Exists);

                IEmployeeRepository repository = new EmployeeRepository(_configuration);
                var employee = await repository.AddAsync(EmployeeGenerator.Default, o => o.ImmediateConsistency());

                Assert.NotNull(employee?.Id);

                var countResponse = await _client.CountAsync <Employee>();

                _logger.LogRequest(countResponse);
                Assert.True(countResponse.IsValid);
                Assert.Equal(1, countResponse.Count);

                var mappingResponse = await _client.Indices.GetMappingAsync <Employee>();

                _logger.LogRequest(mappingResponse);
                Assert.True(mappingResponse.IsValid);
                Assert.NotNull(mappingResponse.GetMappingFor(index.Name));

                var newIndex = new EmployeeIndexWithYearsEmployed(_configuration);
                await newIndex.ReindexAsync();

                countResponse = await _client.CountAsync <Employee>();

                _logger.LogRequest(countResponse);
                Assert.True(countResponse.IsValid);
                Assert.Equal(1, countResponse.Count);

                string version1Mappings = ToJson(mappingResponse.GetMappingFor <Employee>());
                mappingResponse = await _client.Indices.GetMappingAsync <Employee>();

                _logger.LogRequest(mappingResponse);
                Assert.True(mappingResponse.IsValid);
                Assert.NotNull(mappingResponse.GetMappingFor <Employee>());
                Assert.NotEqual(version1Mappings, ToJson(mappingResponse.GetMappingFor <Employee>()));
            }
        }
        public async Task CanCreateMonthlyAliasesAsync(DateTime utcNow)
        {
            using (TestSystemClock.Install()) {
                SystemClock.Test.SetFixedTime(utcNow);

                var index = new MonthlyEmployeeIndex(_configuration, 1);
                await index.DeleteAsync();

                using (new DisposableAction(() => index.DeleteAsync().GetAwaiter().GetResult())) {
                    await index.ConfigureAsync();

                    var repository = new EmployeeRepository(index.Employee);

                    for (int i = 0; i < 4; i++)
                    {
                        var employee = await repository.AddAsync(EmployeeGenerator.Generate(createdUtc: utcNow.SubtractMonths(i)));

                        Assert.NotNull(employee?.Id);

                        Assert.Equal(1, await index.GetCurrentVersionAsync());
                        var existsResponse = await _client.IndexExistsAsync(index.GetIndex(employee.CreatedUtc));

                        _logger.Trace(() => existsResponse.GetRequest());
                        Assert.True(existsResponse.IsValid);
                        Assert.True(existsResponse.Exists);

                        var aliasesResponse = await _client.GetAliasAsync(a => a.Index(index.GetIndex(employee.CreatedUtc)));

                        _logger.Trace(() => aliasesResponse.GetRequest());
                        Assert.True(aliasesResponse.IsValid);
                        Assert.Equal(1, aliasesResponse.Indices.Count);

                        var aliases = aliasesResponse.Indices.Values.Single().Select(s => s.Name).ToList();
                        aliases.Sort();

                        Assert.Equal(GetExpectedEmployeeMonthlyAliases(index, utcNow, employee.CreatedUtc), String.Join(", ", aliases));
                    }
                }
            }
        }
        public async Task CanCreateDailyAliasesAsync(DateTime utcNow)
        {
            using (TestSystemClock.Install()) {
                TestSystemClock.SetFrozenTime(utcNow);
                var index = new DailyEmployeeIndex(_configuration, 1);
                await index.DeleteAsync();

                using (new DisposableAction(() => index.DeleteAsync().GetAwaiter().GetResult())) {
                    await index.ConfigureAsync();

                    IEmployeeRepository repository = new EmployeeRepository(index);

                    for (int i = 0; i < 35; i += 5)
                    {
                        var employee = await repository.AddAsync(EmployeeGenerator.Generate(createdUtc: utcNow.SubtractDays(i)));

                        Assert.NotNull(employee?.Id);

                        Assert.Equal(1, await index.GetCurrentVersionAsync());
                        var existsResponse = await _client.Indices.ExistsAsync(index.GetIndex(employee.CreatedUtc));

                        _logger.LogRequest(existsResponse);
                        Assert.True(existsResponse.ApiCall.Success);
                        Assert.True(existsResponse.Exists);

                        var aliasesResponse = await _client.Indices.GetAliasAsync(index.GetIndex(employee.CreatedUtc));

                        _logger.LogRequest(aliasesResponse);
                        Assert.True(aliasesResponse.IsValid);
                        Assert.Equal(1, aliasesResponse.Indices.Count);

                        var aliases = aliasesResponse.Indices.Values.Single().Aliases.Select(s => s.Key).ToList();
                        aliases.Sort();

                        Assert.Equal(GetExpectedEmployeeDailyAliases(index, utcNow, employee.CreatedUtc), String.Join(", ", aliases));
                    }
                }
            }
        }
        public async Task CanReindexVersionedIndexWithDataInBothIndexesAsync()
        {
            var version1Index = new VersionedEmployeeIndex(_configuration, 1);
            await version1Index.DeleteAsync();

            var version2Index = new VersionedEmployeeIndex(_configuration, 2);
            await version2Index.DeleteAsync();

            using (new DisposableAction(() => version1Index.DeleteAsync().GetAwaiter().GetResult())) {
                await version1Index.ConfigureAsync();

                Assert.True((await _client.Indices.ExistsAsync(version1Index.VersionedName)).Exists);

                IEmployeeRepository version1Repository = new EmployeeRepository(_configuration);
                var employee = await version1Repository.AddAsync(EmployeeGenerator.Default, o => o.ImmediateConsistency());

                Assert.NotNull(employee?.Id);

                using (new DisposableAction(() => version2Index.DeleteAsync().GetAwaiter().GetResult())) {
                    await version2Index.ConfigureAsync();

                    Assert.True((await _client.Indices.ExistsAsync(version2Index.VersionedName)).Exists);

                    // swap the alias so we write to v1 and v2 and try to reindex.
                    await _client.Indices.BulkAliasAsync(x => x
                                                         .Remove(a => a.Alias(version1Index.Name).Index(version1Index.VersionedName))
                                                         .Add(a => a.Alias(version2Index.Name).Index(version2Index.VersionedName)));

                    IEmployeeRepository version2Repository = new EmployeeRepository(_configuration);
                    await version2Repository.AddAsync(EmployeeGenerator.Generate(), o => o.ImmediateConsistency());

                    var countResponse = await _client.CountAsync <Employee>(d => d.Index(version1Index.VersionedName));

                    _logger.LogRequest(countResponse);
                    Assert.True(countResponse.IsValid);
                    Assert.Equal(1, countResponse.Count);

                    countResponse = await _client.CountAsync <Employee>(d => d.Index(version2Index.VersionedName));

                    _logger.LogRequest(countResponse);
                    Assert.True(countResponse.IsValid);
                    Assert.Equal(1, countResponse.Count);

                    // swap back the alias
                    await _client.Indices.BulkAliasAsync(x => x
                                                         .Remove(a => a.Alias(version2Index.Name).Index(version2Index.VersionedName))
                                                         .Add(a => a.Alias(version1Index.Name).Index(version1Index.VersionedName)));

                    Assert.Equal(1, await version2Index.GetCurrentVersionAsync());

                    // alias should still point to the old version until reindex
                    var aliasResponse = await _client.Indices.GetAliasAsync(version2Index.Name);

                    Assert.True(aliasResponse.IsValid);
                    Assert.Equal(1, aliasResponse.Indices.Count);
                    Assert.Equal(version1Index.VersionedName, aliasResponse.Indices.First().Key);

                    await version2Index.ReindexAsync();

                    aliasResponse = await _client.Indices.GetAliasAsync(version2Index.Name);

                    Assert.True(aliasResponse.IsValid);
                    Assert.Equal(1, aliasResponse.Indices.Count);
                    Assert.Equal(version2Index.VersionedName, aliasResponse.Indices.First().Key);

                    Assert.Equal(2, await version1Index.GetCurrentVersionAsync());
                    Assert.Equal(2, await version2Index.GetCurrentVersionAsync());

                    await _client.Indices.RefreshAsync(Indices.All);

                    countResponse = await _client.CountAsync <Employee>(d => d.Index(version2Index.VersionedName));

                    _logger.LogRequest(countResponse);
                    Assert.True(countResponse.IsValid);
                    Assert.Equal(2, countResponse.Count);

                    Assert.False((await _client.Indices.ExistsAsync(version1Index.VersionedName)).Exists);
                }
            }
        }
Beispiel #14
0
        public async Task CanQueryByDeleted()
        {
            var employee1 = EmployeeGenerator.Default;

            employee1.IsDeleted = true;
            employee1           = await _employeeRepository.AddAsync(employee1);

            Assert.NotNull(employee1?.Id);

            await _employeeRepository.AddAsync(EmployeeGenerator.Generate());

            await _client.RefreshAsync();

            var allEmployees = await _employeeRepository.QueryAsync(new MyAppQuery().IncludeDeleted());

            Assert.Equal(2, allEmployees.Total);

            var onlyDeleted = await _employeeRepository.QueryAsync(new MyAppQuery().IncludeOnlyDeleted());

            Assert.Equal(1, onlyDeleted.Total);
            Assert.Equal(employee1.Id, onlyDeleted.Documents.First().Id);

            var nonDeletedEmployees = await _employeeRepository.QueryAsync(new MyAppQuery().IncludeDeleted(false));

            Assert.Equal(1, nonDeletedEmployees.Total);
            Assert.NotEqual(employee1.Id, nonDeletedEmployees.Documents.First().Id);
        }
        public async Task CanReindexVersionedIndexWithReindexScriptAsync()
        {
            var version1Index = new VersionedEmployeeIndex(_configuration, 1);
            await version1Index.DeleteAsync();

            var version20Index = new VersionedEmployeeIndex(_configuration, 20)
            {
                DiscardIndexesOnReindex = false
            };
            await version20Index.DeleteAsync();

            var version21Index = new VersionedEmployeeIndex(_configuration, 21)
            {
                DiscardIndexesOnReindex = false
            };
            await version21Index.DeleteAsync();

            using (new DisposableAction(() => version1Index.DeleteAsync().GetAwaiter().GetResult())) {
                await version1Index.ConfigureAsync();

                IEmployeeRepository version1Repository = new EmployeeRepository(version1Index);

                var utcNow   = SystemClock.UtcNow;
                var employee = await version1Repository.AddAsync(EmployeeGenerator.Generate(createdUtc: utcNow), o => o.ImmediateConsistency());

                Assert.NotNull(employee?.Id);

                using (new DisposableAction(() => version20Index.DeleteAsync().GetAwaiter().GetResult())) {
                    await version20Index.ConfigureAsync();

                    await version20Index.ReindexAsync();

                    IEmployeeRepository version20Repository = new EmployeeRepository(version20Index);
                    var result = await version20Repository.GetByIdAsync(employee.Id);

                    Assert.Equal("scripted", result.CompanyName);

                    using (new DisposableAction(() => version21Index.DeleteAsync().GetAwaiter().GetResult())) {
                        await version21Index.ConfigureAsync();

                        await version21Index.ReindexAsync();

                        IEmployeeRepository version21Repository = new EmployeeRepository(version21Index);
                        result = await version21Repository.GetByIdAsync(employee.Id);

                        Assert.Equal("typed script", result.CompanyName);
                    }
                }
            }

            using (new DisposableAction(() => version1Index.DeleteAsync().GetAwaiter().GetResult())) {
                await version1Index.ConfigureAsync();

                IEmployeeRepository version1Repository = new EmployeeRepository(version1Index);

                var utcNow   = SystemClock.UtcNow;
                var employee = await version1Repository.AddAsync(EmployeeGenerator.Generate(createdUtc: utcNow), o => o.ImmediateConsistency());

                Assert.NotNull(employee?.Id);

                using (new DisposableAction(() => version21Index.DeleteAsync().GetAwaiter().GetResult())) {
                    await version21Index.ConfigureAsync();

                    await version21Index.ReindexAsync();

                    IEmployeeRepository version21Repository = new EmployeeRepository(version21Index);
                    var result = await version21Repository.GetByIdAsync(employee.Id);

                    Assert.Equal("typed script", result.CompanyName);
                }
            }
        }
        public async Task CanReindexVersionedIndexAsync()
        {
            var version1Index = new VersionedEmployeeIndex(_configuration, 1);
            await version1Index.DeleteAsync();

            var version2Index = new VersionedEmployeeIndex(_configuration, 2);
            await version2Index.DeleteAsync();

            using (new DisposableAction(() => version1Index.DeleteAsync().GetAwaiter().GetResult())) {
                await version1Index.ConfigureAsync();

                Assert.True((await _client.Indices.ExistsAsync(version1Index.VersionedName)).Exists);

                var indexes = _client.GetIndicesPointingToAlias(version1Index.Name);
                Assert.Single(indexes);

                var aliasResponse = await _client.Indices.GetAliasAsync(version1Index.Name);

                _logger.LogRequest(aliasResponse);
                Assert.True(aliasResponse.IsValid);
                Assert.Equal(1, aliasResponse.Indices.Count);
                Assert.Equal(version1Index.VersionedName, aliasResponse.Indices.First().Key);

                IEmployeeRepository version1Repository = new EmployeeRepository(_configuration);
                var employee = await version1Repository.AddAsync(EmployeeGenerator.Default, o => o.ImmediateConsistency());

                Assert.NotNull(employee?.Id);

                var countResponse = await _client.CountAsync <Employee>(d => d.Index(version1Index.Name));

                _logger.LogRequest(countResponse);
                Assert.True(countResponse.IsValid);
                Assert.Equal(1, countResponse.Count);

                Assert.Equal(1, await version1Index.GetCurrentVersionAsync());

                using (new DisposableAction(() => version2Index.DeleteAsync().GetAwaiter().GetResult())) {
                    await version2Index.ConfigureAsync();

                    Assert.True((await _client.Indices.ExistsAsync(version2Index.VersionedName)).Exists);

                    // Make sure we can write to the index still. Should go to the old index until after the reindex is complete.
                    IEmployeeRepository version2Repository = new EmployeeRepository(_configuration);
                    await version2Repository.AddAsync(EmployeeGenerator.Generate(), o => o.ImmediateConsistency());

                    countResponse = await _client.CountAsync <Employee>(d => d.Index(version1Index.VersionedName));

                    _logger.LogRequest(countResponse);
                    Assert.True(countResponse.IsValid);
                    Assert.Equal(2, countResponse.Count);

                    countResponse = await _client.CountAsync <Employee>(d => d.Index(version2Index.VersionedName));

                    _logger.LogRequest(countResponse);
                    Assert.True(countResponse.IsValid);
                    Assert.Equal(0, countResponse.Count);

                    Assert.Equal(1, await version2Index.GetCurrentVersionAsync());

                    // alias should still point to the old version until reindex
                    aliasResponse = await _client.Indices.GetAliasAsync(version2Index.Name);

                    Assert.True(aliasResponse.IsValid);
                    Assert.Equal(1, aliasResponse.Indices.Count);
                    Assert.Equal(version1Index.VersionedName, aliasResponse.Indices.First().Key);

                    await version2Index.ReindexAsync();

                    aliasResponse = await _client.Indices.GetAliasAsync(version2Index.Name);

                    Assert.True(aliasResponse.IsValid);
                    Assert.Equal(1, aliasResponse.Indices.Count);
                    Assert.Equal(version2Index.VersionedName, aliasResponse.Indices.First().Key);

                    Assert.Equal(2, await version1Index.GetCurrentVersionAsync());
                    Assert.Equal(2, await version2Index.GetCurrentVersionAsync());

                    countResponse = await _client.CountAsync <Employee>(d => d.Index(version2Index.VersionedName));

                    _logger.LogRequest(countResponse);
                    Assert.True(countResponse.IsValid);
                    Assert.Equal(2, countResponse.Count);

                    Assert.False((await _client.Indices.ExistsAsync(version1Index.VersionedName)).Exists);

                    employee = await version2Repository.AddAsync(EmployeeGenerator.Default, o => o.ImmediateConsistency());

                    Assert.NotNull(employee?.Id);

                    countResponse = await _client.CountAsync <Employee>(d => d.Index(version2Index.Name));

                    _logger.LogRequest(countResponse);
                    Assert.True(countResponse.IsValid);
                    Assert.Equal(3, countResponse.Count);
                }
            }
        }
Beispiel #17
0
        public async Task MaintainMonthlyIndexesAsync()
        {
            using (TestSystemClock.Install()) {
                SystemClock.Test.SetFixedTime(new DateTime(2016, 8, 31, 0, 0, 0, DateTimeKind.Utc));
                var index = new MonthlyEmployeeIndex(_configuration, 1)
                {
                    MaxIndexAge = SystemClock.UtcNow.EndOfMonth() - SystemClock.UtcNow.SubtractMonths(4).StartOfMonth()
                };
                await index.DeleteAsync();

                var utcNow = SystemClock.UtcNow;
                using (new DisposableAction(() => index.DeleteAsync().GetAwaiter().GetResult())) {
                    await index.ConfigureAsync();

                    var repository = new EmployeeRepository(index.Employee);

                    for (int i = 0; i < 4; i++)
                    {
                        var created  = utcNow.SubtractMonths(i);
                        var employee = await repository.AddAsync(EmployeeGenerator.Generate(createdUtc: created));

                        Assert.NotNull(employee?.Id);

                        Assert.Equal(1, await index.GetCurrentVersionAsync());
                        var existsResponse = await _client.IndexExistsAsync(index.GetIndex(employee.CreatedUtc));

                        _logger.LogTraceRequest(existsResponse);
                        Assert.True(existsResponse.IsValid);
                        Assert.True(existsResponse.Exists);

                        var aliasesResponse = await _client.GetAliasAsync(a => a.Index(index.GetIndex(employee.CreatedUtc)));

                        _logger.LogTraceRequest(aliasesResponse);
                        Assert.True(aliasesResponse.IsValid);
                        Assert.Equal(1, aliasesResponse.Indices.Count);

                        var aliases = aliasesResponse.Indices.Values.Single().Select(s => s.Name).ToList();
                        aliases.Sort();

                        Assert.Equal(GetExpectedEmployeeMonthlyAliases(index, utcNow, employee.CreatedUtc), String.Join(", ", aliases));
                    }

                    await index.MaintainAsync();

                    for (int i = 0; i < 4; i++)
                    {
                        var created  = utcNow.SubtractMonths(i);
                        var employee = await repository.AddAsync(EmployeeGenerator.Generate(createdUtc: created));

                        Assert.NotNull(employee?.Id);

                        Assert.Equal(1, await index.GetCurrentVersionAsync());
                        var existsResponse = await _client.IndexExistsAsync(index.GetIndex(employee.CreatedUtc));

                        _logger.LogTraceRequest(existsResponse);
                        Assert.True(existsResponse.IsValid);
                        Assert.True(existsResponse.Exists);

                        var aliasesResponse = await _client.GetAliasAsync(a => a.Index(index.GetIndex(employee.CreatedUtc)));

                        _logger.LogTraceRequest(aliasesResponse);
                        Assert.True(aliasesResponse.IsValid);
                        Assert.Equal(1, aliasesResponse.Indices.Count);

                        var aliases = aliasesResponse.Indices.Values.Single().Select(s => s.Name).ToList();
                        aliases.Sort();

                        Assert.Equal(GetExpectedEmployeeMonthlyAliases(index, utcNow, employee.CreatedUtc), String.Join(", ", aliases));
                    }
                }
            }
        }
        public async Task CanResumeReindexAsync()
        {
            const int numberOfEmployeesToCreate = 2000;

            var version1Index = new VersionedEmployeeIndex(_configuration, 1);
            await version1Index.DeleteAsync();

            var version2Index = new VersionedEmployeeIndex(_configuration, 2);
            await version2Index.DeleteAsync();

            using (new DisposableAction(() => version1Index.DeleteAsync().GetAwaiter().GetResult())) {
                await version1Index.ConfigureAsync();

                Assert.True((await _client.Indices.ExistsAsync(version1Index.VersionedName)).Exists);

                IEmployeeRepository version1Repository = new EmployeeRepository(_configuration);
                await version1Repository.AddAsync(EmployeeGenerator.GenerateEmployees(numberOfEmployeesToCreate), o => o.ImmediateConsistency());

                var countResponse = await _client.CountAsync <Employee>(d => d.Index(version1Index.Name));

                _logger.LogRequest(countResponse);
                Assert.True(countResponse.IsValid);
                Assert.Equal(numberOfEmployeesToCreate, countResponse.Count);
                Assert.Equal(1, await version1Index.GetCurrentVersionAsync());

                using (new DisposableAction(() => version2Index.DeleteAsync().GetAwaiter().GetResult())) {
                    await version2Index.ConfigureAsync();

                    Assert.True((await _client.Indices.ExistsAsync(version2Index.VersionedName)).Exists);

                    // Throw error before second repass.
                    await Assert.ThrowsAsync <ApplicationException>(async() => await version2Index.ReindexAsync((progress, message) => {
                        _logger.LogInformation("Reindex Progress {0}%: {1}", progress, message);
                        if (progress == 91)
                        {
                            throw new ApplicationException("Random Error");
                        }

                        return(Task.CompletedTask);
                    }));

                    Assert.Equal(1, await version1Index.GetCurrentVersionAsync());

                    // Add a document and ensure it resumes from this document.
                    await version1Repository.AddAsync(EmployeeGenerator.Generate(ObjectId.GenerateNewId(SystemClock.UtcNow.AddMinutes(1)).ToString()), o => o.ImmediateConsistency());

                    await version2Index.ReindexAsync();

                    var aliasResponse = await _client.Indices.GetAliasAsync(version2Index.Name);

                    Assert.True(aliasResponse.IsValid);
                    Assert.Equal(1, aliasResponse.Indices.Count);
                    Assert.Equal(version2Index.VersionedName, aliasResponse.Indices.First().Key);

                    Assert.Equal(2, await version1Index.GetCurrentVersionAsync());
                    Assert.Equal(2, await version2Index.GetCurrentVersionAsync());

                    countResponse = await _client.CountAsync <Employee>(d => d.Index(version2Index.VersionedName));

                    _logger.LogRequest(countResponse);
                    Assert.True(countResponse.IsValid);
                    Assert.Equal(numberOfEmployeesToCreate + 1, countResponse.Count);

                    Assert.False((await _client.Indices.ExistsAsync(version1Index.VersionedName)).Exists);
                }
            }
        }
        public async Task AddAsync()
        {
            var employee = EmployeeGenerator.Default;

            Assert.Equal(0, employee.Version);

            employee = await _employeeRepository.AddAsync(employee);

            Assert.NotNull(employee?.Id);
            Assert.Equal(1, employee.Version);

            var employee2 = await _employeeRepository.GetByIdAsync(employee.Id);

            Assert.Equal(employee, employee2);
        }
        public async Task MonthlyAliasMaxAgeAsync(DateTime utcNow)
        {
            using (TestSystemClock.Install()) {
                TestSystemClock.SetFrozenTime(utcNow);

                var index = new MonthlyEmployeeIndex(_configuration, 1)
                {
                    MaxIndexAge = TimeSpan.FromDays(90)
                };
                await index.DeleteAsync();

                using (new DisposableAction(() => index.DeleteAsync().GetAwaiter().GetResult())) {
                    await index.ConfigureAsync();

                    IEmployeeRepository repository = new EmployeeRepository(index);

                    var employee = await repository.AddAsync(EmployeeGenerator.Generate(createdUtc: utcNow), o => o.ImmediateConsistency());

                    Assert.NotNull(employee?.Id);

                    var existsResponse = await _client.Indices.ExistsAsync(index.GetIndex(employee.CreatedUtc));

                    _logger.LogRequest(existsResponse);
                    Assert.True(existsResponse.ApiCall.Success);
                    Assert.True(existsResponse.Exists);

                    var aliasesResponse = await _client.Indices.GetAliasAsync(index.GetIndex(employee.CreatedUtc));

                    _logger.LogRequest(aliasesResponse);
                    Assert.True(aliasesResponse.IsValid);
                    Assert.Equal(1, aliasesResponse.Indices.Count);
                    var aliases = aliasesResponse.Indices.Values.Single().Aliases.Select(s => s.Key).ToList();
                    aliases.Sort();
                    Assert.Equal(GetExpectedEmployeeMonthlyAliases(index, utcNow, employee.CreatedUtc), String.Join(", ", aliases));

                    employee = await repository.AddAsync(EmployeeGenerator.Generate(createdUtc: utcNow.SubtractDays(2)), o => o.ImmediateConsistency());

                    Assert.NotNull(employee?.Id);

                    existsResponse = await _client.Indices.ExistsAsync(index.GetIndex(employee.CreatedUtc));

                    _logger.LogRequest(existsResponse);
                    Assert.True(existsResponse.ApiCall.Success);
                    Assert.True(existsResponse.Exists);

                    aliasesResponse = await _client.Indices.GetAliasAsync(index.GetIndex(employee.CreatedUtc));

                    _logger.LogRequest(aliasesResponse);
                    Assert.True(aliasesResponse.IsValid);
                    Assert.Equal(1, aliasesResponse.Indices.Count);
                    aliases = aliasesResponse.Indices.Values.Single().Aliases.Select(s => s.Key).ToList();
                    aliases.Sort();
                    Assert.Equal(GetExpectedEmployeeMonthlyAliases(index, utcNow, employee.CreatedUtc), String.Join(", ", aliases));

                    employee = await repository.AddAsync(EmployeeGenerator.Generate(createdUtc: utcNow.SubtractDays(35)), o => o.ImmediateConsistency());

                    Assert.NotNull(employee?.Id);

                    existsResponse = await _client.Indices.ExistsAsync(index.GetIndex(employee.CreatedUtc));

                    _logger.LogRequest(existsResponse);
                    Assert.True(existsResponse.ApiCall.Success);
                    Assert.True(existsResponse.Exists);

                    aliasesResponse = await _client.Indices.GetAliasAsync(index.GetIndex(employee.CreatedUtc));

                    _logger.LogRequest(aliasesResponse);
                    Assert.True(aliasesResponse.IsValid);
                    Assert.Equal(1, aliasesResponse.Indices.Count);
                    aliases = aliasesResponse.Indices.Values.Single().Aliases.Select(s => s.Key).ToList();
                    aliases.Sort();
                    Assert.Equal(GetExpectedEmployeeMonthlyAliases(index, utcNow, employee.CreatedUtc), String.Join(", ", aliases));
                }
            }
        }
        public async Task DailyAliasMaxAgeAsync(DateTime utcNow)
        {
            using (TestSystemClock.Install()) {
                SystemClock.Test.SetFixedTime(utcNow);
                var index = new DailyEmployeeIndex(_configuration, 1)
                {
                    MaxIndexAge = TimeSpan.FromDays(45)
                };

                await index.DeleteAsync();

                using (new DisposableAction(() => index.DeleteAsync().GetAwaiter().GetResult())) {
                    await index.ConfigureAsync();

                    var version1Repository = new EmployeeRepository(index.Employee);

                    var employee = await version1Repository.AddAsync(EmployeeGenerator.Generate(createdUtc: utcNow), o => o.ImmediateConsistency());

                    Assert.NotNull(employee?.Id);

                    var existsResponse = await _client.IndexExistsAsync(index.GetIndex(employee.CreatedUtc));

                    _logger.Trace(() => existsResponse.GetRequest());
                    Assert.True(existsResponse.IsValid);
                    Assert.True(existsResponse.Exists);

                    var aliasesResponse = await _client.GetAliasAsync(a => a.Index(index.GetIndex(employee.CreatedUtc)));

                    _logger.Trace(() => aliasesResponse.GetRequest());
                    Assert.True(aliasesResponse.IsValid);
                    Assert.Equal(1, aliasesResponse.Indices.Count);
                    var aliases = aliasesResponse.Indices.Values.Single().Select(s => s.Name).ToList();
                    aliases.Sort();
                    Assert.Equal(GetExpectedEmployeeDailyAliases(index, utcNow, employee.CreatedUtc), String.Join(", ", aliases));

                    employee = await version1Repository.AddAsync(EmployeeGenerator.Generate(createdUtc: utcNow.SubtractDays(2)), o => o.ImmediateConsistency());

                    Assert.NotNull(employee?.Id);

                    existsResponse = await _client.IndexExistsAsync(index.GetIndex(employee.CreatedUtc));

                    _logger.Trace(() => existsResponse.GetRequest());
                    Assert.True(existsResponse.IsValid);
                    Assert.True(existsResponse.Exists);

                    aliasesResponse = await _client.GetAliasAsync(a => a.Index(index.GetIndex(employee.CreatedUtc)));

                    _logger.Trace(() => aliasesResponse.GetRequest());
                    Assert.True(aliasesResponse.IsValid);
                    Assert.Equal(1, aliasesResponse.Indices.Count);
                    aliases = aliasesResponse.Indices.Values.Single().Select(s => s.Name).ToList();
                    aliases.Sort();
                    Assert.Equal(GetExpectedEmployeeDailyAliases(index, utcNow, employee.CreatedUtc), String.Join(", ", aliases));

                    employee = await version1Repository.AddAsync(EmployeeGenerator.Generate(createdUtc: utcNow.SubtractDays(35)), o => o.ImmediateConsistency());

                    Assert.NotNull(employee?.Id);

                    existsResponse = await _client.IndexExistsAsync(index.GetIndex(employee.CreatedUtc));

                    _logger.Trace(() => existsResponse.GetRequest());
                    Assert.True(existsResponse.IsValid);
                    Assert.True(existsResponse.Exists);

                    aliasesResponse = await _client.GetAliasAsync(a => a.Index(index.GetIndex(employee.CreatedUtc)));

                    _logger.Trace(() => aliasesResponse.GetRequest());
                    Assert.True(aliasesResponse.IsValid);
                    Assert.Equal(1, aliasesResponse.Indices.Count);
                    aliases = aliasesResponse.Indices.Values.Single().Select(s => s.Name).ToList();
                    aliases.Sort();
                    Assert.Equal(GetExpectedEmployeeDailyAliases(index, utcNow, employee.CreatedUtc), String.Join(", ", aliases));
                }
            }
        }
Beispiel #22
0
        public async Task CanReindexVersionedIndexWithUpdatedDocsAsync()
        {
            var version1Index = new VersionedEmployeeIndex(_configuration, 1);
            await version1Index.DeleteAsync();

            var version2Index = new VersionedEmployeeIndex(_configuration, 2);
            await version2Index.DeleteAsync();

            using (new DisposableAction(() => version1Index.DeleteAsync().GetAwaiter().GetResult())) {
                await version1Index.ConfigureAsync();

                Assert.True(_client.IndexExists(version1Index.VersionedName).Exists);

                var repository = new EmployeeRepository(version1Index.Employee);
                var employee   = await repository.AddAsync(EmployeeGenerator.Default, o => o.ImmediateConsistency());

                Assert.NotNull(employee?.Id);

                using (new DisposableAction(() => version2Index.DeleteAsync().GetAwaiter().GetResult())) {
                    await version2Index.ConfigureAsync();

                    Assert.True(_client.IndexExists(version2Index.VersionedName).Exists);
                    Assert.Equal(1, await version2Index.GetCurrentVersionAsync());

                    // alias should still point to the old version until reindex
                    var aliasResponse = await _client.GetAliasAsync(descriptor => descriptor.Name(version2Index.Name));

                    Assert.True(aliasResponse.IsValid);
                    Assert.Equal(1, aliasResponse.Indices.Count);
                    Assert.Equal(version1Index.VersionedName, aliasResponse.Indices.First().Key);

                    var countdown   = new AsyncCountdownEvent(1);
                    var reindexTask = version2Index.ReindexAsync((progress, message) => {
                        _logger.LogInformation($"Reindex Progress {progress}%: {message}");
                        if (progress == 91)
                        {
                            countdown.Signal();
                            SystemClock.Sleep(1000);
                        }

                        return(Task.CompletedTask);
                    });

                    // Wait until the first reindex pass is done.
                    await countdown.WaitAsync();

                    Assert.Equal(1, await version1Index.GetCurrentVersionAsync());
                    await repository.AddAsync(EmployeeGenerator.Generate(createdUtc: SystemClock.UtcNow));

                    employee.Name = "Updated";
                    await repository.SaveAsync(employee);

                    // Resume after everythings been indexed.
                    await reindexTask;
                    aliasResponse = await _client.GetAliasAsync(descriptor => descriptor.Name(version2Index.Name));

                    Assert.True(aliasResponse.IsValid);
                    Assert.Equal(1, aliasResponse.Indices.Count);
                    Assert.Equal(version2Index.VersionedName, aliasResponse.Indices.First().Key);

                    Assert.Equal(2, await version1Index.GetCurrentVersionAsync());
                    Assert.Equal(2, await version2Index.GetCurrentVersionAsync());

                    await _client.RefreshAsync(Indices.All);

                    var countResponse = await _client.CountAsync <Employee>(d => d.Index(version2Index.VersionedName));

                    _logger.LogTrace(countResponse.GetRequest());
                    Assert.True(countResponse.IsValid);
                    Assert.Equal(2, countResponse.Count);

                    var result = await repository.GetByIdAsync(employee.Id);

                    Assert.Equal(ToJson(employee), ToJson(result));
                    Assert.False((await _client.IndexExistsAsync(version1Index.VersionedName)).Exists);
                }
            }
        }
        public async Task CanReindexVersionedIndexWithDeletedDocsAsync()
        {
            var version1Index = new VersionedEmployeeIndex(_configuration, 1);
            await version1Index.DeleteAsync();

            var version2Index = new VersionedEmployeeIndex(_configuration, 2);
            await version2Index.DeleteAsync();

            using (new DisposableAction(() => version1Index.DeleteAsync().GetAwaiter().GetResult())) {
                await version1Index.ConfigureAsync();

                Assert.True((await _client.Indices.ExistsAsync(version1Index.VersionedName)).Exists);

                IEmployeeRepository repository = new EmployeeRepository(_configuration);
                var employee = await repository.AddAsync(EmployeeGenerator.Default, o => o.ImmediateConsistency());

                Assert.NotNull(employee?.Id);

                using (new DisposableAction(() => version2Index.DeleteAsync().GetAwaiter().GetResult())) {
                    await version2Index.ConfigureAsync();

                    Assert.True((await _client.Indices.ExistsAsync(version2Index.VersionedName)).Exists);
                    Assert.Equal(1, await version2Index.GetCurrentVersionAsync());

                    // alias should still point to the old version until reindex
                    var aliasResponse = await _client.Indices.GetAliasAsync(version2Index.Name);

                    _logger.LogRequest(aliasResponse);
                    Assert.True(aliasResponse.IsValid);
                    Assert.Equal(1, aliasResponse.Indices.Count);
                    Assert.Equal(version1Index.VersionedName, aliasResponse.Indices.First().Key);

                    var countdown   = new AsyncCountdownEvent(1);
                    var reindexTask = version2Index.ReindexAsync(async(progress, message) => {
                        _logger.LogInformation($"Reindex Progress {progress}%: {message}");
                        if (progress == 91)
                        {
                            countdown.Signal();
                            await Task.Delay(1000);
                        }
                    });

                    // Wait until the first reindex pass is done.
                    await countdown.WaitAsync();

                    Assert.Equal(1, await version1Index.GetCurrentVersionAsync());
                    await repository.RemoveAllAsync(o => o.ImmediateConsistency());

                    // Resume after everythings been indexed.
                    await reindexTask;
                    aliasResponse = await _client.Indices.GetAliasAsync(version2Index.Name);

                    _logger.LogRequest(aliasResponse);
                    Assert.True(aliasResponse.IsValid, aliasResponse.GetErrorMessage());
                    Assert.Equal(1, aliasResponse.Indices.Count);
                    Assert.Equal(version2Index.VersionedName, aliasResponse.Indices.First().Key);

                    Assert.Equal(2, await version1Index.GetCurrentVersionAsync());
                    Assert.Equal(2, await version2Index.GetCurrentVersionAsync());

                    var countResponse = await _client.CountAsync <Employee>(d => d.Index(version1Index.VersionedName));

                    _logger.LogRequest(countResponse);
                    Assert.True(countResponse.ApiCall.HttpStatusCode == 404, countResponse.GetErrorMessage());
                    Assert.Equal(0, countResponse.Count);

                    countResponse = await _client.CountAsync <Employee>(d => d.Index(version2Index.VersionedName));

                    _logger.LogRequest(countResponse);
                    Assert.True(countResponse.IsValid, countResponse.GetErrorMessage());
                    Assert.Equal(1, countResponse.Count);

                    Assert.Equal(employee, await repository.GetByIdAsync(employee.Id));
                    Assert.False((await _client.Indices.ExistsAsync(version1Index.VersionedName)).Exists);
                }
            }
        }
        public async Task CanReindexTimeSeriesIndexAsync()
        {
            var version1Index = new DailyEmployeeIndex(_configuration, 1);
            await version1Index.DeleteAsync();

            var version2Index = new DailyEmployeeIndex(_configuration, 2);
            await version2Index.DeleteAsync();

            using (new DisposableAction(() => version1Index.DeleteAsync().GetAwaiter().GetResult())) {
                await version1Index.ConfigureAsync();

                IEmployeeRepository version1Repository = new EmployeeRepository(version1Index);

                var utcNow   = SystemClock.UtcNow;
                var employee = await version1Repository.AddAsync(EmployeeGenerator.Generate(createdUtc: utcNow), o => o.ImmediateConsistency());

                Assert.NotNull(employee?.Id);

                Assert.Equal(1, await version1Index.GetCurrentVersionAsync());

                var aliasCountResponse = await _client.CountAsync <Employee>(d => d.Index(version1Index.Name));

                _logger.LogRequest(aliasCountResponse);
                Assert.True(aliasCountResponse.IsValid);
                Assert.Equal(1, aliasCountResponse.Count);

                var indexCountResponse = await _client.CountAsync <Employee>(d => d.Index(version1Index.GetIndex(utcNow)));

                _logger.LogRequest(indexCountResponse);
                Assert.True(indexCountResponse.IsValid);
                Assert.Equal(1, indexCountResponse.Count);

                indexCountResponse = await _client.CountAsync <Employee>(d => d.Index(version1Index.GetVersionedIndex(utcNow, 1)));

                _logger.LogRequest(indexCountResponse);
                Assert.True(indexCountResponse.IsValid);
                Assert.Equal(1, indexCountResponse.Count);

                using (new DisposableAction(() => version2Index.DeleteAsync().GetAwaiter().GetResult())) {
                    await version2Index.ConfigureAsync();

                    Assert.Equal(1, await version2Index.GetCurrentVersionAsync());
                    IEmployeeRepository version2Repository = new EmployeeRepository(version2Index);

                    // Make sure we write to the old index.
                    await version2Repository.AddAsync(EmployeeGenerator.Generate(createdUtc: utcNow), o => o.ImmediateConsistency());

                    aliasCountResponse = await _client.CountAsync <Employee>(d => d.Index(version1Index.Name));

                    _logger.LogRequest(aliasCountResponse);
                    Assert.True(aliasCountResponse.IsValid);
                    Assert.Equal(2, aliasCountResponse.Count);

                    indexCountResponse = await _client.CountAsync <Employee>(d => d.Index(version1Index.GetVersionedIndex(utcNow, 1)));

                    _logger.LogRequest(indexCountResponse);
                    Assert.True(indexCountResponse.IsValid);
                    Assert.Equal(2, indexCountResponse.Count);

                    var existsResponse = await _client.Indices.ExistsAsync(version2Index.GetVersionedIndex(utcNow, 2));

                    _logger.LogRequest(existsResponse);
                    Assert.True(existsResponse.ApiCall.Success);
                    Assert.False(existsResponse.Exists);

                    // alias should still point to the old version until reindex
                    var aliasesResponse = await _client.Indices.GetAliasAsync(version1Index.GetIndex(employee.CreatedUtc));

                    _logger.LogRequest(aliasesResponse);
                    Assert.True(aliasesResponse.IsValid);
                    Assert.Equal(version1Index.GetVersionedIndex(employee.CreatedUtc, 1), aliasesResponse.Indices.Single().Key);

                    var aliases = aliasesResponse.Indices.Values.Single().Aliases.Select(s => s.Key).ToList();
                    aliases.Sort();
                    Assert.Equal(GetExpectedEmployeeDailyAliases(version1Index, utcNow, employee.CreatedUtc), String.Join(", ", aliases));

                    await version2Index.ReindexAsync();

                    Assert.Equal(2, await version1Index.GetCurrentVersionAsync());
                    Assert.Equal(2, await version2Index.GetCurrentVersionAsync());

                    aliasesResponse = await _client.Indices.GetAliasAsync(version1Index.GetIndex(employee.CreatedUtc));

                    _logger.LogRequest(aliasesResponse);
                    Assert.True(aliasesResponse.IsValid);
                    Assert.Equal(version1Index.GetVersionedIndex(employee.CreatedUtc, 2), aliasesResponse.Indices.Single().Key);

                    aliases = aliasesResponse.Indices.Values.Single().Aliases.Select(s => s.Key).ToList();
                    aliases.Sort();
                    Assert.Equal(GetExpectedEmployeeDailyAliases(version1Index, utcNow, employee.CreatedUtc), String.Join(", ", aliases));

                    existsResponse = await _client.Indices.ExistsAsync(version1Index.GetVersionedIndex(utcNow, 1));

                    _logger.LogRequest(existsResponse);
                    Assert.True(existsResponse.ApiCall.Success);
                    Assert.False(existsResponse.Exists);

                    existsResponse = await _client.Indices.ExistsAsync(version2Index.GetVersionedIndex(utcNow, 2));

                    _logger.LogRequest(existsResponse);
                    Assert.True(existsResponse.ApiCall.Success);
                    Assert.True(existsResponse.Exists);
                }
            }
        }
        public async Task CanHandleReindexFailureAsync()
        {
            var version1Index = new VersionedEmployeeIndex(_configuration, 1);
            await version1Index.DeleteAsync();

            var version2Index = new VersionedEmployeeIndex(_configuration, 2);
            await version2Index.DeleteAsync();

            using (new DisposableAction(() => version1Index.DeleteAsync().GetAwaiter().GetResult())) {
                await version1Index.ConfigureAsync();

                Assert.True((await _client.Indices.ExistsAsync(version1Index.VersionedName)).Exists);

                IEmployeeRepository version1Repository = new EmployeeRepository(_configuration);
                await version1Repository.AddAsync(EmployeeGenerator.Generate(), o => o.ImmediateConsistency());

                var countResponse = await _client.CountAsync <Employee>(d => d.Index(version1Index.Name));

                _logger.LogRequest(countResponse);
                Assert.True(countResponse.IsValid);
                Assert.Equal(1, countResponse.Count);
                Assert.Equal(1, await version1Index.GetCurrentVersionAsync());

                using (new DisposableAction(() => version2Index.DeleteAsync().GetAwaiter().GetResult())) {
                    //Create invalid mappings
                    var response = await _client.Indices.CreateAsync(version2Index.VersionedName, d => d.Map <Employee>(map => map
                                                                                                                        .Dynamic(false)
                                                                                                                        .Properties(p => p
                                                                                                                                    .Number(f => f.Name(e => e.Id))
                                                                                                                                    )));

                    _logger.LogRequest(response);

                    Assert.True((await _client.Indices.ExistsAsync(version2Index.VersionedName)).Exists);
                    Assert.Equal(1, await version1Index.GetCurrentVersionAsync());

                    await version2Index.ReindexAsync();

                    await version2Index.Configuration.Client.Indices.RefreshAsync(Indices.All);

                    var aliasResponse = await _client.Indices.GetAliasAsync(version2Index.Name);

                    Assert.True(aliasResponse.IsValid);
                    Assert.Equal(1, aliasResponse.Indices.Count);
                    Assert.True(aliasResponse.Indices.ContainsKey(version1Index.VersionedName));

                    var indexResponse = await _client.Cat.IndicesAsync(d => d.Index(Indices.Index("employees-*")));

                    Assert.NotNull(indexResponse.Records.FirstOrDefault(r => r.Index == version1Index.VersionedName));
                    Assert.NotNull(indexResponse.Records.FirstOrDefault(r => r.Index == version2Index.VersionedName));
                    Assert.NotNull(indexResponse.Records.FirstOrDefault(r => r.Index == $"{version2Index.VersionedName}-error"));

                    Assert.Equal(1, await version1Index.GetCurrentVersionAsync());
                    Assert.Equal(1, await version2Index.GetCurrentVersionAsync());

                    countResponse = await _client.CountAsync <Employee>(d => d.Index(version1Index.VersionedName));

                    _logger.LogRequest(countResponse);
                    Assert.True(countResponse.IsValid);
                    Assert.Equal(1, countResponse.Count);

                    countResponse = await _client.CountAsync <Employee>(d => d.Index(version2Index.VersionedName));

                    _logger.LogRequest(countResponse);
                    Assert.True(countResponse.IsValid);
                    Assert.Equal(0, countResponse.Count);

                    countResponse = await _client.CountAsync <object>(d => d.Index($"{version2Index.VersionedName}-error"));

                    _logger.LogRequest(countResponse);
                    Assert.True(countResponse.IsValid);
                    Assert.Equal(1, countResponse.Count);
                }
            }
        }
        public async Task CanQueryByDeleted()
        {
            var employee1 = EmployeeGenerator.Default;

            employee1.IsDeleted = true;
            employee1           = await _employeeRepository.AddAsync(employee1, o => o.ImmediateConsistency());

            Assert.NotNull(employee1?.Id);

            await _employeeRepository.AddAsync(EmployeeGenerator.Generate(), o => o.ImmediateConsistency());

            var allEmployees = await _employeeRepository.GetByQueryAsync(q => q.SoftDeleteMode(SoftDeleteQueryMode.All));

            Assert.Equal(2, allEmployees.Total);

            var onlyDeleted = await _employeeRepository.GetByQueryAsync(q => q.SoftDeleteMode(SoftDeleteQueryMode.DeletedOnly));

            Assert.Equal(1, onlyDeleted.Total);
            Assert.Equal(employee1.Id, onlyDeleted.Documents.First().Id);

            var nonDeletedEmployees = await _employeeRepository.GetByQueryAsync(q => q.SoftDeleteMode(SoftDeleteQueryMode.ActiveOnly));

            Assert.Equal(1, nonDeletedEmployees.Total);
            Assert.NotEqual(employee1.Id, nonDeletedEmployees.Documents.First().Id);
        }
Beispiel #27
0
        public async Task GetByAgeAsync()
        {
            var employee19 = await _employeeRepository.AddAsync(EmployeeGenerator.Generate(age: 19), o => o.ImmediateConsistency());

            var employee20 = await _employeeRepository.AddAsync(EmployeeGenerator.Generate(age: 20), o => o.ImmediateConsistency());

            var results = await _employeeRepository.GetAllByAgeAsync(employee19.Age);

            Assert.Equal(1, results.Total);
            Assert.Equal(employee19, results.Documents.First());

            results = await _employeeRepository.GetAllByAgeAsync(employee20.Age);

            Assert.Equal(1, results.Total);
            Assert.Equal(employee20, results.Documents.First());
        }
        public async Task AddWithDefaultGeneratedIdAsync()
        {
            await RemoveDataAsync();

            var employee = await _repository.AddAsync(EmployeeGenerator.Default);

            Assert.NotNull(employee?.Id);
            Assert.Equal(EmployeeGenerator.Default.Name, employee.Name);
            Assert.Equal(EmployeeGenerator.Default.Age, employee.Age);
            Assert.Equal(EmployeeGenerator.Default.CompanyName, employee.CompanyName);
            Assert.Equal(EmployeeGenerator.Default.CompanyId, employee.CompanyId);
        }
Beispiel #29
0
        public async Task GetByAgeAsync()
        {
            var employee19 = await _employeeRepository.AddAsync(EmployeeGenerator.Generate(age: 19));

            var employee20 = await _employeeRepository.AddAsync(EmployeeGenerator.Generate(age: 20));

            await _client.RefreshAsync();

            var results = await _employeeRepository.GetAllByAgeAsync(employee19.Age);

            Assert.Equal(1, results.Total);
            Assert.Equal(employee19, results.Documents.First());

            results = await _employeeRepository.GetAllByAgeAsync(employee20.Age);

            Assert.Equal(1, results.Total);
            Assert.Equal(employee20, results.Documents.First());
        }