Renaming fields in elasticsearch

后端 未结 2 1152
醉梦人生
醉梦人生 2020-12-20 18:01

I have a document like this

{
    \"_index\": \"testindex\",
    \"_type\": \"logs\",
    \"_id\": \"1\",
    \"_score\": 1,
    \"_source\": {
      \"field         


        
相关标签:
2条回答
  • 2020-12-20 18:15

    The Request field does not yet exist in your documents, so your script needs to create it first:

    POST _reindex
    {
        "source": {
            "index": "testindex"
        },
        "dest": {
            "index": "testindex1"
        },
        "script": {
            "inline": "ctx._source.Request = [:]; ctx._source.Request.field3 = ctx._source.remove(\"field2\") ]"
        }
    }
    

    Or a bit shorter like this:

    POST _reindex
    {
        "source": {
            "index": "testindex"
        },
        "dest": {
            "index": "testindex1"
        },
        "script": {
            "inline": "ctx._source.Request = [field3: ctx._source.remove(\"field2\") ]"
        }
    }
    
    0 讨论(0)
  • 2020-12-20 18:18

    The easiest way to rename the field name is to use the _update_by_query API:

    Example: POST http://localhost:9200/INDEX_NAME/_update_by_query

    {
      "query": { 
        "bool": {
            "must_not": {
                "exists": {
                    "field": "NEW_FIELD_NAME"
                }
            }
        }
      },
      "script" : {
        "inline": "ctx._source.NEW_FIELD_NAME = ctx._source.OLD_FIELD_NAME; ctx._source.remove(\"OLD_FIELD_NAME\");"
      }
    }
    
    0 讨论(0)
提交回复
热议问题