Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
604 views
in Technique[技术] by (71.8m points)

How to update multiple documents that match a query in elasticsearch

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: enter image description here But documents not changed.. enter image description here

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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' -H "Content-Type: application/json" -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 ;).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...