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

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

时间: 2024-08-03 22:13:52

Elasticsearch.net&Nest 的使用的相关文章

ElasticSearch.net NEST批量创建修改删除索引完整示例

本示例采用Elasticsearch+Nest 网上查了很多资料,发现用C#调用Elasticsearch搜索引擎的功能代码很分散,功能不完整,多半是非常简单的操作,没有成型的应用示例.比如新增或修改索引,都是发起一个request新增或修改一条数据,当一次性修改几千条数据时,发起的requst请求过多容易导致429 Too Many Request的错误,单个新增修改索引就非常不适用.其实Nest有批量新增.修改索引的功能,批量删除也可以.现将项目中采用Elasticsearch的C#代码分享

使用ElasticSearch,Kibana,ASP.NET Core和Docker可视化数据

想要轻松地通过许多不同的方式查询数据,甚至是从未预料到的方式?想要以多种方式可视化日志?同时支持基于时间.文本和其他类型的即时过滤器? 借助于 Elastic stack 的卓越性能和可扩展方式的优点,我们将通过两个示例轻松实现. 本文由 DNC Magazine for Developers and Architects 发布. 从这里下载此杂志[PDF] 或 免费订阅本杂志 下载所有以前和当前的版本版本. 在这篇文章中,我将介绍流行的搜索引擎 Elasticsearch,其配套的可视化应用

.net Elasticsearch 学习入门笔记

一. es安装相关1.elasticsearch安装  运行http://localhost:9200/2.head插件3.bigdesk插件安装(安装细节百度:windows elasticsearch 安装,有详细内容) 二. es插件相关http://www.searchtech.pro/elasticsearch-plugins (es 插件大全)https://github.com/medcl/elasticsearch-analysis-ik (ik 项目)https://githu

[翻译] 使用ElasticSearch,Kibana,ASP.NET Core和Docker可视化数据

原文地址:http://www.dotnetcurry.com/aspnet/1354/elastic-search-kibana-in-docker-dotnet-core-app 想要轻松地通过许多不同的方式查询数据,甚至是从未预料到的方式?想要以多种方式可视化日志?同时支持基于时间.文本和其他类型的即时过滤器?借助于 Elastic stack 的卓越性能和可扩展方式的优点,我们将通过两个示例轻松实现. 本文由 DNC Magazine for Developers and Archite

[2017-12-20]ElasticSearch 小记

介绍 ElasticSearch是一款搜索引擎中间件,因其强大的全文索引.查询统计能力和非常方便的全套基于Restful的接口,以及在自动分片.无停机升级扩容.故障转移等运维方面的高效性,逐渐成为中小型甚至非专门处理搜索业务的大型公司的首选搜索引擎方案. 入门可以看完整汉化的<Elasticsearch: 权威指南>,但打算上手实践或者应用到生产时,建议还是过一遍对应你所使用版本的英文文档. 安装 之前有写过一篇ELK安装笔记,这里就不重复介绍了. 基本管理 之前在搭建ELK的时候,我并没有深

(转)c#使用nest

elsticsearch中安装了x-pack后,查询时就需要用户名和密码了. 无账号密码,不可访问 curl http://192.168.0.2:9200/testindex/_count?pretty=true Authentication Required 访问basic认证的页面 (1)通过user选项带上账号密码,返回正常数据 curl –user elastic:changeme http://192.168.0.2:9200/testindex/_count?pretty=true

DotNet 资源大全中文版(Awesome最新版)

Awesome系列的.Net资源整理.awesome-dotnet是由quozd发起和维护.内容包括:编译器.压缩.应用框架.应用模板.加密.数据库.反编译.IDE.日志.风格指南等. API 框架 NancyFx:轻量.用于构建 HTTP 基础服务的非正式(low-ceremony)框架,基于.Net 及 Mono 平台. 官网 ASP.NET WebAPI:快捷创建 HTTP 服务的框架,可以广泛用于多种不同的客户端,包括浏览器和移动设备. 官网 ServiceStack:架构缜密.速度飞快

DotNet 资源

DotNet 资源 目录 API 应用框架(Application Frameworks) 应用模板(Application Templates) 人工智能(Artificial Intelligence) 程序集处理(Assembly Manipulation) 资源(Assets) 认证和授权(Authentication and Authorization) 自动构建(Build Automation) 缓存(Caching) CLI CLR CMS 代码分析和度量(Code Analys

.Net开源框架列表

API 框架 NancyFx:轻量.用于构建 HTTP 基础服务的非正式(low-ceremony)框架,基于.Net 及 Mono 平台.官网 ASP.NET WebAPI:快捷创建 HTTP 服务的框架,可以广泛用于多种不同的客户端,包括浏览器和移动设备.官网 ServiceStack:架构缜密.速度飞快.令人愉悦的 web 服务.官网 Nelibur:Nelibur 是一个使用纯 WCF 构建的基于消息的 web 服务框架.Nelibur 可以便捷地创建高性能.基于消息的 web 服务,使