How can we reindex with existing documents to a new type along with already existing types using painless


(Shivaprasad) #1

In an index I have 3 types type A, type B, type C along with their properties,
I have to reindex with a new type D using painless.

I am using Elasticsearch version 5.6.9


(David Pilato) #2

May be I did not get the question but you don't need painless for that:

POST _reindex
{
  "source": {
    "index": "twitter",
    "type": "a",
  },
  "dest": {
    "index": "new_twitter",
    "type": "_doc"
  }
}
POST _reindex
{
  "source": {
    "index": "twitter",
    "type": "b",
  },
  "dest": {
    "index": "new_twitter",
    "type": "_doc"
  }
}
POST _reindex
{
  "source": {
    "index": "twitter",
    "type": "c",
  },
  "dest": {
    "index": "new_twitter",
    "type": "_doc"
  }
}

Note that in 6.x you are not allowed anymore to create indices with multiple types so it's better to be prepared for that and to start a new index instead.


(Shivaprasad) #3

Sorry I didn't explain it properly,
below if we see we have _type = exams and we have studentUid = 112546
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 10,
"successful": 10,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 597680,
"max_score": 1,
"hits": [
{
"_index": "dev-environment",
"_type": "exams",
"_id": "112546",
"_score": 1,
"_routing": "689",
"_source": {

      "studentUid": "112546",
      "isValid": true

So now if I reindex I want _type = student with studentUid = 112546 as property (value comes from exams type) using painless script something like this below.

	  {

"took": 7,
"timed_out": false,
"_shards": {
"total": 10,
"successful": 10,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 597680,
"max_score": 1,
"hits": [
{
"_index": "dev-environment",
"_type": "student",
"_id": "112546",
"_score": 1,
"_routing": "689",
"_source": {

      "studentUid": "112546",
      "isValid": true
	  }

I went through Reindex api (https://www.elastic.co/guide/en/elasticsearch/reference/5.6/docs-reindex.html) and also _type field api (https://www.elastic.co/guide/en/elasticsearch/reference/5.4/mapping-type-field.html) but nowhere they have mentioned reindexing to a new _type ( student ) with property values taking from existing _type (exams) existing


(David Pilato) #4

Please format your code, logs or configuration files using </> icon as explained in this guide and not the citation button. It will make your post more readable.

Or use markdown style like:

```
CODE
```

This is the icon to use if you are not using markdown format:

There's a live preview panel for exactly this reasons.

Lots of people read these forums, and many of them will simply skip over a post that is difficult to read, because it's just too large an investment of their time to try and follow a wall of badly formatted text.
If your goal is to get an answer to your questions, it's in your interest to make it as easy to read and understand as possible.
Please update your post.

Did you try what I shared?

POST _reindex
{
  "source": {
    "index": "dev-environment",
    "type": "exams",
  },
  "dest": {
    "index": "new_dev-environment",
    "type": "student"
  }
}

(system) #5

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