如何在弹性搜索中更新与查询匹配的多个文档 [英] How to update multiple documents that match a query in elasticsearch

查看:117
本文介绍了如何在弹性搜索中更新与查询匹配的多个文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我有仅包含url(分析)和respsize(not_analyzed)字段的文档。我想要更新匹配url的文档,并添加新的字段category
我的意思是
at first doc1:

  {
url:http://stackoverflow.com/用户/ 4005632 / mehmet-yener-yilmaz,
respsize:500
}

我有一个外部数据,我知道stackoverflow.com属于第10类,
我需要更新文档,并使其如下:

  {
url:http://stackoverflow.com/users/4005632/mehmet-yener-yilmaz,
respsize :500,
category:10
}

当然我会这样做的所有文件url字段有stackoverflow.com
,我需要更新每个文件一次一次..因为url的类别数据是不可更改的,不需要再次更新。
我需要使用_update api与_version号来检查它,但不能组合dsl查询。
编辑
我运行这个,看起来很好:

但文件没有更改..



尽管查询结果看起来是真的,但新添加到文档中的新字段需要刷新等等。

解决方案

您可以使用通过查询插件更新类别的文档,其 url 匹配某个字符串并添加所需的类别。

  curl -XPOST'localhost:9200 / webproxylog / _update_by_query'-d'
{
查询:{
filtered:{
filter:{
bool:{
must:[
{
term :{
url:stackoverflow.com
}
},
{
missing:{
field类别
}
}
]
}
}
}
},
script:ctx._source .category = \10\;
}'

运行该文件后,所有文档都有 url :没有类别的stackoverflow.com 将获得类别:10 。您可以稍后再次运行相同的查询,以修复在此期间已编入索引的新的 stackoverflow.com 文档。



还要确保在 elasticsearch.yml 中启用脚本,然后重新启动ES:

  script.inline:on 
script.indexed:on

在脚本中,你可以自由添加任意多个字段,例如

  ... 
script:ctx ._source.category1 = \10\; ctx._source.category2 = \20 \;

更新



ES 2.3现在将更新为查询功能。您仍然可以按照原样使用上述查询,它将工作(除了过滤缺少已被弃用,但仍然工作;)。


I have documents which contains only "url"(analyzed) and "respsize"(not_analyzed) fields at first. I want to update documents that match the url and add new field "category" I mean; at first doc1:

{
 "url":"http://stackoverflow.com/users/4005632/mehmet-yener-yilmaz",
 "respsize":"500"
}

I have an external data and I know "stackoverflow.com" belongs to category 10, And I need to update the doc, and make it like:

{
 "url":"http://stackoverflow.com/users/4005632/mehmet-yener-yilmaz",
 "respsize":"500",
 "category":"10"
}

Of course I will do this all documents which url fields has "stackoverflow.com" and I need the update each doc oly once.. Because category data of url is not changeable, no need to update again. I need to use _update api with _version number to check it but cant compose the dsl query. EDIT I run this and looks works fine: But documents not changed..

Although query result looks true, new field not added to docs, need refresh or etc?

解决方案

You could use the update by query plugin in order to do just that. The idea is to select all document without a category and whose url matches a certain string and add the category you wish.

curl -XPOST 'localhost:9200/webproxylog/_update_by_query' -d '
{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "url": "stackoverflow.com"
              }
            },
            {
              "missing": {
                "field": "category"
              }
            }
          ]
        }
      }
    }
  },
  "script" : "ctx._source.category = \"10\";"
}'

After running this, all your documents with url: stackoverflow.com that don't have a category, will get category: 10. You can run the same query again later to fix new stackoverflow.com documents that have been indexed in the meantime.

Also make sure to enable scripting in elasticsearch.yml and restart ES:

script.inline: on 
script.indexed: on

In the script, you're free to add as many fields as you want, e.g.

  ...
  "script" : "ctx._source.category1 = \"10\"; ctx._source.category2 = \"20\";"

UPDATE

ES 2.3 now features the update by query functionality. You can still use the above query exactly as is and it will work (except that filtered and missing are deprecated, but still working ;).

这篇关于如何在弹性搜索中更新与查询匹配的多个文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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