卷前:
- centos 7.0
- yum源安装
elasticsearch.yml配置文件:
network.host: 0.0.0.0 http.port: 9200
注意冒号后面的空格是必须的。
kibina.yml配置文件:
server.port= 5601 server.host= 0.0.0.0
注意以上两处IP配置也可以指定ip,kibina指定的是ES的IP,这里因为是单机安装所以用的0.0.0.0
ES虚拟机安装常见问题:
- 虚拟内存太小
- 需要创建用户并授权ES安装目录777权限后运行ES
- 关闭防火墙或者添加例外
vi /etc/sysctl.conf #添加 vm.max_map_count = 655360 #查看 sysctl -p
#新增用户、新增组 useradd -m -g root pcshao passwd pcshao #授权ES安装目录777权限 chown -R pcshao /usr/local/elastic-search-6.5.1
#停止防火墙(cenetos7下) systemctl stop firewalld.service #设置为开机不启动防火墙 systemctl disable firewalld.service
存储数据到ES的行为叫做索引,一个ES集群包含多个索引,每个索引包含多个类型,每类类型存储多个文档(可认为java对象),每个文档包含多个属性。
基本操作:
//创建pcshao索引——user类型——文档1——属性username等等
PUT /pcshao/user/1{ "username":"yeah", "age":20 }
//取到pcshao索引——user类型——文档1
GET /pcshao/user/1
//再PUT一次,还是文档1
PUT /pcshao/user/1{ "username":"shaopc", "age":20 }
//不会失败,但也不会覆盖,由于乐观锁机制,会加上version版本,如下面json中的version属性
{ "_index" : "pcshao", "_type" : "user", "_id" : "1", "_version" : 2, "found" : true, "_source" : { "name" : "shaopc", "age: 20 } }
//那如果手动指定version?
PUT /pcshao/user/1?version=2 { "name":"shaopc", "age":31 }
//会报error
CURL操作:
curl -X<verb> ‘协议://主机:端口/索引?参数…’
curl -X<verb> ‘协议://主机:端口/索引?参数…’ -d ‘body’
Verb常用操作,GET、PUT、POST等等
取:GET
curl -X GET "localhost:9200/site-info/url/_search" -H 'Content-Type: application/json' -d' { "query" : { "sites" : { "filter" : { "term" : { "url" : "www.pcshao.cn" } } } } } '
存:PUT
curl -X DELETE "localhost:9200/site-info" curl -X PUT "localhost:9200/my_store" -H 'Content-Type: application/json' -d' { "mappings" : { "sites" : { "properties" : { "url" : { "type" : "string", "index" : "not_analyzed" } } } } '
9200端口restful接口操作
GET http://172.16.31.128:9200/_analyze HEADER Content-Type:application/json BODY { "analyzer": "standard", "text": "牛掰pcshao" }
{ "tokens": [ { "token": "牛", "start_offset": 0, "end_offset": 1, "type": "<IDEOGRAPHIC>", "position": 0 }, { "token": "掰", "start_offset": 1, "end_offset": 2, "type": "<IDEOGRAPHIC>", "position": 1 }, { "token": "pcshao", "start_offset": 2, "end_offset": 8, "type": "<ALPHANUM>", "position": 2 } ] }
//默认IK-SMART分词器不支持中文
可去https://github.com/pcshao/elasticsearch-analysis-ik下载与你ES对应版本号的中文分词器!
扩展自定义分词
cd ..ESdir/plugin/ik/config vi custom/my_word.dic
注意先停掉ES后修改文件并保存
文档映射
- 动态映射,给文档的每个属性自动映射不同的类型,类似于var
- 静态映射,事先定义
- text类型:可分词查询
- keyword类型:不可分词查询,精确term查询
#静态映射 PUT /myway POST /myway/_mapping/role { "role":{ "properties":{ "role_id":{ "type":"integer" }, "user_id":{ "type":"integer" } } } } PUT /myway/role/1 { "role_id":1, "user_id":1 }
{ "acknowledged" : true }
//此时role文档已经定义了静态类型映射,如果强行插入非integer,会报400
{ "error": { "root_cause": [ { "type": "mapper_parsing_exception", "reason": "failed to parse field [role_id] of type [integer]" } ], "type": "mapper_parsing_exception", "reason": "failed to parse field [role_id] of type [integer]", "caused_by": { "type": "number_format_exception", "reason": "For input string: \"123a\"" } }, "status": 400 }
9200端口与9300端口区别
- 9200TCP协议,集群之间通讯
- 9300暴露restful接口
ELK大型分布式日志分析系统
E(es) L(logstash) K(kibana)
推荐使用kibana作为ES的图形化操作界面,也可使用postman、jmeter等api友好支持app测试和使用elasticsearch
卷尾: