c如何使用es剖析

上传人:今*** 文档编号:105886847 上传时间:2019-10-13 格式:DOC 页数:9 大小:48KB
返回 下载 相关 举报
c如何使用es剖析_第1页
第1页 / 共9页
c如何使用es剖析_第2页
第2页 / 共9页
c如何使用es剖析_第3页
第3页 / 共9页
c如何使用es剖析_第4页
第4页 / 共9页
c如何使用es剖析_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《c如何使用es剖析》由会员分享,可在线阅读,更多相关《c如何使用es剖析(9页珍藏版)》请在金锄头文库上搜索。

1、C#如何使用ESElasticsearch简介Elasticsearch (ES)是一个基于 Lucene 的开源搜索引擎,它不但稳定、可靠、快速,而且也具有良好的水平扩展能力,是专门为分布式环境设计的。Elasticsearch是什么Elasticsearch是一个基于Apache Lucene(TM)的开源搜索引擎,无论在开源还是专有领域,Lucene可以被认为是迄今为止最先进、性能最好的、功能最全的搜索引擎库。 但是,Lucene只是一个库。想要发挥其强大的作用,你需使用C#将其集成到你的应用中。Lucene非常复杂,你需要深入的了解检索相关知识来理解它是如何工作的。 Elasticse

2、arch也是使用Java编写并使用Lucene来建立索引并实现搜索功能,但是它的目的是通过简单连贯的RESTful API让全文搜索变得简单并隐藏Lucene的复杂性。 不过,Elasticsearch不仅仅是Lucene和全文搜索引擎,它还提供:分布式的实时文件存储,每个字段都被索引并可被搜索实时分析的分布式搜索引擎可以扩展到上百台服务器,处理PB级结构化或非结构化数据而且,所有的这些功能被集成到一台服务器,你的应用可以通过简单的RESTful API、各种语言的客户端甚至命令行与之交互。上手Elasticsearch非常简单,它提供了许多合理的缺省值,并对初学者隐藏了复杂的搜索引擎理论。它

3、开箱即用(安装即可使用),只需很少的学习既可在生产环境中使用。Elasticsearch在Apache 2 license下许可使用,可以免费下载、使用和修改。 随着知识的积累,你可以根据不同的问题领域定制Elasticsearch的高级特性,这一切都是可配置的,并且配置非常灵活。以上内容来自 百度百科 关于ES详细概念见:http:/88250.b3log.org/full-text-search-elasticsearch#b3_solo_h3_0 使用C#操作ESNEST是一个高层的客户端,可以映射所有请求和响应对象,拥有一个强类型查询DSL(领域特定语言),并且可以使用.net的特性比

4、如协变、Auto Mapping Of POCOs,NEST内部使用的依然是Elasticsearch.Net客户端。(NEST)客户端提供了强类型查询DSL,方便用户使用,源码下载。一、如何安装NEST打开VS的工具菜单,通过NuGet包管理器控制台,输入以下命令安装NESTInstall-Package NEST安装后引用了以下三个DLLElasticsearch.Net.dll(2.4.4)Nest.dll(2.4.4)Newtonsoft.Json.dll(9.0版本)二、链接elasticsearch你可以通过单个节点或者指定多个节点使用连接池链接到Elasticsearch集群,使

5、用连接池要比单个节点链接到Elasticsearch更有优势,比如支持负载均衡、故障转移等。通过单点链接:1 var node = new Uri(http:/myserver:9200);2 var settings = new ConnectionSettings(node);3 var client = new ElasticClient(settings); 通过连接池链接:复制代码 1 var nodes = new Uri 2 3 new Uri(http:/myserver1:9200), 4 new Uri(http:/myserver2:9200), 5 new Uri(ht

6、tp:/myserver3:9200) 6 ; 7 8 var pool = new StaticConnectionPool(nodes); 9 var settings = new ConnectionSettings(pool);10 var client = new ElasticClient(settings);复制代码 NEST Index为了知道请求需要操作哪个索引,Elasticsearch API期望收到一个或多个索引名称作为请求的一部分。一、指定索引1、可以通过ConnectionSettings使用.DefaultIndex(),来指定默认索引。当一个请求里没有指定具体索

7、引时,NEST将请求默认索引。1 var settings = new ConnectionSettings()2 .DefaultIndex(defaultindex);2、可以通过ConnectionSettings使用.MapDefaultTypeIndices(),来指定被映射为CLR类型的索引。1 var settings = new ConnectionSettings()2 .MapDefaultTypeIndices(m = m3 .Add(typeof(Project), projects)4 ); 注意:通过.MapDefaultTypeIndices()指定索引的优先级要

8、高于通过.DefaultIndex()指定索引,并且更适合简单对象(POCO)3、另外还可以显示的为请求指定索引名称,例如:1 var response = client.Index(student, s=s.Index(db_test);2 var result = client.Search(s = s.Index(db_test);3 var result = client.Delete(null, s = s.Index(db_test);4 注意:当现实的为请求指定索引名称时,这个优先级是最高的,高于以上两种方式指定的索引。4、一些Elasticsearch API(比如query)

9、可以采用一个、多个索引名称或者使用_all特殊标志发送请求,请求NEST上的多个或者所有节点复制代码 1 /请求单一节点 2 var singleString = Nest.Indices.Index(db_studnet); 3 var singleTyped = Nest.Indices.Index(); 4 5 ISearchRequest singleStringRequest = new SearchDescriptor().Index(singleString); 6 ISearchRequest singleTypedRequest = new SearchDescriptor(

10、).Index(singleTyped); 7 8 /请求多个节点 9 var manyStrings = Nest.Indices.Index(db_studnet, db_other_student);10 var manyTypes = Nest.Indices.Index().And();11 12 ISearchRequest manyStringRequest = new SearchDescriptor().Index(manyStrings);13 ISearchRequest manyTypedRequest = new SearchDescriptor().Index(ma

11、nyTypes);14 15 /请求所有节点16 var indicesAll = Nest.Indices.All;17 var allIndices = Nest.Indices.AllIndices;18 19 ISearchRequest indicesAllRequest = new SearchDescriptor().Index(indicesAll);20 ISearchRequest allIndicesRequest = new SearchDescriptor().Index(allIndices);复制代码 二、创建索引Elasticsearch API允许你创建索引的

12、同时对索引进行配置,例如:1 var descriptor = new CreateIndexDescriptor(db_student)2 .Settings(s = s.NumberOfShards(5).NumberOfReplicas(1);3 4 client.CreateIndex(descriptor); 这里指定了该索引的分片数为5、副本数为1。三、删除索引Elasticsearch API允许你删除索引,例如:1 var descriptor = new DeleteIndexDescriptor(db_student).Index(db_student);2 3 clien

13、t.DeleteIndex(descriptor) 这里制定了要删除的索引名称“db_student”,以下为更多删除用例:1 /删除指定索引所在节点下的所有索引2 var descriptor = new DeleteIndexDescriptor(db_student).AllIndices(); NEST MappingNEST提供了多种映射方法,这里介绍下通过Attribute自定义映射。一、简单实现1、定义业务需要的POCO,并指定需要的Attribute复制代码 1 ElasticsearchType(Name = student) 2 public class Student 3

14、 4 Nest.String(Index = FieldIndexOption.NotAnalyzed) 5 public string Id get; set; 6 7 Nest.String(Analyzer = standard) 8 public string Name get; set; 9 10 Nest.String(Analyzer = standard)11 public string Description get; set; 12 13 public DateTime DateTime get; set; 14 复制代码 2、接着我们通过.AutoMap()来实现映射复制代码1 var descriptor = new CreateIndexDescriptor(db_student)2 .Settings(s = s.NumberOfShards(5).NumberOfReplicas(1)3 .Mappings(ms = ms4 .Map

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号