ElasticSearch在文档插入(Insert API)上运行脚本 [英] ElasticSearch run script on document insertion (Insert API)

查看:1057
本文介绍了ElasticSearch在文档插入(Insert API)上运行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用 Index API ElasticSearch 中插入文档时指定脚本吗?通过传递HTTP请求正文中的脚本属性,使用其更新API 更新现有文档时,会出现此功能。我认为在Index API中也是有用的,因为也许有一些用户希望在插入期间自动计算和填充这些字段,而不必在插入后再发送一个额外的Update请求来执行脚本。 p>

解决方案

Elasticsearch 1.3



需要搜索/过滤您要添加的字段,添加到1.3.0中的映射变换功能可能适用于您:


可以通过在映射的transform元素中注册
脚本来将索引文档进行变换。
变换的结果被索引,但原始的来源存储在_source
字段中。


< a href =http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-transform.html =nofollow> http://www.elasticsearch.org/guide/en/ elasticsearch / reference / current / mapping-transform.html



您也可以在获取文档时运行相同的转换,也可以通过添加 _source_transform url参数的请求:


如果_source_transform,get endpoint将重新传输源
参数是set.The变换在任何源
过滤之前执行,但它主要是为了使得很容易看到
传递到索引进行调试。


HTTP://www.elasticse arch.org/guide/en/elasticsearch/reference/current/_get_transformed.html



但是,我不认为_search端点接受_source_transform url参数,所以我不认为你可以将转换应用于搜索结果。



弹性搜索1.4

一对夫妇的功能,使这一切更好。如前所述,更新API允许您指定要执行的脚本。 1.4中的update API也可以接受在upsert的情况下使用的默认文档。从1.4文档:

  curl -XPOST'localhost:9200 / test / type1 / 1 / _update'-d'{
script:ctx._source.counter + = count,
params:{
count:4
},
upsert {
counter:1
}
}'

在上面的示例中,如果文档不存在,则使用upsert键的内容来初始化文档。因此,在上述情况下,新创建的文档中的计数器键值将为1。



现在,如果我们设置 scripted_upsert 为true(scripted_upsert是1.4中的另一个新选项),我们的脚本将针对新初始化的文档运行:

  curl -XPOST'localhost:9200 / test / type1 / 2 / _update'-d'{
script:ctx._source.counter + = count,
params:{
count:4
},
upsert:{
counter:1
},
scripted_upsert:true
}'

在此示例中,如果文档不存在,则计数器键将具有5。



Elasticsearch站点的完整文档。


Is it possible to specify a script be executed when inserting a document into ElasticSearch using its Index API? This functionality exists when updating an existing document with new information using its Update API, by passing in a script attribute in the HTTP request body. I think it would be useful too in the Index API because perhaps there are some fields the user wants to be auto-calculated and populated during insertion, without having to send an additional Update request after the insertion to have the script be executed.

解决方案

Elasticsearch 1.3

If you just need to search/filter on the fields that you'd like to add, the mapping transform capabilities that were added into 1.3.0 could possibly work for you:

The document can be transformed before it is indexed by registering a script in the transform element of the mapping. The result of the transform is indexed but the original source is stored in the _source field.

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-transform.html

You can also have the same transformation run when you get a document as well by adding the _source_transform url parameter to the request:

The get endpoint will retransform the source if the _source_transform parameter is set.The transform is performed before any source filtering but it is mostly designed to make it easy to see what was passed to the index for debugging.

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/_get_transformed.html

However, I don't think the _search endpoint accepts the _source_transform url parameter so I don't think you can apply the transformation to search results. That would be a nice feature request.

Elasticsearch 1.4

Elasticsearch 1.4 added a couple features which makes all this much nicer. As you mentioned, the update API allows you to specify a script to be executed. The update API in 1.4 can also accept a default document to be used in the case of an upsert. From the 1.4 docs:

curl -XPOST 'localhost:9200/test/type1/1/_update' -d '{
    "script" : "ctx._source.counter += count",
    "params" : {
        "count" : 4
    },
    "upsert" : {
        "counter" : 1
    }
}'

In the example above, if the document doesn't exist it uses the contents of the upsert key to initialize the document. So in the case above the counter key in the newly created document will have a value of 1.

Now, if we set scripted_upsert to true (scripted_upsert is another new option in 1.4), our script will run against the newly initialized document:

curl -XPOST 'localhost:9200/test/type1/2/_update' -d '{
    "script": "ctx._source.counter += count",
    "params": {
        "count": 4
    },
    "upsert": {
        "counter": 1
    },
    "scripted_upsert": true
}'

In this example, if the document didn't exist the counter key would have a value of 5.

Full documentation from Elasticsearch site.

这篇关于ElasticSearch在文档插入(Insert API)上运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆