ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Elasticsearch.net&Nest 的使用

2020-01-17 15:01:17  阅读:190  来源: 互联网

标签:set name get Nest Elasticsearch var new net public


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

标签:set,name,get,Nest,Elasticsearch,var,new,net,public
来源: https://www.cnblogs.com/DavidHuAtIT/p/12205819.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有