コード例 #1
0
        public void ReindexTest()
        {
            var indexAliasDtoTestV1 = new IndexAliasDtoTestThree { Id = 1, Description = "V1" };
            var indexAliasDtoTestV2 = new IndexAliasDtoTestThree { Id = 2, Description = "V2" };

            IElasticsearchMappingResolver elasticsearchMappingResolverDirectIndex = new ElasticsearchMappingResolver();
            IElasticsearchMappingResolver elasticsearchMappingResolverDirectIndexV1 = new ElasticsearchMappingResolver();
            var mappingV1 = new IndexAliasDtoTestThreeMappingV1();

            IElasticsearchMappingResolver elasticsearchMappingResolverDirectIndexV2 = new ElasticsearchMappingResolver();
            var mappingV2 = new IndexAliasDtoTestThreeMappingV2();

            elasticsearchMappingResolverDirectIndexV1.AddElasticSearchMappingForEntityType(typeof(IndexAliasDtoTestThree), mappingV1);
            elasticsearchMappingResolverDirectIndexV2.AddElasticSearchMappingForEntityType(typeof(IndexAliasDtoTestThree), mappingV2);

            // Step 1 create index V1 and add alias
            using (var context = new ElasticsearchContext(ConnectionString, elasticsearchMappingResolverDirectIndexV1))
            {
                // create the index
                context.AddUpdateDocument(indexAliasDtoTestV1, indexAliasDtoTestV1.Id);
                context.SaveChanges();

                var resultCreate = context.AliasCreateForIndex("indexaliasdtotestthrees", "indexaliasdtotestthree_v1");
                Assert.IsTrue(resultCreate);
            }

            // Step 2 create index V2 and replace alias
            using (var context = new ElasticsearchContext(ConnectionString, elasticsearchMappingResolverDirectIndexV2))
            {
                // create the index
                context.AddUpdateDocument(indexAliasDtoTestV2, indexAliasDtoTestV2.Id);
                context.SaveChanges();

                var result = context.AliasReplaceIndex("indexaliasdtotestthrees", "indexaliasdtotestthree_v1", "indexaliasdtotestthree_v2");
                Assert.IsTrue(result);
            }

            Task.Run(() =>
            {
                using (var context = new ElasticsearchContext(ConnectionString, elasticsearchMappingResolverDirectIndex))
                {
                    while (true)
                    {
                        Thread.Sleep(1000);
                        var itemOk = context.SearchById<IndexAliasDtoTestThree>(2);
                        if (itemOk != null)
                        {
                            _resetEvent.Set();
                        }

                    }
                }
            // ReSharper disable once FunctionNeverReturns
            });

            WaitForDataOrFail();

            // delete index v1
            using (var context = new ElasticsearchContext(ConnectionString, elasticsearchMappingResolverDirectIndexV1))
            {
                context.AllowDeleteForIndex = true;
                var thirdDelete = context.DeleteIndexAsync<IndexAliasDtoTestThree>();
                thirdDelete.Wait();
            }

            // delete index v2
            using (var context = new ElasticsearchContext(ConnectionString, elasticsearchMappingResolverDirectIndexV2))
            {
                context.AllowDeleteForIndex = true;
                var thirdDelete = context.DeleteIndexAsync<IndexAliasDtoTestThree>();
                thirdDelete.Wait();
            }
        }
コード例 #2
0
        public void ReindexTest()
        {
            var indexAliasDtoTestV1 = new IndexAliasDtoTestThree {
                Id = 1, Description = "V1"
            };
            var indexAliasDtoTestV2 = new IndexAliasDtoTestThree {
                Id = 2, Description = "V2"
            };

            IElasticsearchMappingResolver elasticsearchMappingResolverDirectIndex   = new ElasticsearchMappingResolver();
            IElasticsearchMappingResolver elasticsearchMappingResolverDirectIndexV1 = new ElasticsearchMappingResolver();
            var mappingV1 = new IndexAliasDtoTestThreeMappingV1();

            IElasticsearchMappingResolver elasticsearchMappingResolverDirectIndexV2 = new ElasticsearchMappingResolver();
            var mappingV2 = new IndexAliasDtoTestThreeMappingV2();

            elasticsearchMappingResolverDirectIndexV1.AddElasticSearchMappingForEntityType(typeof(IndexAliasDtoTestThree), mappingV1);
            elasticsearchMappingResolverDirectIndexV2.AddElasticSearchMappingForEntityType(typeof(IndexAliasDtoTestThree), mappingV2);

            // Step 1 create index V1 and add alias
            using (var context = new ElasticsearchContext(ConnectionString, elasticsearchMappingResolverDirectIndexV1))
            {
                // create the index
                context.AddUpdateDocument(indexAliasDtoTestV1, indexAliasDtoTestV1.Id);
                context.SaveChanges();

                var resultCreate = context.AliasCreateForIndex("indexaliasdtotestthrees", "indexaliasdtotestthree_v1");
                Assert.IsTrue(resultCreate);
            }

            // Step 2 create index V2 and replace alias
            using (var context = new ElasticsearchContext(ConnectionString, elasticsearchMappingResolverDirectIndexV2))
            {
                // create the index
                context.AddUpdateDocument(indexAliasDtoTestV2, indexAliasDtoTestV2.Id);
                context.SaveChanges();

                var result = context.AliasReplaceIndex("indexaliasdtotestthrees", "indexaliasdtotestthree_v1", "indexaliasdtotestthree_v2");
                Assert.IsTrue(result);
            }


            Task.Run(() =>
            {
                using (var context = new ElasticsearchContext(ConnectionString, elasticsearchMappingResolverDirectIndex))
                {
                    while (true)
                    {
                        Thread.Sleep(1000);
                        var itemOk = context.SearchById <IndexAliasDtoTestThree>(2);
                        if (itemOk != null)
                        {
                            _resetEvent.Set();
                        }
                    }
                }
// ReSharper disable once FunctionNeverReturns
            });


            WaitForDataOrFail();

            // delete index v1
            using (var context = new ElasticsearchContext(ConnectionString, elasticsearchMappingResolverDirectIndexV1))
            {
                context.AllowDeleteForIndex = true;
                var thirdDelete = context.DeleteIndexAsync <IndexAliasDtoTestThree>();
                thirdDelete.Wait();
            }

            // delete index v2
            using (var context = new ElasticsearchContext(ConnectionString, elasticsearchMappingResolverDirectIndexV2))
            {
                context.AllowDeleteForIndex = true;
                var thirdDelete = context.DeleteIndexAsync <IndexAliasDtoTestThree>();
                thirdDelete.Wait();
            }
        }