• Elasticsearch.net&Nest 的使用


            static void Main(string[] args)
            {
    
    
    
    
    
                Nest();
    
                Console.WriteLine("End..");
    
                Console.ReadKey();
    
                //MyUser user = new MyUser();
                //var indexResponse = lowlevelClient.Index<byte[]>("user", "guest", user.name, user);
                //byte[] responseBytes = indexResponse.Body;
                
            }
    
    
    
            private static void Elasticsearch_net()
            {
                var settings = new ConnectionConfiguration(new Uri("http://localhost:9200"))
                   .RequestTimeout(TimeSpan.FromMinutes(2));
    
                ElasticLowLevelClient lowlevelClient = new ElasticLowLevelClient(settings);
    
                ElasticsearchResponse<string> rs = lowlevelClient.Search<string>("megacorp", "employee", new
                {
                    query = new
                    {
                        match = new
                        {
                            first_name = "John"
                        }
                    }
                });
    
                var json = rs.Body;
    
                Console.WriteLine(json);
    
            }
    
    
    
            private static void Nest()
            {
                IESSever eSSever = new ESSever("http://localhost:9200/");
    
                var list = eSSever.ElasticLinqClient.Search<employee>(
                                 p => p.Index("megacorp").Type("employee")
                                       .Query(op => op.Match(x=>x.Field(e=>e.first_name== "John"))));
    
    
                var rs = list.Documents.ToList();
                foreach (var item in rs)
                {
                    Console.WriteLine(item.first_name);
                }
            }
    
    
        }
    
        public class employee
        {
            public string first_name { get; set; }
    
            public string last_name { get; set; }
    
            public int age { get; set; }
    
            public string about { get; set; }
    
    
            public List<string> interests { get; set; }
            public IApiCallDetails ApiCall { get;set; }
    
            public bool TryGetServerErrorReason(out string reason)
            {
                reason = "MrbinError";
                return true;
            }
        }

    ESSever:

    using Elasticsearch.Net;
    using Nest;
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace ElasticSearch
    {
        /// <summary>
        /// 访问ElasticSearch服务类
        /// </summary>
        public class ESSever : IESSever
        {
            /// <summary>
            /// Linq查询的官方Client
            /// </summary>
            public IElasticClient ElasticLinqClient { get; set; }
            /// <summary>
            /// Js查询的官方Client
            /// </summary>
            public IElasticLowLevelClient ElasticJsonClient { get; set; }
            public ESSever(string connetionStr)
            {
                var uris = new List<Uri>();//配置节点地址,以,分开
                uris.Add(new Uri(connetionStr));
                var connectionPool = new StaticConnectionPool(uris);//配置请求池
                var settings = new ConnectionSettings(connectionPool).RequestTimeout(TimeSpan.FromSeconds(30));//请求配置参数
                this.ElasticJsonClient = new ElasticLowLevelClient(settings);//json请求客户端初始化
                this.ElasticLinqClient = new ElasticClient(settings);//linq请求客户端初始化
            }
    
    
        }
    }

    IESSever:

    using Elasticsearch.Net;
    using Nest;
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace ElasticSearch
    {
        /// <summary>
        /// 访问ElasticSearch服务接口类
        /// </summary>
        public interface IESSever
        {
            /// <summary>
            /// Linq查询的官方Client
            /// </summary>
            IElasticClient ElasticLinqClient { get; set; }
    
            /// <summary>
            /// Js查询的官方Client
            /// </summary>
            IElasticLowLevelClient ElasticJsonClient { get; set; }
        }
    }


            static void Main(string[] args)        {




                Nest();
                Console.WriteLine("End..");
                Console.ReadKey();
                //MyUser user = new MyUser();            //var indexResponse = lowlevelClient.Index<byte[]>("user", "guest", user.name, user);            //byte[] responseBytes = indexResponse.Body;                    }


            private static void Elasticsearch_net()        {            var settings = new ConnectionConfiguration(new Uri("http://localhost:9200"))               .RequestTimeout(TimeSpan.FromMinutes(2));
                ElasticLowLevelClient lowlevelClient = new ElasticLowLevelClient(settings);
                ElasticsearchResponse<string> rs = lowlevelClient.Search<string>("megacorp", "employee", new            {                query = new                {                    match = new                    {                        first_name = "John"                    }                }            });
                var json = rs.Body;
                Console.WriteLine(json);
            }


            private static void Nest()        {            IESSever eSSever = new ESSever("http://localhost:9200/");
                var list = eSSever.ElasticLinqClient.Search<employee>(                             p => p.Index("megacorp").Type("employee")                                   .Query(op => op.Match(x=>x.Field(e=>e.first_name== "John"))));

                var rs = list.Documents.ToList();            foreach (var item in rs)            {                Console.WriteLine(item.first_name);            }        }

        }
        public class employee    {        public string first_name { get; set; }
            public string last_name { get; set; }
            public int age { get; set; }
            public string about { get; set; }

            public List<string> interests { get; set; }        public IApiCallDetails ApiCall { get;set; }
            public bool TryGetServerErrorReason(out string reason)        {            reason = "MrbinError";            return true;        }    }

  • 相关阅读:
    软工实践-Alpha 冲刺 (7/10)
    软工实践-Alpha 冲刺 (6/10)
    软工实践-Alpha 冲刺 (5/10)
    软工实践-Alpha 冲刺 (4/10)
    BETA 版冲刺前准备
    第十一次作业
    Alpha 冲刺 (9/10)
    Alpha 冲刺 (8/10)
    Alpha 冲刺 (7/10)
    Alpha 冲刺 (6/10)
  • 原文地址:https://www.cnblogs.com/DavidHuAtIT/p/12205819.html
Copyright © 2020-2023  润新知