1

修改你的数据

Elasticsearch提供了近实时的数据操作和搜索能力。默认情况下,从你开始索引/更新/删除你的数据到出现搜索结果的时间会有一秒的延时(刷新间隔)。这个与其它的SQL数据库平台在一个事务完成后立即获取到数据这一点上有很大的优势。

将文档放入索引/替换索引中的文档

我们之前已经看过如何将一个文档放入索引中,让我们再次回忆一下那个命令:

Http请求:


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:

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重新
加入索引)。

Http请求内容:

PUT /customer/doc/1?pretty
{
  "name": "Jane Doe"
}

curl命令:

curl -XPUT 'localhost:9200/customer/doc/1?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/2.json

上述操作将ID为1的文档的name属性从“John Doe”改成了“Jane Doe”。设想另一种场景,我们使用一个不同的ID,这样的话将会创建一个新的文档,而之前的文档还是保持原样。

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返回时,它会提供每条指令的执行状态(以发送时的顺序),以便你可以检查一个特定的指令是否失败。


HoloLens
51 声望6 粉丝