标签:
ElasticiSearch 简介 ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。
因为在工作的项目中有使用到所以写下相关的内容,并附带源码 感兴趣的朋友可以自己玩一玩,整个项目都是在Linux上跑的,所以安装步骤都以Linux为主。什么?你不会Linux? 学啊...........
rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
在 /etc/yum.repos.d/ 目录下创建.repo 后缀的文件,如elasticsearch.repo,编辑内容如下:
[elasticsearch-2.x]
name=Elasticsearch repository for2.x packages
baseurl=https://packages.elastic.co/elasticsearch/2.x/centos
gpgcheck=1
gpgkey=https://packages.elastic.co/GPG-KEY-elasticsearch
enabled=1
yum install elasticsearch
chkconfig --add elasticsearch
编辑文件/etc/elasticsearch/elasticsearch.yml,根据实际情况配置如下节点(也可使用默认)
运行命令:
Service ElasticSearch start
参考地址:
下载地址:
编辑文件config/kibana.yml ,配置属性:
.bin/kibana
参考地址:
Marvel是Elasticsearch的管理和监控工具
在ES目录下运行 bin/plugin 安装插件许可
bin/plugin install license
运行bin/plugin install 安装Marvel代理插件
bin/plugin install marvel-agent
运行命令:
bin/kibana plugin --install elasticsearch/marvel/latest
参考地址:
一个flask写的elasticsearch查询工具:
支持es查询语言自动提示,es结构自动提示,支持两种主题,支持查询历史记录,支持快捷键。
到Kibana目录运行命令安装 Sense插件
./bin/kibana plugin --install elastic/sense
重新启动 Kibana
参考地址:
git clone https://github.com/medcl/elasticsearch-analysis-ik.git
去源码根目录下编译
cd elasticsearch-analysis-ik
mvn clean
mvn compile
mvn package
将文件 #{project_path}/elasticsearch-analysis-ik/target/releases/elasticsearch-analysis-ik-*.zip 复制解压到 elasticsearch的目录: plugins/ik
这几个插件安装完成后效果如下
.net 版的客户端 是NEST 语法有相应的官网文档解释很全,也很简单 http://nest.azurewebsites.net/nest/search/basics.html
示例代码只写了最常用的对索引的一些基本操作 新建, 删除, 添加数据, 局部更新,给索引设别名
1 public class ElasticSearchService 2 { 3 private IElasticClient EsClient; 4 public ElasticSearchService() 5 { 6 EsClient = ElasticSearchConfig.EsClient; 7 } 8 9 /// <summary> 10 /// 创建索引并设置别名 11 /// </summary> 12 /// <returns></returns> 13 public string CreateIndex() 14 { 15 string newIndex = ElasticSearchConfig.IndexName + DateTime.Now.ToString("yyyyMMddHHmm"); 16 //创建索引,设置mapping 17 var response = EsClient.CreateIndex(newIndex, c => 18 c.Mappings(m => m.Map<DoctorEntity>(map => map.AutoMap())) 19 .Settings(setting=>setting.NumberOfShards(3).NumberOfReplicas(1)));//3个分片,1个副本 20 if (!response.Acknowledged) 21 { 22 return "创建索引失败" + response.ServerError.Error.Reason; 23 } 24 //设置别名 25 var aliasResponse = EsClient.Alias(new BulkAliasDescriptor().Add(c => c.Index(newIndex).Alias(ElasticSearchConfig.IndexName))); 26 if (!aliasResponse.Acknowledged) 27 { 28 return "设置索引别名失败" + aliasResponse.ServerError.Error.Reason; 29 } 30 31 return "创建索引并设置别名成功!"; 32 } 33 34 /// <summary> 35 /// 移除别名与索引 36 /// </summary> 37 /// <returns></returns> 38 public string DeleteIndex() 39 { 40 //获取别名下的索引 41 var alias = EsClient.CatAliases(a => a.Name(ElasticSearchConfig.IndexName)); 42 BulkAliasDescriptor aliasBulk = new BulkAliasDescriptor(); 43 var indexNames = new List<string>(); 44 foreach (var record in alias.Records) 45 { 46 //移除别名下的索引 47 aliasBulk.Add(new AliasRemoveDescriptor().Index(record.Index).Alias(record.Alias)); 48 indexNames.Add(record.Index); 49 } 50 var response = EsClient.Alias(aliasBulk); 51 52 //删除旧索引 53 indexNames.ForEach(index => EsClient.DeleteIndex(new DeleteIndexRequest(index))); 54 55 if (!response.Acknowledged&&alias.Records.Any()) 56 { 57 return "索引与别名关系移除失败!" + response.ServerError.Error.Reason; 58 } 59 60 return "索引与别名关系移除成功!"; 61 } 62 63 /// <summary> 64 /// 切换索引别名 65 /// </summary> 66 public string ChangeIndexAliase(string newIndexName) 67 { 68 //1 创建新的索引后,将别名指向的索引改为新索引名 69 var alias = EsClient.CatAliases(new CatAliasesRequest(ElasticSearchConfig.IndexName)); 70 71 BulkAliasDescriptor aliasDescriptor = new BulkAliasDescriptor(); 72 73 foreach (var record in alias.Records) 74 { 75 aliasDescriptor.Add(new AliasRemoveDescriptor().Index(record.Index).Alias(record.Alias)); 76 } 77 78 aliasDescriptor.Add(new AliasAddDescriptor().Alias(ElasticSearchConfig.IndexName).Index(newIndexName)); 79 var response = EsClient.Alias(aliasDescriptor); 80 if (!response.Acknowledged) 81 { 82 return "切换索引别名失败" + response.ServerError.Error.Reason; 83 } 84 85 return string.Empty; 86 } 87 88 /// <summary> 89 /// 向索引中添加数据 90 /// </summary> 91 /// <param name="doctorEntities"></param> 92 /// <returns></returns> 93 public string AddDoctorInfoToIndex(List<DoctorEntity> doctorEntities) 94 { 95 96 BulkRequest bulk = new BulkRequest(ElasticSearchConfig.IndexName) 97 { 98 Operations = new List<IBulkOperation>() 99 }; 100 foreach (var doctorEntity in doctorEntities) 101 { 102 bulk.Operations.Add(new BulkIndexOperation<DoctorEntity>(doctorEntity)); 103 } 104 105 var response = EsClient.Bulk(bulk); 106 if (response.Errors) 107 { 108 return "添加索引数据失败" + response.Items.First().Error; 109 } 110 111 return "添加索引数据成功!"; 112 } 113 114 /// <summary> 115 /// 更新索引数据 116 /// </summary> 117 public string UpdateDoctorInfoToIndex(List<DoctorEntity> doctorEntities) 118 { 119 var bulk = new BulkRequest(ElasticSearchConfig.IndexName) 120 { 121 Operations = new List<IBulkOperation>() 122 }; 123 BulkUpdateDescriptor<DoctorEntity, PartialDoctorEntity> updateDescriptor = new BulkUpdateDescriptor<DoctorEntity, PartialDoctorEntity>(); 124 125 foreach (var doctorEntity in doctorEntities) 126 { 127 var updatedescript = updateDescriptor.IdFrom(doctorEntity)//会自动推断出document的id 128 .Doc(PartialDoctorEntity.Generate(doctorEntity)) 129 .Upsert(doctorEntity) 130 .RetriesOnConflict(3); 131 132 bulk.Operations.Add(updatedescript); 133 } 134 135 var response = EsClient.Bulk(bulk); 136 137 if (response.Errors) 138 { 139 return "更新索引数据失败" + response.ItemsWithErrors; 140 } 141 142 return "更新索引数据成功!"; 143 } 144 145 146 /// <summary> 147 /// 删除索引中数据 148 /// </summary> 149 /// <param name="doctors"></param> 150 /// <returns></returns> 151 public string DeleteDoctorInfoToIndex(List<DoctorEntity> doctors) 152 { 153 154 BulkRequest bulk = new BulkRequest(ElasticSearchConfig.IndexName) 155 { 156 Operations = new List<IBulkOperation>() 157 }; 158 159 foreach (var doctor in doctors) 160 { 161 var deleteDescript = new BulkDeleteDescriptor<DoctorEntity>().Document(doctor); 162 bulk.Operations.Add(deleteDescript); 163 } 164 165 var response = EsClient.Bulk(bulk); 166 if (response.Errors) 167 { 168 return "删除索引数据失败" + response.Items.First().Error; 169 } 170 171 return "删除索引数据成功"; 172 } 173 174 /// <summary> 175 /// 查询索引数据 176 /// </summary> 177 /// <param name="doctorId"></param> 178 /// <returns></returns> 179 public List<DoctorEntity> QueryDoctors(string doctorId) 180 { 181 SearchDescriptor<DoctorEntity> searchDescriptor =new SearchDescriptor<DoctorEntity>(); 182 183 searchDescriptor.Query(q => q.Term(t => t.Field("doctorId").Value(doctorId))); 184 185 var result = EsClient.Search<DoctorEntity>(searchDescriptor); 186 187 return result.Documents.ToList(); 188 } 189 190 public List<DoctorEntity> BuiDoctorEntities() 191 { 192 var doctorList = new List<DoctorEntity>(); 193 string[] doctorNames = new string[] { "石霖", "陆桂香", "蔡江云", "刘玉凤", "谭志团", "贾雁平", "周琼华", "张平", "周华", "赵子龙" }; 194 for (int i = 0; i < 10; i++) 195 { 196 doctorList.Add(new DoctorEntity() 197 { 198 DoctorId = "5588235" + i, 199 DoctorName = doctorNames[i], 200 DoctorNumber = "134" + i, 201 DepartmentNumber = "654", 202 HospitalDepartmentId = Guid.NewGuid().ToString(), 203 HospitalDepartmentName = "内科", 204 HospitalId = Guid.NewGuid().ToString(), 205 HospitalName = "北京大学深圳医院", 206 HospitalNumber = "bjdxszyy", 207 ProfessionalDepartmentId = "225" + i, 208 ProfessionalDepartmentName = "心胸内科", 209 SupplierNumber = "Thirdpart" 210 }); 211 } 212 213 return doctorList; 214 } 215 216 217 }
以上只是对ElasticSearch 最简单基本的一些操作与概念,大家如果有兴趣可以继续深入了解。
GitHub 源码下载:https://github.com/ZeryZhang/ElasticSearchDemo
参考资料: http://www.linuxidc.com/Linux/2015-02/114243.htm
https://www.elastic.co/
http://nest.azurewebsites.net/nest/search/basics.html
ElasticSearch+Kibana 索引操作( 附源码)
标签:
原文地址:http://www.cnblogs.com/zery/p/5366115.html