Elasticsearch提供了近实时的数据操做和搜索能力。默认状况下,从你开始索引/更新/删除你的数据到出现搜索结果的时间会有一秒的延时(刷新间隔)。这个与其它的SQL数据库平台在一个事务完成后当即获取到数据这一点上有很大的优点。html
咱们以前已经看过如何将一个文档放入索引中,让咱们再次回忆一下那个命令:数据库
Http请求:json
PUT /customer/doc/1?pretty { "name": "John Doe" }
curl命令:网络
curl -XPUT 'localhost:9200/customer/doc/1?pretty&pretty' -H 'Content-Type: application/json' -d' { "name": "John Doe" } '
Kibana Console:app
http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_modifying_your_data/1.json
再次补充说明一下,上面的请求将会将一个ID为1的文档加入customer索引。若是咱们再次执行上面的请求,以相同的文档内容或者是不一样的,Elasticsearch将会用这个新文档替换以前的文档(就是以相同的ID从新
加入索引)。curl
Http请求内容:elasticsearch
PUT /customer/doc/1?pretty { "name": "Jane Doe" }
curl命令:ide
curl -XPUT 'localhost:9200/customer/doc/1?pretty&pretty' -H 'Content-Type: application/json' -d' { "name": "Jane Doe" } '
Kibana Console:ui
http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_modifying_your_data/2.json
上述操做将ID为1的文档的name属性从“John Doe”改为了“Jane Doe”。设想另外一种场景,咱们使用一个不一样的ID,这样的话将会建立一个新的文档,而以前的文档仍是保持原样。url
Http请求:
PUT /customer/doc/2?pretty { "name": "Jane Doe" }
curl命令:
curl -XPUT 'localhost:9200/customer/doc/2?pretty&pretty' -H 'Content-Type: application/json' -d' { "name": "Jane Doe" } '
Kibana Console:
http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_modifying_your_data/3.json
上述操做将一个ID为2的文档加入索引。
当将文档加入索引时,ID部分并非必须的。若是没有指定,Elasticsearch将会生产一个随机的ID,而后使用它去索引文档。实际Elasticsearch生成的ID(或者是咱们明确指定的)将会在API调用成功后返回。
以下这个例子演示如何使用隐式的ID将一个文档加入索引:
Http请求:
POST /customer/doc?pretty { "name": "Jane Doe" }
curl命令:
curl -XPOST 'localhost:9200/customer/doc?pretty&pretty' -H 'Content-Type: application/json' -d' { "name": "Jane Doe" } '
Kibana Console:
http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_modifying_your_data/4.json
注意在上面的例子中,当咱们没有明确指定ID的时候,咱们须要使用POST
方法代替PUT
来发送请求。
除了可以新增和替换文档,咱们也能够更新文档。注意虽然Elasticsearch在底层并无真正更新文档,而是当咱们更新文档时,Elasticsearch首先去删除旧的文档,而后加入新的文档。
以下的例子演示如何去更新咱们的以前ID为1的文档,在这里将name属性改成“Jane Doe”:
Http请求内容:
POST /customer/doc/1/_update?pretty { "doc": { "name": "Jane Doe" } }
curl命令:
curl -XPOST 'localhost:9200/customer/doc/1/_update?pretty&pretty' -H 'Content-Type: application/json' -d' { "doc": { "name": "Jane Doe" } } '
Kibana Console:
http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_updating_documents/1.json
以下示例演示如何更新咱们以前ID为1的文档,修改name属性为“Jane Doe”,并同时添加新的age属性:
Http请求内容:
POST /customer/doc/1/_update?pretty { "doc": { "name": "Jane Doe", "age": 20 } }
curl命令:
curl -XPOST 'localhost:9200/customer/doc/1/_update?pretty&pretty' -H 'Content-Type: application/json' -d' { "doc": { "name": "Jane Doe", "age": 20 } } '
Kibana Console:
http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_updating_documents/2.json
更新操做也可使用简单的脚原本执行。以下的示例使用一个脚本将age增长了5:
Http请求:
POST /customer/doc/1/_update?pretty { "script" : "ctx._source.age += 5" }
curl命令:
curl -XPOST 'localhost:9200/customer/doc/1/_update?pretty&pretty' -H 'Content-Type: application/json' -d' { "script" : "ctx._source.age += 5" } '
Kibana Console:
http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_updating_documents/3.json
在上面的示例中,ctx._source
指代的是当前须要被更新的source文档。
Elasticsearch提供了一次更新多个文档的功能,经过使用查询条件(好比SQL的UPDATE-WHERE语句)。详情查看docs-update-by-query API
删除一个文档操做至关的直截了当。以下的示例演示了如何删除咱们以前ID为2的文档:
Http请求内容:
DELETE /customer/doc/2?pretty
curl命令:
curl -XDELETE 'localhost:9200/customer/doc/2?pretty&pretty'
Kibana Console:
http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_deleting_documents/1.json
查看_delete_by_query API去删除匹配特定条件的全部的文档。有一个值得注意的的地方是,直接删除整个索引比经过Query API删除索引中的全部文档更高效。
除了在单个文档上执行索引,更新和删除操做外,Elasticsearch还提供了批操做的功能,经过使用 _bulk
API完成。这个功能很是重要,由于它提供了一种很是高效的机制去经过更少的网络切换尽量快的执行多个操做。
做为一个快速入门示例,以下请求在一个批操做中建立了两个文档:
Http请求内容:
POST /customer/doc/_bulk?pretty {"index":{"_id":"1"}} {"name": "John Doe" } {"index":{"_id":"2"}} {"name": "Jane Doe" }
curl命令:
curl -XPOST 'localhost:9200/customer/doc/_bulk?pretty&pretty' -H 'Content-Type: application/json' -d' {"index":{"_id":"1"}} {"name": "John Doe" } {"index":{"_id":"2"}} {"name": "Jane Doe" } '
Kibana Console:
http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_batch_processing/1.json
以下的示例在一个批操做中首先更新ID为1的文档,而后删除ID为2的文档:
Http请求内容:
POST /customer/doc/_bulk?pretty {"update":{"_id":"1"}} {"doc": { "name": "John Doe becomes Jane Doe" } } {"delete":{"_id":"2"}}
curl命令:
curl -XPOST 'localhost:9200/customer/doc/_bulk?pretty&pretty' -H 'Content-Type: application/json' -d' {"update":{"_id":"1"}} {"doc": { "name": "John Doe becomes Jane Doe" } } {"delete":{"_id":"2"}} '
Kibana Console:
http://localhost:5601/app/kibana#/dev_tools/console?load_from=https://www.elastic.co/guide/en/elasticsearch/reference/current/snippets/_batch_processing/2.json
注意上面的删除操做,删除时只须要指定被删除的文档的ID便可,不须要指定对应的source内容。
批处理API不会由于单条指令失败而所有失败。若是里面有单条指令由于一些缘由失败了,那么整个批处理还会继续执行它后面剩余的指令。当批处理API返回时,它会提供每条指令的执行状态(以发送时的顺序),以便你能够检查一个特定的指令是否失败。