Esempio n. 1
0
        private void updateAlias(string alias, string index, bool close = true)
        {
            Console.WriteLine($"Updating `{alias}` alias to `{index}`...");

            var aliasDescriptor = new BulkAliasDescriptor();
            var oldIndices      = elasticClient.GetIndicesPointingToAlias(alias);

            foreach (var oldIndex in oldIndices)
            {
                aliasDescriptor.Remove(d => d.Alias(alias).Index(oldIndex));
            }

            aliasDescriptor.Add(d => d.Alias(alias).Index(index));

            Console.WriteLine(elasticClient.Alias(aliasDescriptor));

            // cleanup
            if (!close)
            {
                return;
            }
            foreach (var toClose in oldIndices.Where(x => x != index))
            {
                Console.WriteLine($"Closing {toClose}");
                elasticClient.CloseIndex(toClose);
            }
        }
Esempio n. 2
0
        private static void CloseIndex(string suffix)
        {
            string indexName = INDEX_PREFIX + suffix;

            var host = "localhost";

            if (Process.GetProcessesByName("fiddler").Any())
            {
                host = "ipv4.fiddler";
            }

            var connSettings = new ConnectionSettings(new Uri("http://" + host + ":9200"))
                               .SetDefaultIndex(indexName);

            var client = new ElasticClient(connSettings);

            var result = client.RootNodeInfo();

            if (!result.IsValid)
            {
                Console.Error.WriteLine("Could not connect to {0}:\r\n{1}",
                                        connSettings.Host, result.ConnectionStatus.Error.OriginalException.Message);
                Console.Read();
                return;
            }

            client.CloseIndex(indexName);
        }
Esempio n. 3
0
        private static List <Func <object> > IndexCommands(ElasticClient elastic)
        {
            return(new List <Func <object> >
            {
                () => elastic.CreateIndex("test_index_1"),
                () => elastic.IndexExists("test_index_1"),
                () => elastic.UpdateIndexSettings(new UpdateIndexSettingsRequest("test_index_1")
                {
                    IndexSettings = new IndexSettings()
                    {
                        Sorting = new SortingSettings
                        {
                            Fields = new Field("Title"),
                        },
                    },
                }),
                () => elastic.Alias(new BulkAliasRequest
                {
                    Actions = new List <IAliasAction>
                    {
                        new AliasAddAction
                        {
                            Add = new AliasAddOperation
                            {
                                Index = "test_index_1",
                                Alias = "test_index_2",
                            },
                        },
                    },
                }),
                () => elastic.GetAliasesPointingToIndex("test_index_1"),
                () => elastic.PutAlias("test_index_1", "test_index_3"),
                () => elastic.AliasExists(new AliasExistsRequest("test_index_1")),
                () => elastic.DeleteAlias(new DeleteAliasRequest("test_index_1", "test_index_3")),
                () => elastic.DeleteAlias(new DeleteAliasRequest("test_index_1", "test_index_2")),
                () => elastic.CreateIndex("test_index_4"),
#if (ELASTICSEARCH_6_1 && !DEFAULT_SAMPLES)
                () => elastic.SplitIndex("test_index_1", "test_index_4"),
#endif
                () => elastic.DeleteIndex("test_index_4"),
                () => elastic.CloseIndex("test_index_1"),
                () => elastic.OpenIndex("test_index_1"),
                () => elastic.PutIndexTemplate(new PutIndexTemplateRequest("test_template_1")),
                () => elastic.IndexTemplateExists("test_template_1"),
                () => elastic.DeleteIndexTemplate("test_template_1"),
                () => elastic.IndicesShardStores(),
                () => elastic.IndicesStats("test_index_1"),
                () => elastic.DeleteIndex("test_index_1"),
                () => elastic.GetAlias(new GetAliasRequest()),
            });
        }
Esempio n. 4
0
 private static List <Func <object> > IndexCommands(ElasticClient elastic)
 {
     return(new List <Func <object> >
     {
         () => elastic.CreateIndex("test_index_1"),
         () => elastic.IndexExists("test_index_1"),
         () => elastic.UpdateIndexSettings(new UpdateIndexSettingsRequest("test_index_1")
         {
             IndexSettings = new IndexSettings()
             {
                 // V6 feature
                 // Sorting = new SortingSettings
                 // {
                 //     Fields = new Field("Title"),
                 // },
             },
         }),
         () => elastic.Alias(new BulkAliasRequest
         {
             Actions = new List <IAliasAction>
             {
                 new AliasAddAction
                 {
                     Add = new AliasAddOperation
                     {
                         Index = "test_index_1",
                         Alias = "test_index_2",
                     },
                 },
             },
         }),
         () => elastic.GetAliasesPointingToIndex("test_index_1"),
         () => elastic.PutAlias("test_index_1", "test_index_3"),
         // () => elastic.AliasExists(new AliasExistsRequest("test_index_1")), // TODO: enable
         () => elastic.DeleteAlias(new DeleteAliasRequest("test_index_1", "test_index_3")),
         () => elastic.DeleteAlias(new DeleteAliasRequest("test_index_1", "test_index_2")),
         () => elastic.CreateIndex("test_index_4"),
         // () => elastic.SplitIndex("test_index_1", "test_index_4"), // V6 Feature
         () => elastic.DeleteIndex("test_index_4"),
         () => elastic.CloseIndex("test_index_1"),
         () => elastic.OpenIndex("test_index_1"),
         () => elastic.PutIndexTemplate(new PutIndexTemplateRequest("test_template_1")),
         () => elastic.IndexTemplateExists("test_template_1"),
         () => elastic.DeleteIndexTemplate("test_template_1"),
         () => elastic.IndicesShardStores(),
         () => elastic.IndicesStats("test_index_1"),
         () => elastic.DeleteIndex("test_index_1"),
         () => elastic.GetAlias(new GetAliasRequest()),
     });
 }
Esempio n. 5
0
        private static void CloseIndex(string suffix)
        {
            string indexName = INDEX_PREFIX + suffix;

            var connSettings = new ConnectionSettings("localhost", 9200)
                               .SetDefaultIndex(indexName);

            var client = new ElasticClient(connSettings);

            ConnectionStatus connStatus;

            if (!client.TryConnect(out connStatus))
            {
                Console.Error.WriteLine("Could not connect to {0}:\r\n{1}",
                                        connSettings.Host, connStatus.Error.OriginalException.Message);
                Console.Read();
                return;
            }

            client.CloseIndex(indexName);
        }
        public void ConnectionPool_ThrowOnServerExceptions_ThrowsElasticsearchServerException()
        {
            var uris = new []
            {
                ElasticsearchConfiguration.CreateBaseUri(9200),
                ElasticsearchConfiguration.CreateBaseUri(9200),
                ElasticsearchConfiguration.CreateBaseUri(9200),
            };
            var connectionPool = new StaticConnectionPool(uris);
            var client         = new ElasticClient(new ConnectionSettings(connectionPool)
                                                   .ThrowOnElasticsearchServerExceptions()
                                                   .SetTimeout(1000)
                                                   );
            var e = Assert.Throws <ElasticsearchServerException>(() =>
            {
                var index  = ElasticsearchConfiguration.NewUniqueIndexName();
                var create = client.CreateIndex(index);
                var close  = client.CloseIndex(index);
                var result = client.Search <ElasticsearchProject>(s => s.Index(index));
            });

            e.Should().NotBeNull();
        }
Esempio n. 7
0
 private static List <Func <object> > IndexCommands(ElasticClient elastic)
 {
     return(new List <Func <object> >
     {
         () => elastic.CreateIndex("test_index_1"),
         () => elastic.IndexExists("test_index_1"),
         () => elastic.UpdateIndexSettings(new UpdateIndexSettingsRequest("test_index_1")
         {
             IndexSettings = new IndexSettings()
             {
                 // V6 feature
                 // Sorting = new SortingSettings
                 // {
                 //     Fields = new Field("Title"),
                 // },
             },
         }),
         () => elastic.Alias(new BulkAliasRequest
         {
             Actions = new List <IAliasAction>
             {
                 new AliasAddAction
                 {
                     Add = new AliasAddOperation
                     {
                         Index = "test_index_1",
                         Alias = "test_index_2",
                     },
                 },
             },
         }),
         () => elastic.GetAliasesPointingToIndex("test_index_1"),
         () => elastic.PutAlias("test_index_1", "test_index_3"),
         // () => elastic.AliasExists(new AliasExistsRequest("test_index_1")), // TODO: enable
         () => elastic.DeleteAlias(new DeleteAliasRequest("test_index_1", "test_index_3")),
         () => elastic.DeleteAlias(new DeleteAliasRequest("test_index_1", "test_index_2")),
         () => elastic.CreateIndex("test_index_4"),
         // () => elastic.SplitIndex("test_index_1", "test_index_4"), // V6 Feature
         () => elastic.DeleteIndex("test_index_4"),
         () => elastic.CloseIndex("test_index_1"),
         () => elastic.OpenIndex("test_index_1"),
         () => elastic.PutIndexTemplate(new PutIndexTemplateRequest("test_template_1")),
         () => elastic.IndexTemplateExists("test_template_1"),
         () => elastic.DeleteIndexTemplate("test_template_1"),
         () => elastic.IndicesShardStores(),
         () => elastic.IndicesStats("test_index_1"),
         () => elastic.DeleteIndex("test_index_1"),
         () => elastic.GetAlias(new GetAliasRequest()),
         () => elastic.ReindexOnServer(new ReindexOnServerRequest
         {
             Source = new ReindexSource
             {
                 Remote = new RemoteSource
                 {
                     Host = new Uri("http://" + Host()),
                     Username = "******",
                     Password = "******"
                 },
                 Index = "some_index",
                 Size = 10
             },
             Destination = new ReindexDestination
             {
                 Index = "some_index_clone",
             }
         })
     });
 }
		public void ConnectionPool_ThrowOnServerExceptions_ThrowsElasticsearchServerException()
		{
			var uris = new []
			{
				ElasticsearchConfiguration.CreateBaseUri(9200),
				ElasticsearchConfiguration.CreateBaseUri(9200),
				ElasticsearchConfiguration.CreateBaseUri(9200),
			};
			var connectionPool = new StaticConnectionPool(uris);
			var client = new ElasticClient(new ConnectionSettings(connectionPool)
				.ThrowOnElasticsearchServerExceptions()
				.SetTimeout(1000)
			);
			var e = Assert.Throws<ElasticsearchServerException>(() =>
			{
				var index = ElasticsearchConfiguration.NewUniqueIndexName();
				var create = client.CreateIndex(index);
				var close = client.CloseIndex(index);
				var result = client.Search<ElasticsearchProject>(s => s.Index(index));
			});
			e.Should().NotBeNull();
		}
Esempio n. 9
0
        public void CustomAnalyzer()
        {
            _client.CloseIndex("people");
            _client.UpdateIndexSettings(
                "people", i =>
                i.IndexSettings(
                    s =>
                    s.Analysis(
                        a =>
                        a.CharFilters(
                            cf =>
                            cf.Mapping(
                                "my_char_filter", m =>
                                m.Mappings("F# => FSharp")
                                )
                            ).TokenFilters(
                            tf =>
                            tf.Synonym(
                                "my_synonym", sf =>
                                sf.Synonyms("superior", "great")
                                )
                            ).Analyzers(
                            an =>
                            an.Custom(
                                "my_analyzer", ca =>
                                ca.Tokenizer("standard")
                                .CharFilters("my_char_filter")
                                .Filters("lowercase", "stop", "my_synonym")
                                )
                            )
                        )
                    )
                );
            _client.OpenIndex("people");
            _client.ClusterHealth(
                h =>
                h.WaitForStatus(WaitForStatus.Green)
                .Index("people")
                .Timeout(TimeSpan.FromSeconds(5))
                );

            var analyzeResponse = _client.Analyze(
                a =>
                a.Index("people")
                .Analyzer("my_analyzer")
                .Text("F# is THE SUPERIOR language :)")
                );

            PrintResponse(analyzeResponse);
            Console.WriteLine("########");

            var analyzeRequest = new AnalyzeRequest("people")
            {
                Analyzer = "standard",
                Text     = new [] { "F# is THE SUPERIOR languaage :) HH" },
                Explain  = true
            };

            analyzeResponse = _client.Analyze(analyzeRequest);
            PrintResponse(analyzeResponse);
        }