public void CreateIndexWithAlias()
        {
            IElasticsearchMappingResolver elasticsearchMappingResolver = new ElasticsearchMappingResolver();

            elasticsearchMappingResolver.AddElasticSearchMappingForEntityType(typeof(TestDto), new ElasticsearchMappingTestDto());
            using (var context = new ElasticsearchContext(ConnectionString, new ElasticsearchSerializerConfiguration(elasticsearchMappingResolver, true, true)))
            {
                context.TraceProvider = new ConsoleTraceProvider();

                context.IndexCreate <TestDto>(
                    new IndexDefinition
                {
                    IndexAliases = new IndexAliases
                    {
                        Aliases = new List <IndexAlias>
                        {
                            // alais maps to default index name
                            new IndexAlias("testdtos")
                        }
                    },
                    IndexSettings = new IndexSettings {
                        RefreshInterval = "-1", NumberOfReplicas = 0
                    }
                }
                    );
            }
        }
Exemplo n.º 2
0
        public Address GetAddressFromElasticsearch(string id, string parentId)
        {
            Address address;
            IElasticsearchMappingResolver elasticsearchMappingResolver = new ElasticsearchMappingResolver();

            elasticsearchMappingResolver.AddElasticSearchMappingForEntityType(typeof(Address), new MappingAddress());
            using (var elasticSearchContext = new ElasticsearchContext("http://localhost:9200/", elasticsearchMappingResolver))
            {
                elasticSearchContext.TraceProvider = new ConsoleTraceProvider();
                address = elasticSearchContext.GetDocument <Address>(id, new RoutingDefinition {
                    ParentId = parentId
                });
            }

            return(address);
        }
Exemplo n.º 3
0
        private static MappingTestsParent SetupIndexMappingTests(string index, out IElasticsearchMappingResolver elasticsearchMappingResolver)
        {
            var mappingTestsParent = new MappingTestsParent
            {
                Calls = 3,
                MappingTestsParentId = 2,
                MappingTestsItem     = new MappingTestsChild
                {
                    Description         = "Hello nested",
                    MappingTestsChildId = 5
                }
            };

            elasticsearchMappingResolver = new ElasticsearchMappingResolver();
            elasticsearchMappingResolver.AddElasticSearchMappingForEntityType(typeof(MappingTestsParent),
                                                                              MappingUtils.GetElasticsearchMapping(index));
            return(mappingTestsParent);
        }
Exemplo n.º 4
0
        public static void Main(string[] args)
        {
            IElasticsearchMappingResolver elasticsearchMappingResolver = new ElasticsearchMappingResolver();

            // You only require a mapping if the default settings are not good enough
            elasticsearchMappingResolver.AddElasticSearchMappingForEntityType(typeof(AppRecord), new AppRecordMapping());

            using (var elasticSearchContext = new ElasticsearchContext("http://localhost:9200/", elasticsearchMappingResolver))
            {
                elasticSearchContext.TraceProvider = new TraceProvider("tracingExample");
                elasticSearchContext.AddUpdateDocument(TestData.AppRecord1, TestData.AppRecord1.Hash);
                elasticSearchContext.AddUpdateDocument(TestData.AppRecord2, TestData.AppRecord2.Hash);
                elasticSearchContext.AddUpdateDocument(TestData.AppRecord3, TestData.AppRecord3.Hash);

                var addEntitiesResult = elasticSearchContext.SaveChanges();

                Console.WriteLine(addEntitiesResult.PayloadResult);
                Console.WriteLine(addEntitiesResult.Status);
                Console.WriteLine(addEntitiesResult.Description);
            }
        }
        public void CreateAliasForIndex3()
        {
            var indexAliasDtoTest3 = new IndexAliasDtoTest { Id = 3, Description = "no" };
            var indexAliasDtoTest4 = new IndexAliasDtoTest { Id = 4, Description = "boo" };
            var indexAliasDtoTest5 = new IndexAliasDtoTest { Id = 5, Description = "boo" };

            var aliasParameters = new AliasParameters
            {
                Actions = new List<AliasBaseParameters>
                {
                    new AliasAddParameters("test4", "indexaliasdtotests")
                    {
                        Routing="newroute",
                        Filter= new TermFilter("description", "boo") // "{ \"term\" : { \"description\" : \"boo\" } }"
                    }
                }
            };

            const bool userDefinedRouting = true;
            var elasticsearchSerializerConfiguration = new ElasticsearchSerializerConfiguration(_elasticsearchMappingResolver,
                true, false, userDefinedRouting);

            using (var context = new ElasticsearchContext(ConnectionString, elasticsearchSerializerConfiguration))
            {
                context.AddUpdateDocument(indexAliasDtoTest3, indexAliasDtoTest3.Id, new RoutingDefinition { RoutingId = "newroute" });
                context.AddUpdateDocument(indexAliasDtoTest4, indexAliasDtoTest4.Id, new RoutingDefinition { RoutingId = "newroute" });
                context.AddUpdateDocument(indexAliasDtoTest5, indexAliasDtoTest5.Id, new RoutingDefinition { RoutingId = "newroute" });
                context.SaveChanges();

                var result = context.Alias(aliasParameters.ToString());
                Assert.IsTrue(result);

                Assert.IsTrue(context.AliasExists("test4"));

                // using the index
                var doc3 = context.GetDocument<IndexAliasDtoTest>(3, new RoutingDefinition {RoutingId = "newroute"});
                Assert.IsTrue(doc3.Id == 3);
                var doc4 = context.GetDocument<IndexAliasDtoTest>(4, new RoutingDefinition { RoutingId = "newroute" });
                Assert.IsTrue(doc4.Id == 4);

            }

            IElasticsearchMappingResolver elasticsearchMappingResolver = new ElasticsearchMappingResolver();

            elasticsearchMappingResolver.AddElasticSearchMappingForEntityType(
                typeof(IndexAliasDtoTest),
                MappingUtils.GetElasticsearchMapping<IndexAliasDtoTest>("test4", "indexaliasdtotest")
            );

            using (var context = new ElasticsearchContext(ConnectionString, elasticsearchMappingResolver))
            {
                // using the alias
                var xx = context.GetDocument<IndexAliasDtoTest>(4);
                Assert.IsTrue(xx.Id == 4);

                // should not be found due to filter
                var result = context.SearchById<IndexAliasDtoTest>(3);
                Assert.IsNull(result);
            }
        }
        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();
            }
        }
        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();
            }
        }
        public void CreateAliasForIndex3()
        {
            var indexAliasDtoTest3 = new IndexAliasDtoTest {
                Id = 3, Description = "no"
            };
            var indexAliasDtoTest4 = new IndexAliasDtoTest {
                Id = 4, Description = "boo"
            };
            var indexAliasDtoTest5 = new IndexAliasDtoTest {
                Id = 5, Description = "boo"
            };

            var aliasParameters = new AliasParameters
            {
                Actions = new List <AliasBaseParameters>
                {
                    new AliasAddParameters("test4", "indexaliasdtotests")
                    {
                        Routing = "newroute",
                        Filter  = new TermFilter("description", "boo")                       // "{ \"term\" : { \"description\" : \"boo\" } }"
                    }
                }
            };

            const bool userDefinedRouting = true;
            var        elasticsearchSerializerConfiguration = new ElasticsearchSerializerConfiguration(_elasticsearchMappingResolver,
                                                                                                       true, false, userDefinedRouting);

            using (var context = new ElasticsearchContext(ConnectionString, elasticsearchSerializerConfiguration))
            {
                context.AddUpdateDocument(indexAliasDtoTest3, indexAliasDtoTest3.Id, new RoutingDefinition {
                    RoutingId = "newroute"
                });
                context.AddUpdateDocument(indexAliasDtoTest4, indexAliasDtoTest4.Id, new RoutingDefinition {
                    RoutingId = "newroute"
                });
                context.AddUpdateDocument(indexAliasDtoTest5, indexAliasDtoTest5.Id, new RoutingDefinition {
                    RoutingId = "newroute"
                });
                context.SaveChanges();

                var result = context.Alias(aliasParameters.ToString());
                Assert.IsTrue(result);

                Assert.IsTrue(context.AliasExists("test4"));

                // using the index
                var doc3 = context.GetDocument <IndexAliasDtoTest>(3, new RoutingDefinition {
                    RoutingId = "newroute"
                });
                Assert.IsTrue(doc3.Id == 3);
                var doc4 = context.GetDocument <IndexAliasDtoTest>(4, new RoutingDefinition {
                    RoutingId = "newroute"
                });
                Assert.IsTrue(doc4.Id == 4);
            }

            IElasticsearchMappingResolver elasticsearchMappingResolver = new ElasticsearchMappingResolver();

            elasticsearchMappingResolver.AddElasticSearchMappingForEntityType(
                typeof(IndexAliasDtoTest),
                MappingUtils.GetElasticsearchMapping <IndexAliasDtoTest>("test4", "indexaliasdtotest")
                );

            using (var context = new ElasticsearchContext(ConnectionString, elasticsearchMappingResolver))
            {
                // using the alias
                var xx = context.GetDocument <IndexAliasDtoTest>(4);
                Assert.IsTrue(xx.Id == 4);

                // should not be found due to filter
                var result = context.SearchById <IndexAliasDtoTest>(3);
                Assert.IsNull(result);
            }
        }
Exemplo n.º 9
0
        private static MappingTestsParent SetupIndexMappingTests(string index, out IElasticsearchMappingResolver elasticsearchMappingResolver)
        {
            var mappingTestsParent = new MappingTestsParent
            {
                Calls = 3,
                MappingTestsParentId = 2,
                MappingTestsItem = new MappingTestsChild
                {
                    Description = "Hello nested",
                    MappingTestsChildId = 5
                }
            };

            elasticsearchMappingResolver = new ElasticsearchMappingResolver();
            elasticsearchMappingResolver.AddElasticSearchMappingForEntityType(typeof (MappingTestsParent),
                MappingUtils.GetElasticsearchMapping(index));
            return mappingTestsParent;
        }