# Reindexing the data from an existing index to new index with Completion fields

**URL:** <https://discuss.elastic.co/t/reindexing-the-data-from-an-existing-index-to-new-index-with-completion-fields/323471>\
**Category:** Kibana\
**Tags:** painless\
**Created:** [January 19, 2023, 4:53am UTC](https://discuss.elastic.co/t/reindexing-the-data-from-an-existing-index-to-new-index-with-completion-fields/323471 "2023-01-19T04:53:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vaibhav\_Sinha](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vaibhav_sinha/32/116130_2.png) [@Vaibhav\_Sinha](https://discuss.elastic.co/u/Vaibhav_Sinha)\
**Post date:** [January 19, 2023, 4:53am UTC](https://discuss.elastic.co/t/reindexing-the-data-from-an-existing-index-to-new-index-with-completion-fields/323471/1 "2023-01-19T04:53:38Z")

</div>

Hey Team,

I need some help reindexing the data from an existing Index (keyword/string-based fields) to a new index with fields of type Completion.

I have created the new index to be used for Completion Suggesters with field mapping as such:

```auto
 PUT product_completion_suggestions
{
  "settings": {
    "analysis": {
      "analyzer": {
        "suggestion_analyzer": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "shingle"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "title": {
        "type": "completion",
        "analyzer": "suggestion_analyzer"
      },
      "id": {
        "type": "integer"
      },
      "brand": {
        "type": "completion",
        "analyzer": "suggestion_analyzer"
      },
      "category": {
        "type": "completion",
        "analyzer": "suggestion_analyzer"
      },
      "genericproductname": {
        "type": "completion",
        "analyzer": "suggestion_analyzer"
      },
      "variety": {
        "type": "completion",
        "analyzer": "suggestion_analyzer"
      }
    }
  }
}

```

After creating the index, I am looking to reindex the data from an existing index into this new index via the reindex API as such:

```auto
POST _reindex?wait_for_completion=true
{
  "source": {
    "index": "product-v2",
    "_source": [
       "stockcode",
       "genericproductname",
       "brand",
       "variety"
       ],
    "query": {
      "bool": {
        "must": [
          {
            "exists": {
              "field": "displayname"
            }
          },
          {
            "exists": {
              "field": "genericproductname"
            }
          },
          {
            "exists": {
              "field": "brand"
            }
          }
        ]
      }
    }
  },
  "dest": {
    "index": "product_v2_completion_suggestions"
  },
  "script": {
    "source": """def concat = ' '; concat = concat + (ctx._source.brand != null ? ctx._source.brand : ''); concat = concat + ' '; concat = concat + (ctx._source.genericproductname != null ? ctx._source.genericproductname : (ctx._source.variety != null ? ctx._source.variety : '')); ctx._source.title.input = new def[]{concat.trim()};"""
  }
}

```

Note: 'title' is a dynamically calculated field, and the field's value is a concatenation of three fields, i.e. brand, genericproductname and variety. Also that I am mapping 'keyword' type fields to Completion types in the ReIndex API call above as mentioned before.

But I keep getting compile time error:

```auto
script_stack" : [
      "ctx._source.title.input = new def[]{concat.trim()};",
      " ^---- HERE"
    ],
    "script" : "def concat = ' '; concat = concat + (ctx._source.brand != null ? ctx._source.brand : ''); concat = concat + ' '; concat = concat + (ctx._source.genericproductname != null ? ctx._source.genericproductname : (ctx._source.variety != null ? ctx._source.variety : '')); ctx._source.title.input = new def[]{concat.trim()};",
    "lang" : "painless",
    "position" : {
      "offset" : 282,
      "start" : 265,
      "end" : 316
    },
    "caused_by" : {
      "type" : "null_pointer_exception",
      "reason" : "Cannot invoke \"Object.getClass()\" because \"callArgs[0]\" is null"
    }
  },
  "status" : 400

```

My intention is to index the data into new Completion fields as such:

```auto
"title": {
 "input": "some_text",
"weight": 10
}

```

via the reindex script, but I cannot achieve it clearly, and some how my script above is incorrect. I need to have a default weight assigned to the fields as they are being reindexed. I couldn't find any documentation regarding mapping data from existing fields into new completion fields while capturing the input and weight fields during reindexing.

Any help or assistance here will be much appreciated.

Thanks.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 16, 2023, 4:54am UTC](https://discuss.elastic.co/t/reindexing-the-data-from-an-existing-index-to-new-index-with-completion-fields/323471/2 "2023-02-16T04:54:11Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
