コード例 #1
0
 public ElasticDao()
 {
     node = new Uri("http://localhost:9200");
     connectionSettings = new ConnectionSettings(node);
     client             = new ElasticClient(connectionSettings);
     client.ClusterHealth(ch => ch
                          .Local());
 }
コード例 #2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            var host     = Environment.GetEnvironmentVariable("ELASTICSEARCH_HOST") ?? "localhost";
            var uri      = new Uri($"http://{host}:9200");
            var settings = new ConnectionSettings(uri).DefaultIndex("elastic-net-example");
            var elastic  = new ElasticClient(settings);

            elastic.ClusterHealth(new ClusterHealthRequest());
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: onfjp/dd-trace-dotnet
 private static List <Func <object> > ClusterCommands(ElasticClient elastic)
 {
     return(new List <Func <object> >
     {
         () => elastic.ClusterAllocationExplain(new ClusterAllocationExplainRequest()),
         () => elastic.ClusterGetSettings(new ClusterGetSettingsRequest()),
         () => elastic.ClusterHealth(new ClusterHealthRequest()),
         () => elastic.ClusterPendingTasks(new ClusterPendingTasksRequest()),
         () => elastic.ClusterPutSettings(new ClusterPutSettingsRequest()),
         () => elastic.ClusterReroute(new ClusterRerouteRequest()),
         () => elastic.ClusterState(new ClusterStateRequest()),
         () => elastic.ClusterStats(new ClusterStatsRequest()),
     });
 }
コード例 #4
0
        public void UsePrettyResponsesShouldSurviveUrlModififications()
        {
            var settings = new ConnectionSettings(UnitTestDefaults.Uri, UnitTestDefaults.DefaultIndex)
                .UsePrettyResponses();
            var connection = new InMemoryConnection(settings);
            var client = new ElasticClient(settings, connection);

            var r = client.ClusterHealth(h=>h.Level(LevelOptions.Cluster));
            var u = new Uri(r.ConnectionStatus.RequestUrl);
            u.AbsolutePath.Should().StartWith("/_cluster/health");
            u.Query.Should().Contain("level=cluster");

            u.Query.Should().Contain("pretty=true");
        }
コード例 #5
0
        private IHealthResponse GetClusterHealth()
        {
            var request = new ClusterHealthRequest
            {
                Level = Level.Cluster,
                //http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/cluster-health.html
                WaitForStatus = WaitForStatus.Yellow,
                Timeout       = _logstashConfiguration.Timeout.ToString()
            };

            var client = new ElasticClient(new ConnectionSettings(new Uri(_logstashConfiguration.HostName)));

            return(client.ClusterHealth(request));
        }
コード例 #6
0
        public void UsePrettyResponsesShouldSurviveUrlModififications()
        {
            var settings = new ConnectionSettings(UnitTestDefaults.Uri, UnitTestDefaults.DefaultIndex)
                           .UsePrettyResponses();
            var connection = new InMemoryConnection(settings);
            var client     = new ElasticClient(settings, connection);

            var r = client.ClusterHealth(h => h.Level(LevelOptions.Cluster));
            var u = new Uri(r.ConnectionStatus.RequestUrl);

            u.AbsolutePath.Should().StartWith("/_cluster/health");
            u.Query.Should().Contain("level=cluster");

            u.Query.Should().Contain("pretty=true");
        }
コード例 #7
0
        /// <summary>
        /// 创建客户端
        /// </summary>
        /// <returns></returns>
        private IElasticClient GetElasticClient(Uri[] nodes, string indexName)
        {
            var connectionPool = new SniffingConnectionPool(nodes);
            var settings       = new ConnectionSettings(connectionPool).DefaultIndex(indexName).RequestTimeout(TimeSpan.FromSeconds(60)).DisableDirectStreaming();
            var client         = new ElasticClient(settings);
            var res            = client.ClusterHealth();

            Console.WriteLine("ES状态:{0}", res.Status);

            if (!client.IndexExists(indexName).Exists)
            {
                var createIndexResponse = client.CreateIndex(indexName);
                if (!createIndexResponse.IsValid)
                {
                    //发生错误输出错误原因
                    Console.WriteLine(createIndexResponse.ServerError.Error.Reason);
                }
            }
            return(client);
        }
コード例 #8
0
        public static ConnectionTestResults PerformConnectivityTest(PluginConfig config, Logger logger = null)
        {
            ConnectionTestResults results = new ConnectionTestResults();

            try
            {
                ElasticClient client = GetESClient(config);
                results.ClusterHealth        = client.ClusterHealth((s) => s.Level(Level.Cluster));
                results.ConnectionSuccessful = results.ClusterHealth.IsValid;
            }
            catch (Exception e)
            {
                if (logger != null)
                {
                    logger.LogInfo(string.Format("Connection test failed: {0}", e.Message));
                }
                results.ConnectionSuccessful = false;
            }

            return(results);
        }
コード例 #9
0
        public IActionResult Index()
        {
            SearchRequest <Person> sr = new SearchRequest <Person>
            {
                Query = new MatchQuery
                {
                    Field = "firstname",
                    Query = "Hubert"
                }
            };

            var searchResults = client.Search <Person>(sr);

            var searchResults2 = client.Search <Person>(s => s
                                                        .Query(q => q.Match(m => m.Field("lastname")
                                                                            .Query("Dumas")))
                                                        );

            var allPeople = client.Search <Person>();

            var multiResults = client.Search <Person>(s => s
                                                      .Query(q => q.MultiMatch(c => c
                                                                               .Fields(f => f.Field("lastname").Field("firstname"))
                                                                               .Query("Gaye")
                                                                               ))
                                                      );

            var multiResults2 = client.Search <Person>(s => s
                                                       .Query(q => q.MultiMatch(c => c
                                                                                .Fields(f => f.Field(p => p.Firstname).Field(p => p.Lastname))
                                                                                .Query("Dumas")
                                                                                ))
                                                       );


            IClusterHealthResponse res = client.ClusterHealth();

            return(View(res));
        }
コード例 #10
0
        static void Main(string[] args)
        {
            var    local     = new Uri("http://localhost:9200");
            string indexName = "blog_post_author_index";
            var    settings  = new ConnectionSettings(local).DefaultIndex(indexName);
            var    elastic   = new ElasticClient(settings);

            var res = elastic.ClusterHealth();

            Console.WriteLine(res.Status);

            var blogPost = new BlogPost
            {
                Id    = Guid.NewGuid(),
                Title = "First blog post",
                Body  = "This is very long blog post!"
            };

            if (!elastic.IndexExists(indexName).Exists)
            {
                var createIndexResponse = elastic.CreateIndex(indexName);
                var mappingBlogPost     = elastic.Map <BlogPost>(s => s.AutoMap());
                var mappingBlogAuthor   = elastic.Map <Author>(s => s.AutoMap());
                Console.WriteLine("createIndexResponse=" + createIndexResponse.IsValid);
                Console.WriteLine("mappingBlogPost=" + mappingBlogPost.IsValid);
                Console.WriteLine("mappingBlogAuthor=" + mappingBlogAuthor.IsValid);
            }

            IIndexResponse indexResponse = elastic.Index(blogPost, i => i
                                                         .Index(indexName)
                                                         .Type(typeof(BlogPost))
                                                         .Id(1)
                                                         .Refresh());

            Console.WriteLine("IIndexResponse=" + indexResponse.IsValid);


            //insert 10 documents
            for (var i = 2; i < 12; i++)
            {
                var blogPostNew = new BlogPost
                {
                    Id    = Guid.NewGuid(),
                    Title = string.Format("title {0:000}", i),
                    Body  = string.Format("This is {0:000} very long blog post!", i)
                };
                IIndexResponse bulkIndexReponse = elastic.Index(blogPostNew, p => p
                                                                .Type(typeof(BlogPost))
                                                                .Id(i)
                                                                .Refresh());
                Console.WriteLine("bulk IIndexResponse=" + bulkIndexReponse.IsValid);
            }

            //Get document by id
            var result = elastic.Get <BlogPost>(new GetRequest(indexName, typeof(BlogPost), 16));

            Console.WriteLine("Document id:" + result.Id);
            Console.WriteLine("Document fields:" + result.Fields);
            Console.WriteLine("Document Type:" + result.Type);
            Console.WriteLine("Document Found Status:" + result.Found);

            //delete document by id
            //var deleteResult = elastic.Delete(new DeleteRequest(indexName, typeof(BlogPost), 1));
            //Console.WriteLine(deleteResult.Found);

            //Query search queries for match all
            var searchResult = elastic.Search <BlogPost>(sr => sr
                                                         .From(0)
                                                         .Size(5)
                                                         .Query(q => q.MatchAll())
                                                         .Sort(ss => ss
                                                               .Ascending(p => p.Title)
                                                               .Field(f => f.Field(ff => ff.Title)))
                                                         );

            Console.WriteLine("Search results for Match All!! ==>");
            Console.WriteLine(searchResult.Hits.Count());
            foreach (var hit in searchResult.Hits)
            {
                Console.WriteLine(hit.Source);
            }

            //Query search results using Match
            var blogPostsForSearch = new[]
            {
                new BlogPost {
                    Id    = Guid.NewGuid(),
                    Title = "test post 123",
                    Body  = "1"
                },
                new BlogPost
                {
                    Id    = Guid.NewGuid(),
                    Title = "test something 123",
                    Body  = "2"
                },
                new BlogPost
                {
                    Id    = Guid.NewGuid(),
                    Title = "read this post",
                    Body  = "3"
                }
            };
            var id = 15;

            foreach (var blogPostSearch in blogPostsForSearch)
            {
                var insertRes = elastic.Index(blogPostSearch, p => p
                                              .Id(++id)
                                              .Refresh());
                Console.WriteLine("Match SearchResults IIndexResponse=" + insertRes.IsValid);
            }

            var searchMatch = elastic.Search <BlogPost>(es => es
                                                        .Query(q => q
                                                               .Match(m => m
                                                                      .Field(f => f.Title)
                                                                      .Query("test post 123"))));

            Console.WriteLine("Search results for Match!! ==>");
            Console.WriteLine(searchMatch.Hits.Count());
            foreach (var hit in searchMatch.Hits)
            {
                Console.WriteLine(hit.Source);
            }

            //Match with AND Operator
            var searchMatchAnd = elastic.Search <BlogPost>(es => es
                                                           .Query(q => q
                                                                  .Match(m => m
                                                                         .Field(f => f.Title)
                                                                         .Query("test post 123")
                                                                         .Operator(Operator.And))));

            Console.WriteLine("Search results for Match!! ==>");
            Console.WriteLine(searchMatchAnd.Hits.Count());
            foreach (var hit in searchMatchAnd.Hits)
            {
                Console.WriteLine(hit.Source);
            }

            //MinimumShouldMatch
            var searchMinMatch = elastic.Search <BlogPost>(es => es
                                                           .Query(q => q
                                                                  .Match(m => m
                                                                         .Field(f => f.Title)
                                                                         .Query("test post 123")
                                                                         .Operator(Operator.Or)
                                                                         .MinimumShouldMatch(2))));

            Console.WriteLine("Search results for Min Match!! ==>");
            Console.WriteLine(searchMinMatch.Hits.Count());
            foreach (var hit in searchMinMatch.Hits)
            {
                Console.WriteLine(hit.Source);
            }

            //Bool Query
            var boolQuerySearchResult = elastic.Search <BlogPost>(es => es
                                                                  .Query(qu => qu
                                                                         .Bool(b => b
                                                                               .Must(m =>
                                                                                     m.Match(mt => mt.Field(f => f.Title).Query("title")) &&
                                                                                     m.Match(mt2 => mt2.Field(f => f.Body).Query("002")))))
                                                                  .Sort(so => so.Field(fe => fe.Field(fe1 => fe1.Title))
                                                                        .Ascending(p => p.Title)));

            Console.WriteLine("Search results for Bool with Must!! ==>");
            Console.WriteLine(boolQuerySearchResult.Hits.Count());
            foreach (var hit in boolQuerySearchResult.Hits)
            {
                Console.WriteLine(hit.Source);
            }

            //Using replacing Must with should (or)
            var boolQuerySearchResultShould = elastic.Search <BlogPost>(es => es
                                                                        .Query(qu => qu
                                                                               .Bool(b => b
                                                                                     .Should(m =>
                                                                                             m.Match(mt => mt.Field(f => f.Title).Query("title")) ||
                                                                                             m.Match(mt2 => mt2.Field(f => f.Body).Query("002")))))
                                                                        .Sort(so => so.Field(fe => fe.Field(fe1 => fe1.Title))
                                                                              .Ascending(p => p.Title)));

            Console.WriteLine("Search results for Bool with Should!! ==>");
            Console.WriteLine(boolQuerySearchResultShould.Hits.Count());
            foreach (var hit in boolQuerySearchResultShould.Hits)
            {
                Console.WriteLine(hit.Source);
            }

            //Using bool with MUST NOT
            var boolQuerySearchResultMustNot = elastic.Search <BlogPost>(es => es
                                                                         .Query(qu => qu
                                                                                .Bool(b => b
                                                                                      .Should(m =>
                                                                                              m.Match(mt => mt.Field(f => f.Title).Query("title")) ||
                                                                                              m.Match(mt2 => mt2.Field(f => f.Body).Query("002")))
                                                                                      .Must(ms => ms
                                                                                            .Match(mt3 => mt3.Field(fi => fi.Body).Query("this")))
                                                                                      .MustNot(mn => mn
                                                                                               .Match(mt4 => mt4.Field(fi => fi.Body).Query("003")))))
                                                                         .Sort(so => so.Field(fe => fe.Field(fe1 => fe1.Title))
                                                                               .Ascending(p => p.Title)));

            Console.WriteLine("Search results for Bool with MUST NOT!! ==>");
            Console.WriteLine(boolQuerySearchResultMustNot.Hits.Count());
            foreach (var hit in boolQuerySearchResultMustNot.Hits)
            {
                Console.WriteLine(hit.Source);
            }

            //Using the above query with bitwise operator
            var boolQuerySearchResultBitwise = elastic.Search <BlogPost>(es => es
                                                                         .Query(q =>
                                                                                (q.Match(mt1 => mt1.Field(f1 => f1.Title).Query("title")) ||
                                                                                 q.Match(mt2 => mt2.Field(f2 => f2.Body).Query("002"))) &&
                                                                                (q.Match(mt3 => mt3.Field(fe3 => fe3.Body).Query("this"))) &&
                                                                                (!q.Match(mt4 => mt4.Field(fe4 => fe4.Body).Query("003")))));

            Console.WriteLine("Search results for Bool with Bitwise operator!! ==>");
            Console.WriteLine(boolQuerySearchResultBitwise.Hits.Count());
            foreach (var hit in boolQuerySearchResultBitwise.Hits)
            {
                Console.WriteLine(hit.Source);
            }
            //Nested Types and Nested Query
            Console.WriteLine("*******Nested Types and Nested Query*************");

            var authors = new[]
            {
                new Author {
                    Id = Guid.NewGuid(), FirstName = "John", LastName = "Doe"
                },
                new Author {
                    Id = Guid.NewGuid(), FirstName = "Notjohn", LastName = "Doe"
                },
                new Author {
                    Id = Guid.NewGuid(), FirstName = "John", LastName = "Notdoe"
                }
            };

            foreach (var author in authors)
            {
                IIndexResponse indexResponse2 = elastic.Index(author, i => i
                                                              .Index(indexName)
                                                              .Type(typeof(Author))
                                                              .Id(Guid.NewGuid())
                                                              .Refresh());
                Console.WriteLine("IIndexResponse=" + indexResponse2.IsValid);
            }


            Console.WriteLine("IIndexResponse=" + indexResponse.IsValid);

            var blogPostWithAuthor = new[]
            {
                new BlogPost {
                    Id = Guid.NewGuid(), Title = "test post 1", Body = "1", Author = authors[0]
                },
                new BlogPost {
                    Id = Guid.NewGuid(), Title = "test post 2", Body = "2", Author = authors[1]
                },
                new BlogPost {
                    Id = Guid.NewGuid(), Title = "test post 3", Body = "3", Author = authors[2]
                }
            };



            foreach (var blogPostAuthor in blogPostWithAuthor)
            {
                var resindex = elastic.Index(blogPostAuthor, p => p
                                             .Id(blogPostAuthor.Id.ToString())
                                             .Refresh());
                Console.WriteLine("Match SearchResults IIndexResponse=" + resindex.IsValid);
            }

            Console.WriteLine("*******Nested Query*************");
            var nestedQuery = elastic.Search <BlogPost>(es => es
                                                        .Query(q => q
                                                               .Nested(n => n
                                                                       .Path(b => b.Author)
                                                                       .Query(nq =>
                                                                              nq.Match(m1 => m1.Field(f1 => f1.Author.FirstName).Query("John")) &&
                                                                              nq.Match(m2 => m2.Field(f2 => f2.Author.LastName).Query("Notdoe"))))
                                                               ));

            Console.WriteLine(nestedQuery.IsValid);
            Console.WriteLine(nestedQuery.Hits.Count());
            foreach (var hit in nestedQuery.Hits)
            {
                Console.WriteLine(hit.Source);
            }
            Console.ReadLine();
        }
コード例 #11
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);
        }
コード例 #12
0
ファイル: ESImpl.cs プロジェクト: akumardu/csharpmisc
        public IClusterHealthResponse ClusterHealth()
        {
            var clusterHealth = client.ClusterHealth();

            return(clusterHealth);
        }
コード例 #13
0
        public static List <Sales> ConfigureES(string inputText)
        {
            List <Sales> salesReports = new List <Sales>();

            // 1. Connection URL's elastic search
            var listOfUrls = new Uri[]
            {
                // here we can set multple connectionn URL's...
                new Uri("http://localhost:9200/")
            };

            StaticConnectionPool connPool = new StaticConnectionPool(listOfUrls);
            ConnectionSettings   connSett = new ConnectionSettings(connPool);
            ElasticClient        eClient  = new ElasticClient(connSett);

            //  var see = eClient.DeleteIndex(INDEX_NAME);

            // check the connection health
            var checkClusterHealth = eClient.ClusterHealth();

            if (checkClusterHealth.ApiCall.Success && checkClusterHealth.IsValid)
            {
                // 2. check the index exist or not
                var checkResult = eClient.IndexExists(INDEX_NAME);
                if (!checkResult.Exists)
                {
                    // Raise error to Index not avaliable
                }

                // Search All fileds in a documet type
                //var searchResponse = eClient.Search<GlobalCompany>(s => s
                //.Index(INDEX_NAME)
                //.Type(INDEX_TYPE)
                //.Query(q => q.QueryString(qs => qs.Query(inputText + "*"))));

                // Search particular text field
                var searchResponse = eClient.Search <Sales>(s => s.Index(INDEX_NAME)
                                                            .Query(q => q
                                                                   .Bool(b => b
                                                                         .Should(m => m
                                                                                 .Wildcard(c => c
                                                                                           .Field(f => f.Region).Value(inputText.ToLower() + "*"))))));



                //var results = eClient.Scroll<Salesreport>("10m", searchResponse.ScrollId);
                while (searchResponse.Documents.Any())
                {
                    var res = searchResponse.Documents;
                    var sds = res.Cast <Sales>();
                    salesReports.AddRange(sds);
                    searchResponse = eClient.Scroll <Sales>("10m", searchResponse.ScrollId);
                }

                if (salesReports.Count > 0)
                {
                }
                else
                {
                }

                //var lastRecordResponse = eClient.Search<Salesreport>(s => s
                //    .Index(INDEX_NAME)
                //    .Type(INDEX_TYPE)
                //    .From(0)
                //    .Size(1).Sort(sr => sr.Descending(f => f.Region)));

                if (searchResponse.ApiCall.Success && searchResponse.IsValid)
                {
                }
                else
                {
                    // fail log the exception further use
                    var exception      = searchResponse.OriginalException.ToString();
                    var debugException = searchResponse.DebugInformation.ToString();
                }
            }
            else
            {
                // fail log the exception further use
                var exception      = checkClusterHealth.OriginalException.ToString();
                var debugException = checkClusterHealth.DebugInformation.ToString();
            }

            return(salesReports);
        }
コード例 #14
0
 public BulkInserter(Uri esHost)
 {
     esClient = new ElasticClient(esHost);
     esClient.ClusterHealth();
 }