Search code examples
pythonelasticsearchsense

Simple update field in elasticsearch


I started using Elsaticsearch and Sense few weeks ago. Now I need to bulk update String field in all the documents of certain index as follows: If the String starts with "+", update the field to same value without the "+".

old: number: "+212112233" new: number: "212112233"

Is there a simple way for me to do it with the REST DSL or do I need to use Python?

Thanks!


Solution

  • If you can install the update-by-query plugin, there's a way to do it. That plugin works by giving it a query matching the documents to update and a script to update the matching documents.

    curl -XPOST localhost:9200/your_index/your_type/_update_by_query -d '
    {
      "query": {
        "filtered": {
          "filter": {
            "script": {
              "script": "_source[\"your_field\"].indexOf(\"+\") == 0"
            }
          }
        }
      },
      "script": "ctx._source.your_field = ctx._source.your_field.substring(1);"
    }'
    

    Note: replace your_index, your_type and your_field with respectively your index, type and field name.

    So, we tell the plugin to update all documents containing a your_field value that starts with + (not knowing if your_field is an analyzed string or not, here we look directly into the _source to make sure we check the raw string value that was indexed) and then we tell the script to update each matching document by taking a substring of the value leaving out the + sign.