全文检索elasticsearch(已包含分布式) 运行elasticsearch服务器,项目中引入架包,set数据,get数据。其实elasticsearch就是这么简单,许多牛人能写出好几本书那是很有水平的,我就会把这些复杂的,抽丝剥茧成简单的,再深入分析。
https://www.elastic.co/downloads/elasticsearch下载完成之后解压文件
- 1
启动ElasticSearch命令:
- 1
- 1
默认情况下, Elasticsearch 使用 9200 来提供对其 REST API 的访问。 访问http://127.0.0.1:9200/
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
安装head 插件,在浏览器输入地址:http://localhost:9200/_plugin/head/ 即可调用 head 插件 查看集群状态、节点信息、做查询等等。
安装head插件命令:
- 1
- 1
安装head插件命令:
- 1
- 1
安装IK分词器
(a).首先使用Git clone
命令下载IK分词器源码
- 1
- 1
也可以直接访问github地址(https://github.com/medcl/elasticsearch-analysis-ik)点击右侧Clone or download按钮,然后Download ZIP直接下载.
(b.)解压下载的elasticsearch-analysis-ik-master.zip.
- 1
- 1
(c.)使用maven打包
确保系统已经安装maven,使用mvn -version
命令查看是否已经安装maven.如果没有安装,可以根据系统选择安装方法,比如mac OS系统可以使用brew install maven
命令完成安装.
进入ik分词器的下载目录,运行命令:
- 1
- 1
打包完成以后可以看到根目录下多出一个target
文件夹.
(d.) 配置Ik插件
在elasticsearch-2.3.3/plugins/
目录下新建名为ik的文件夹.把elasticsearch-analysis-ik-master/target/releases
/elasticsearch-analysis-ik-1.9.3.zip解压,把解压后的所有文件拷贝到elasticsearch-2.3.3/plugins/ik/
目录下.
重新启动es,如果配置正确,不会有异常信息输出。
测试Ik分词器
1.首先创建一个索引用于测试:
- 1
- 1
2.为索引index创建mapping:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
3.测试:
- 1
- 2
- 3
- 4
- 1
- 2
- 3
- 4
显示结果如下:
索引、类型、文档、字段
- 索引是ElasticSearch存放数据的地方,可以理解为关系型数据库中的一个数据库。
- 类型用于区分同一个索引下不同的数据类型,相当于关系型数据库中的表
- 文档是ElasticSearch中存储的实体,类比关系型数据库,每个文档相当于数据库表中的一行数据。
- 文档由字段组成,相当于关系数据库中列的属性,不同的是ES的不同文档可以具有不同的字段集合。
对比关系型数据库:
- 1
- 2
创建文档
以博客内容管理为例,索引名为blog,类型为article,新加一个文档:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
检索文档
- 1
- 1
更新文档
- 1
- 2
- 3
- 1
- 2
- 3
删除文档
节点与集群
ElasticSearch是一个分布式全文搜索引擎,既可以做为一个独立的搜索服务器工作,也可以使用多台服务器同时运行,这样就构成了一个集群(cluster),集群的每一个服务器称为一个节点(node).
ElasticSearch的强大之处:可以多开几个终端,运行起来集群名相同的节点都在一个集群中。
分片
当数据量比较大的时候,受RAM、硬盘容量的限制,同时一个节点的计算能力有限。可以将数据切分,每部分是一个单独的lucene索引,成为分片(shard)。每个分片可以被存储在集群的不同节点上。当需要查询由多个分片构成的索引时,ElasticSearch将查询发送到每个相关的分片,之后将查询结果合并。过程对应用透明,无须知道分片的存在。
副本
副本是对原始分片的一个精确拷贝,原始分片成为主分片。对索引的所有操作都直接作用在主分片上,每个主分片可以有零个或多个副分片。主分片丢失,集群可以将一个副分片提升为主的新分片。
中文api:
https://es.xiaoleilu.com
英文api:
https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-index.html