Home > Back-end >  How to update a text type field in Elasticsearch to a keyword field, where each word becomes a keywo
How to update a text type field in Elasticsearch to a keyword field, where each word becomes a keywo

Time:11-18

I’m looking to update a field in Elasticsearch from text to keyword type.

I’ve tried changing the type from text to keyword in the mapping and then reindexing, but with this method the entire text value is converted into one big keyword. For example, ‘limited time offer’ is converted into one keyword, rather than being broken up into something like ['limited', 'time', 'offer'].

Is it possible to change a text field into a list of keywords, rather than one big keyword? Also, is there a way to do this with only a mapping change and then reindexing?

CodePudding user response:

You need create a new index and reindex using a pipeline to create a list words.

Pipeline

POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "processors": [
      {
        "split": {
          "field": "items",
          "target_field": "new_list",
          "separator": " ",
          "preserve_trailing": true
        }
      }
    ]
  },
  "docs": [
    {
      "_index": "index",
      "_id": "id",
      "_source": {
        "items": "limited time offer"
      }
    }
  ]
}

Results

{
  "docs": [
    {
      "doc": {
        "_index": "index",
        "_id": "id",
        "_version": "-3",
        "_source": {
          "items": "limited time offer",
          "new_list": [
            "limited",
            "time",
            "offer"
          ]
        },
        "_ingest": {
          "timestamp": "2022-11-11T14:49:15.9814242Z"
        }
      }
    }
  ]
}

Steps

1 - Create a new index

2 - Create a pipeline

PUT _ingest/pipeline/split_words_field
{
  "processors": [
    {
      "split": {
        "field": "items",
        "target_field": "new_list",
        "separator": " ",
        "preserve_trailing": true
      }
    }
  ]
}

3 - Reindex with pipeline

POST _reindex
{
  "source": {
    "index": "idx_01"
  },
  "dest": {
    "index": "idx_02",
    "pipeline": "split_words_field"
  }
}

Example:

PUT _ingest/pipeline/split_words_field
{
  "processors": [
    {
      "split": {
        "field": "items",
        "target_field": "new_list",
        "separator": " ",
        "preserve_trailing": true
      }
    }
  ]
}


POST idx_01/_doc
{
  "items": "limited time offer"
}

POST _reindex
{
  "source": {
    "index": "idx_01"
  },
  "dest": {
    "index": "idx_02",
    "pipeline": "split_words_field"
  }
}

GET idx_02/_search
  • Related