[ElasticSearch]基本操作

ElasticSearch 基本操作

本篇博文介绍的是 ElasticSearch-5.6.3基本操作。

查看集群健康状况

curl -XGET 'localhost:9200/_cat/health?v&pretty'

执行结果如下所示:

epoch      timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1509052590 05:16:30  leo     yellow          1         1     26  26    0    0       26             0                  -                 50.0%

status有以下三种状态:
- green, 表示集群一切 ok;
- yellow, 数据可用但是一些分片未分配;
- red,部分数据不可用。这种情况就需要处理了。

查看集群所有节点

curl -XGET 'localhost:9200/_cat/nodes?v&pretty'

执行结果如下所示:

host      ip        heap.percent ram.percent load node.role master name
127.0.0.1 127.0.0.1            6          97 3.54 d         *      Turac

可以发现,集群现在只有一个节点,命名为“Turac”。

查看集群所有索引

curl -XGET 'localhost:9200/_cat/indices?v'

结果如下所示:

health status index    pri rep docs.count docs.deleted store.size pri.store.size
yellow open   myweibo1   5   1          7            0     22.3kb         22.3kb

目前只用一个索引“myweibo1”。

新建索引

##新键索引
curl -XPUT 'localhost:9200/employees'
## 查看索引
curl -XGET 'localhost:9200/_cat/indices?v'
##查看_settings
curl -XGET 'localhost:9200/employees/_settings'

新建索引后,执行查看索引语句结果如下:

yellow open   employees   5   1          0            0       260b           260b

新增了employees这个索引。
查看_settings,结果如下:

    "employees": {
        "settings": {
            "index": {
                "creation_date": "1509088075357",
                "number_of_shards": "5",
                "number_of_replicas": "1",
                "uuid": "0twhlJ89QuSF1Y1p_Mj7dw",
                "version": {
                    "created": "2040699"
                }
            }
        }
    }
}

其中 number_of_shards 的默认值是5,number_of_replicas 的默认值是1,当然建立索引时可以如下进行设置:

curl -XPUT 'localhost:9200/employees' -d '{
    "settings": {
            "index": {
                "number_of_shards": "3",
                "number_of_replicas": "1",
              }
        }
   }'

再次查看_settings,结果如下:

{
    "employees": {
        "settings": {
            "index": {
                "creation_date": "1509088734594",
                "number_of_shards": "3",
                "number_of_replicas": "1",
                "uuid": "xBVUUhm5SwWvoMQoTW8y1w",
                "version": {
                    "created": "2040699" }
            }
        }
    }
}

删除索引

curl -XDELETE 'localhost:9200/employees/'

执行查看索引命令,索引“employees ”就没有了。

创建文档

curl -XPUT "http://localhost:9200/<index>/<type>/[<id>]" -d'{JSON}'

以上是ES 创建文档的一般格式,其中 index 和 type 是必选项,id 可选。
指定 id 如下所示:

curl -XPOST "http://localhost:9200/employees/employee/1" -d'
{
    "name":"Leo",
    "age":"27",
    "tel":12345678901
}'

执行结果如下所示:

{
    "_index": "employees",
    "_type": "employee",
    "_id": "1",
    "_version": 1,
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "created": true
}

不指定 id 如下所示:

curl -XPOST "http://localhost:9200/employees/employee/" -d'
{
    "name":"Ming",
    "age":"27",
    "tel":12345643901
}'

执行结果如下所示:

{
    "_index": "employees",
    "_type": "employee",
    "_id": "AV9czyvByznybavM0vVu",
    "_version": 1,
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "created": true
}

如上所示,当不指定id时,系统会自动分配一个 id,本例中 id 为“AV9czyvByznybavM0vVu”

查看文档

 curl -XGET "http://localhost:9200/employees/employee/1"

执行结果如下所示:

{
    "_index": "employees",
    "_type": "employee",
    "_id": "1",
    "_version": 1,
    "found": true,
    "_source": {
        "name": "Leo",
        "age": "27",
        "tel": 12345678901
    }
}

只查看_source字段

curl -XGET 'http://localhost:9200/employees/employee/1/_source'

执行结果如下所示:

{
    "name":"Leo",
    "age":"27",
    "tel":12345678901
}

更新文档

curl -XPOST "http://localhost:9200/employees/employee/1" -d'
{
    "name":"Leo",
    "age":"27",
    "tel":23415678901
}'

执行结果如下所示:

{
    "_index": "employees",
    "_type": "employee",
    "_id": "1",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "created": false
}

由于前面在创建文档时已经创建了 id 为1的雇员,所以此时是执行的更新操作,”_version”字段从1变为了2, “result”字段为updated,执行了修改电话号码的操作。如果索引中无 id 为1的雇员,此时会新建一个文档。

删除文档

curl -XDELETE "http://localhost:9200/employees/employee/1" 

文档删除。

批量创建文档

curl -XPOST 'localhost:9200/employees/employee/_bulk?pretty&pretty'  -d'
{"index":{"_id":"2"}}
{"name": "John Doe", "age":"28", "tel":234567776}
{"index":{"_id":"3"}}
{"name": "Jane Doe", "age":"38", "tel":234334764}
'

从文件导入数据到索引中

以官网数据account.json为例。下载问价到 ElasticSearch根目录下,执行以下命令:

curl -H "Content-Type: application/json" -XPOST 'localhost:9200/bank/account/_bulk?pretty&refresh' --data-binary "@accounts.json"
curl 'localhost:9200/bank/account/_count'

执行结果如下所示:

{
    "count": 1000,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    }
}

共有1000条数据导入。

清理索引缓存

curl -XPOST "http://localhost:9200/employees/_cache/clear"

刷新索引数据

curl -XPOST "http://localhost:9200/employees/_refresh"

猜你喜欢

转载自blog.csdn.net/lionel_fengj/article/details/78344327