# Stringify object during reindexing

**URL:** <https://discuss.elastic.co/t/stringify-object-during-reindexing/295900>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [February 1, 2022, 4:45am UTC](https://discuss.elastic.co/t/stringify-object-during-reindexing/295900 "2022-02-01T04:45:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Animesh\_Agarwal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/animesh_agarwal/32/80330_2.png) [@Animesh\_Agarwal](https://discuss.elastic.co/u/Animesh_Agarwal)\
**Post date:** [February 1, 2022, 4:45am UTC](https://discuss.elastic.co/t/stringify-object-during-reindexing/295900/1 "2022-02-01T04:45:02Z")

</div>

Hi Team,

I am looking for a solution to convert an object to string during reindexing. I searched for solutions using painless scripting but was unable to do so as it seems painless doesn't support JSON ( might be wrong here )

Can anyone help in this regard ?

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [February 1, 2022, 4:54am UTC](https://discuss.elastic.co/t/stringify-object-during-reindexing/295900/2 "2022-02-01T04:54:45Z")

</div>

Please share document mapping summary, current script and result, and desired result.

---

<div class="post-metadata">

**Author:** ![Animesh\_Agarwal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/animesh_agarwal/32/80330_2.png) [@Animesh\_Agarwal](https://discuss.elastic.co/u/Animesh_Agarwal)\
**Post date:** [February 1, 2022, 5:24am UTC](https://discuss.elastic.co/t/stringify-object-during-reindexing/295900/3 "2022-02-01T05:24:32Z")

</div>

Say we have a document in an index index-a

```auto
{
  "req":{
    "body":{
      "a":1,
      "b":3,
      "c":{
        "d":0,
        "e":1
      }
    }
 }
}

```

And I want to stringify, req.body while reindexing into another index say index-b.

I tried with

```auto
POST _reindex
{
  "source": {
    "index": "index-a"
  },
  "dest": {
    "index": "index-b"
  },
  "script":{
      "inline":"ctx._source.req.body= ctx._source.req.bodytoString()",
      "lang": "painless"
    }
}

```

But this will not stringify the object correctly.  
toString() will produce {a:1,b:3,c:{d:0,e:1}}

whereas the desired result is '{"a":1,"b":3,"c":{"d":0,"e":1}}'

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [February 1, 2022, 6:21am UTC](https://discuss.elastic.co/t/stringify-object-during-reindexing/295900/4 "2022-02-01T06:21:09Z")

</div>

There is no class handle Json in painless, as you said. If you have only numerical fields, the easiest way might be to replace '{' with '{"' and so on. If it is not so simple, maybe you have to make your custom stringifier (possibly recursive) function of hash.

---

<div class="post-metadata">

**Author:** ![Animesh\_Agarwal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/animesh_agarwal/32/80330_2.png) [@Animesh\_Agarwal](https://discuss.elastic.co/u/Animesh_Agarwal)\
**Post date:** [February 1, 2022, 7:06am UTC](https://discuss.elastic.co/t/stringify-object-during-reindexing/295900/5 "2022-02-01T07:06:15Z")

</div>

Thanks @Tomo_M for the reply. Yes, looks like we need to implement the stringify function in painless script. Our object contains complex data types as well apart from numerical.

Was wondering if there is any other approach ?  
We thought of enforcing the mapping on the new index ( index-b in the above example )

```auto
PUT index-b/_mapping/_doc
{
    "properties" : {
      "req.body" : {
        "type" : "object",
        "enabled": false
      }
    }
}

```

But in this case, the field req.body cannot be searched. Is there a way during indexing itself we enforce that the field `req.body` be indexed as string.

We don't have control over the data coming from source, so we can't stringify the req.body at the source. We need a solution to do it at the Elasticsearch layer

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [February 1, 2022, 11:38am UTC](https://discuss.elastic.co/t/stringify-object-during-reindexing/295900/6 "2022-02-01T11:38:30Z")

</div>

Below is a painless jsonify sample, but I don't recommend to use such script **reinventing the wheel**. I have no idea about other way to do it purely in Elasticsearch.

The easier way is use some client (eg. python). Retrive documents, jsonify using library and update documents with new jsonified text field.

# Paramters

```auto
{
  "_source":{
    "my_object":{
      "parent":{
        "child":100,
        "childA":1.27,
        "childB": "2022-01-01T050505:000Z",
        "childC": [{
          "A":"B",
          "B":"C"
        },{
          "A":"B",
          "B":"C"
        }]
      }
    }
  }
}

```

# Script

```auto
String jsonify(def object){
    if (object instanceof Map) {
        String out = "{";
        List keyList = new ArrayList(object.keySet());
        Collections.sort(keyList);
        for (int i=0; i< keyList.length; i++){
            String key = keyList[i];
            out = out + "\"" + key + "\"";
            if (object[key] instanceof String){
                out = out + ": \"" + object[key] + "\""
            } else {
                out = out + ":" + jsonify(object[key])
            }
            if (i< keyList.length-1){
                out = out+","
            }
        }
        out = out + "}";
        return out
    } else if (object instanceof ArrayList) {
        String out = "[";
        for (int i = 0; i< object.length; i++){
            out = out + jsonify(object[i]);
            if (i< object.length-1){
                out = out + ","
            }
        }
        out = out + "]";
        return out
    } else {
        if (object instanceof String){
            return "\"" + object + "\""
        } else {
            return object.toString()
        }
    }
}
jsonify(params._source)

```

# Output

```auto
{"my_object":{"parent":{"child":100,"childA":1.27,"childB": "2022-01-01T050505:000Z","childC":[{"A": "B","B": "C"},{"A": "B","B": "C"}]}}}

```

---

<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:** [March 1, 2022, 11:39am UTC](https://discuss.elastic.co/t/stringify-object-during-reindexing/295900/7 "2022-03-01T11:39:00Z")

</div>

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