下载Elasticsearch
mkdir /home/es
cd /home/es
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.0.0.zip
解压:
unzip elasticsearch-6.0.0.zip
因为是开箱即用,所以不需要安装
切换到bin目录下
cd /home/es/elasticsearch6.0.0/bin
执行
./elasticsearch -d (-d是后台运行,如果不输入,当前Console会被锁死,不能进行其他操作,执行Ctrl + c,可关闭Elasticsearch;如果执行-d,则无法看见执行的详细信息)
即可运行
如果需要覆盖集群或者节点的名字,则在启动时,添加参数:
./elasticsearch --cluster.name my_cluster_name --node.name my_node_name -d
要检查集群健康,我们将使用_cat API。需要事先记住的是,我们的节点HTTP的端口是9200:
curl 'localhost:9200/_cat/health?v'
如下:
============================================================
[root@localhost ~]# curl 'localhost:9200/_cat/health?v'
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1512571194 22:39:54 elasticsearch green 1 1 0 0 0 0 0 0 - 100.0%
[root@localhost ~]#
============================================================
绿色:一切正常(集群功能齐全)
黄色:所有的数据都是可用的,但是某些复制没有被分配(集群功能是完备的)
红色:因为某些原因,某些数据不可用。
注意,即使是集群状态是红色的,集群仍然是部分可用的(它仍然会利用可用的分片来响应搜索请求),但是可能你需要尽快修复它,因为你有丢失的数据
获得节集群中的节点列表:
curl 'localhost:9200/_cat/nodes?v'
============================================================
[root@localhost ~]# curl 'localhost:9200/_cat/nodes?v'
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
127.0.0.1 11 96 1 0.00 0.04 0.05 mdi * qnnd6ME
============================================================
显示索引
curl 'localhost:9200/_cat/indices?v'
============================================================
[root@localhost ~]# curl 'localhost:9200/_cat/indices?v'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open customer n8UenwhWTr-8Y84WPsU92g 5 1 1 0 4.7kb 4.7kb
============================================================
创建索引
curl -XPUT 'localhost:9200/[索引名]?[返回json格式:pretty 美化的格式]'
例如curl -XPUT 'localhost:9200/myindex?pretty'
============================================================
[root@localhost ~]# curl -XPUT 'localhost:9200/myindex?pretty'
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "myindex"
}
============================================================
插入数据
curl -XPUT 'localhost:9200/[索引名]/[类型名]/[指定id]' -d '{"key":"value","key":"value",...}'
例如:
curl -XPUT 'localhost:9200/myindex/external/1' -d '{"firstName":"John","LastName":"Smith"}'
注意:-d之前,必需要空格!!!!
但是执行之后,出现错误:
============================================================
[root@localhost ~]# curl -XPUT 'localhost:9200/myindex/external/1' -d '{"firstName":"John","LastName":"Smith"}'
{"error":"Content-Type header [application/x-www-form-urlencoded] is not supported","status":406}[root@localhost ~]#
============================================================
格式化输入依然报错
============================================================
bash-4.2$ curl -XPUT 'localhost:9200/myindex/external/1?pretty' -d '
{
"firstName":"John"
"LastName":"Smith"
}'
{
"error" : "Content-Type header [application/x-www-form-urlencoded] is not supported",
"status" : 406
}
============================================================
这个问题,是在报文Content-type的参数:application/x-www-form-urlencoded不支持Json发送。需要改成application/Json
所以需要添加参数 ; -H 'Content-Type: application/json'
:::::Content-Type现有类型不支持JSON等格式传递
curl -XPUT 'localhost:9200/myindex/external/1' -H 'Content-Type: application/json' -d '{"firstName":"John","LastName":"Smith"}'
curl -XPUT 'localhost:9200/myindex/external/1?pretty' -H 'Content-Type: application/json' -d '
{
"firstName":"John",
"LastName":"Smith"
}'
============================================================
[root@localhost ~]# curl -XPUT 'localhost:9200/myindex/external/1?pretty' -H 'Content-Type: application/json' -d '
> {
> "firstName":"John",
> "LastName":"Smith"
> }'
{
"_index" : "myindex",
"_type" : "external",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
============================================================
取出索引:
curl -XGET 'localhost:9200/myindex/external/1?pretty'
============================================================
[root@localhost ~]# curl -XGET 'localhost:9200/myindex/external/1?pretty'
{
"_index" : "myindex",
"_type" : "external",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"firstName" : "John",
"LastName" : "Smith"
}
}
============================================================
_source部分,就是我们添加的数据
_id是之前添加的索引id
删除索引 curl -XDELETE 'localhost:9200/myindex?pretty'
============================================================
[root@localhost ~]# curl -XDELETE 'localhost:9200/myindex?pretty'
{
"acknowledged" : true
}
============================================================