简体   繁体   中英

In Elasticsearch, how to move data from one field into another field

I have an index with mappings that look like this:

"mappings": {
  "default": {
    "_all": {
      "enabled": false
    },
    "properties": {
      "Foo": {
        "properties": {
          "Bar": {
            "type": "keyword"
          }
      }
    }
  }
}

I am trying to change the mapping to introduce a sub-field of Bar, called Code, whilst migrating the string currently in Bar into Bar.Code. Here is the new mapping:

"mappings": {
  "default": {
    "_all": {
      "enabled": false
    },
    "properties": {
      "Foo": {
        "properties": {
          "Bar": {
            "properties": {
              "Code": {
                "type": "keyword"
              }
            }
          }
      }
    }
  }
}

In order to do this, I think I need to do a _reindex and specify a pipeline. Is that correct? If so, how does my pipeline access the original data?

I have tried variations on the following code, but without success:

PUT _ingest/pipeline/transformFooBar
{
    "processors": [
      {
        "set": {
          "field": "Bar.Code",
          "value": "{{_source.Bar}}"
        }
      }
    ]
}

POST _reindex
{
  "source": {
    "index": "foo_v1"
  },
  "dest": {
    "index": "foo_v2",
    "pipeline": "transformFooBar"
  }
}

Ah, I almost had the syntax right. The _source is not required:

// Create a pipeline with a SET processor
PUT _ingest/pipeline/transformFooBar
{
    "processors": [
      {
        "set": {
          "field": "Bar.Code",
          "value": "{{Bar}}"
        }
      }
    ]
}

// Reindex using the above pipeline
POST _reindex
{
  "source": {
    "index": "foo_v1"
  },
  "dest": {
    "index": "foo_v2",
    "pipeline": "transformFooBar"
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM