es 笔记
HTTP协议给出了可以在API调用中用作动词的一组相当长的类型。合乎逻辑的选择是,
GET用来获得请求对象的当前状态,POST来改变对象的当前状态,PUT创建一个对象,而DELETE销毁对象,
另外还有个HEAD请求仅仅用来获取对象的基础信息。
es 命令 查看节点健康状态
curl -XGET http://127.0.0.1:9200/_cluster/health?pretty
参数-X是一个请求方法,默认值是GET(所以在上面的例子中,可以忽略此参数)。暂时不要担心GET这个值
http://192.168.0.168:9200/_cluster/health?pretty
关掉整个集群
curl -XPOST http://localhost:9200/_cluster/nodes/_shutdown
为关闭单一节点,假如节点标识符是BlrmMvBdSKiCeYGsiHijdg
curl –XPOST http://localhost:9200/_cluster/nodes/BlrmMvBdSKiCeYGsiHijdg/_shutdown
这个命令获取集群中节点的信息。
节点的标识符可以在日志中看到,或者使用_cluster/nodes API,命令如下:
curl -XGET http://localhost:9200/_cluster/nodes/
http://192.168.0.168:9200/_cluster/state/nodes/
POST http://localhost:9200/_cluster/nodes/_shutdown:这个命令向集群中所有节点发送一个shutdown请求。
PUT 新增 索引
curl -XPUT http://localhost:9200/blog/article/1 -d'{"title": "New version of Elasticsearch released!", content": "Version 1.0 released today!", "tags": ["announce","elasticsearch", "release"] }'
POST 修改索引
curl -XPOST http://localhost:9200/blog/article/ -d '{"title": "New version of Elasticsearch released!", "content": "Version 1.0 released today!", "tags": ["announce", "elasticsearch","release"] }'
检索文档
curl -XGET http://localhost:9200/blog/article/1
更新文档
curl -XPOST http://localhost:9200/blog/article/1/_update -d '{"script": "ctx._source.content = \"new content\""}'
删除文档
curl -XDELETE http://localhost:9200/blog/article/1
我们将使用一个名为blog的索引和名为article的类型。为了把示例文档以给定类型、标识符为1建立在索引中,执行以下命令:curl -XPUT http://localhost:9200/blog/article/1 -d '{"title": "New version of Elasticsearch released!", content": "Version 1.0 released today!", "tags": ["announce","elasticsearch", "release"] }'
注意cURL命令的一个新选项:-d参数。此选项的值是将作为请求负载的文本,也即请求主体(request body)。这样,我们可以发送附加信息,如文档定义。同时,注意唯一标识符(1)是
放在URL,而不是请求主体中。使用HTTP PUT请求时,如果省略此标识符,该请求将返回以下
Elasticsearch必须首先获取文档,从_source属性获得数据,删除旧的文件,更改_source属性,然后把它作为新的文档来索引。它如此复杂,因为信息一旦在Lucene的倒排索引中存储,就不能再被更改。Elasticsearch通过一个带_update参数的脚本来实现它。这样就可以做比简单修改字段更加复杂的文档转换。下面用简单的例子看看的工作原理。
POST /books/es/1/_update
{
"script": "ctx._source.title = \"new content\""
}
关于文档更新,还有一点,如果你的脚本需要更新文档的一个字段,你可以设置一个值用来处理文档中没有该字段的情况。例如,想增加文档中的counter字段,而该字段不存在,你可以在请求中使用upsert节来提供字段的默认值。看下面的例子:
curl -XPOST http://localhost:9200/blog/article/1/_update -d '{
"script": "ctx._source.counter =ctx._source.counter + 1",
"upsert": {
"counter" : 0
}
}'
POST /books/es/1/_update
{
"script": "ctx._source.counter =ctx._source.counter + 1",
"upsert": {
"counter" : 0
}
}
curl –XGET 'localhost:9200/books/_search?pretty&q=title:elasticsearch'
通过在elasticsearch.yml配置文件中添加以下指令来关闭自动创建索引:
action.auto_create_index: false