Elasticsearch

安装与启动

教程:《ElasticSearch:权威指南》
http://www.ruanyifeng.com/blog/2017/08/elasticsearch.html
https://www.e-learn.cn/content/java/1078247

下载:https://www.elastic.co/cn/downloads/elasticsearch

报错:

  • 启动elasticsearch时,不能以root身份运行。解决方法是:新建用户,并切换用户,重新启动es即可;
adduser yangsong  # 新建用户
passwd yangsong  # 设置新用户密码
useradd yangsong -p yangsong  # 设置新用户及密码
chown -R user_name file_name  # 用root用户执行,赋权新用户
su yangsong  # 切换用户
sh ./bin/elasticsearch/sh elasticsearch  # 重新启动es
  • 启动elasticsearch时,报错log为max virtual memory areas vm.maxmapcount [65530] is too low。解决方法如下:
sysctl -a|grep vm.max_map_count  # 显示最大虚拟内存
sysctl -w vm.max_map_count=262144  # 指定最大虚拟内存为262144
  • 验证elasticsearch是否正确安装时:切记在另外一个Xshell窗口输入curl localhost:9200,而本窗口的启动界面不可关闭、不可退出

常用命令

# --------------------- 查看集群信息 ---------------------
curl localhost:9200  # 测试es是否安装成功
curl localhost:9200/_cat/health?v  # 查看es集群健康状态
curl localhost:9200/_cat/indices?v  # 查看es集群的索引数目
curl localhost:9200/_mapping?pretty=true  # 查看分组
curl localhost:9200/_cat/allocation?v  # 查看es集群磁盘的分配状况
curl localhost:9200/_cat/nodes?v  # 查看es集群的节点
curl -X GET "localhost:9200/_cat/nodes?v"  # 同上,换一种命令写法
curl localhost:9200/_cat/  # 查看es集群的其它信息

# --------------------- 常用操作 ---------------------
curl -X PUT "localhost:9200/yangsong?pretty"  # 创建索引yangsong
curl -X PUT "localhost:9200/yangsong/_doc/1?pretty" -H 'Content-Type: application/json' -d'{"name": "John Doe"}'  # 向索引增加数据
curl -X GET "localhost:9200/yangsong?pretty"  # 查询索引
curl 'localhost:9200/accounts/person/_search'  # 查询所有信息
curl -X GET "localhost:9200/yangsong/_doc/1?pretty"  # 查询文档1
curl -X DELETE "localhost:9200/yangsong?pretty"  # 删除索引
curl -X DELETE "localhost:9200/customer/_doc/2?pretty"  # 删除ID=2的文档

# 更新
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d
'
{
  "doc": { "name": "Jane Doe", "age": 20 }
}
'

curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d
'
{
  "script" : "ctx._source.age += 5"
}
'

# 批处理
curl -X POST "localhost:9200/customer/_doc/_bulk?pretty" -H 'Content-Type: application/json' -d
'
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
'  # 索引两个

curl -X POST "localhost:9200/customer/_doc/_bulk?pretty" -H 'Content-Type: application/json' -d
'
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
'  # 更新文档1,删除文档2


Kibana

Kibana:配合Elasticsearch使用的Web展示工具;

下载:https://www.elastic.co/cn/downloads/kibana

Demo

Tps:

  1. Elasticsearch是NoSQL的一种,属于文档型数据库,常用与存储json格式信息;
  2. Elasticsearch与传统关系型数据库的类比:index-库、type-表、document-行、fields-列;

版权声明:本文为qq_34100655原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。