码迷,mamicode.com
首页 > Web开发 > 详细

Elasticsearch.net&Nest 的使用

时间:2020-01-17 15:03:58      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:初始化   地址   客户端   lld   ==   class   使用   linq查询   col   

        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;        }    }

Elasticsearch.net&Nest 的使用

标签:初始化   地址   客户端   lld   ==   class   使用   linq查询   col   

原文地址:https://www.cnblogs.com/DavidHuAtIT/p/12205819.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!