在实际开发中,主要有三种方式可以作为elasticsearch服务的客户端:
(1)elasticsearch-head插件
(2)使用ElasticSearch提供的Restful接口
(3)使用ElasticSearch提供的API
1、elasticsearch-head插件
(1)下载head插件
ElasticSearch自带图形化界面,通过安装head插件可以完成图形化界面效果,完成索引数据查询。head插件的安装方式有在线安装和本地安装。
如果需要本地安装,可以到github上下载head插件,地址:https://github.com/mobz/elasticsearch-head
或者直接通过git克隆到本地:git clone git://github.com/mobz/elasticsearch-head.git
(2)下载nodejs:https://nodejs.org/en/download/
(3)将grunt安装为全局命令,grunt是基于Node.js的项目构建工具,在cmd控制台中输入如下执行命令:
# -g表示全局安装
npm install -g grunt-cli
cd到elasticsearch-head的目录执行启动命令
npm install
grunt server
(4)访问head客户端
访问 localhost:9100
,即可进入head客户端,然后连接 192.168.127.131:9200 就可以连上es服务器
2、Restful接口访问
这里使用使用PostMan进行Restful接口访问
2.1、ElasticSearch的接口语法
curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/PATH?<QUERY_STRING>' -d '<BODY>'
2.2、同步创建索引、类型和映射
创建索引用PUT
请求,跟在端口号后面的为索引名,在本案例中索引为blog1,索引中有一个类型 article
,它有3个字段,分别是 id
、title
、content
,type
表示字段的类型,store
表示是否存储,index
表示是否用于作为索引,analyzer
表示分词器,分词器会把字段分为一个个词,在搜索时搜索其中的任意一个词都能搜到对应文档。分词器可以使用es的standard分词器,这个分词器对英文比较友好,但是在分中文的时候会有一点问题,它会把中文分成一个字一个字。如果需要对中文分词可以使用IKAnalyzer分词器。
PUT http://192.168.127.131:9200/blog1
{
"mappings": {
"article": {
"properties": {
"id": {
"type": "long",
"store": true,
"index": "not_analyzed"
},
"title": {
"type": "text",
"store": true,
"index": "analyzed",
"analyzer":