elasticsearch常用API

124 阅读1分钟

一、快速检查集群的健康状况:GET /_cat/health?v。集群健康状况分为三种:green,yellow,red

       green:每个index的primary shard和replica shard都是active状态

        yellow:每个index的primary shard都是active状态,但是部分replica shard不是active状态

         red:不是所有的index的primary shard都是active状态,部分index有数据丢失了

二、查看集群中有哪些index:GET /_cat/indices?v

三、简单的索引操作

       创建索引:PUT /test_index?pretty

       删除索引: DELETE /test_index?pretty

四、举例商品的CRUD

1:、新增商品,新增文档,建立索引

 语法:PUT /index/type/id { json数据 }

 PUT /test_index2/product/1

       {
"name" : "gaolujie yagao",
"desc" :  "gaoxiao meibai",
"price" :  30,
"producer" :      "gaolujie producer",
"tags": [ "meibai", "fangzhu" ]
}

     es会自动建立index和type,不需要提前建好,而且es会默认对每个document的field都建立倒排索引,可以让其被搜索

2、查询商品,检索文档

语法:GET /index/type/id

GET /test_index2/product/1

3、更新商品,替换文档

PUT /test_index2/product/1
{
"name" : "jiaqiangban gaolujie yagao",
"desc" :  "gaoxiao meibai",
"price" :  30,
"producer" :      "gaolujie producer",
"tags": [ "meibai", "fangzhu" ]
}

此种方法有个缺点,就是document必须带上所有的field才能进行修改,如果只是写了修改name,那么{}里面name了,没有其他的field了

4、更新商品,更新文档

POST /test_index2/product/1/_update

{
"doc": {
"name": "jiaqiangban gaolujie yagao"
}
}

5、删除文档

DELETE /test_index2/product/1