# ElasticSearch Index Mapping for Dynamic

**URL:** https://discuss.elastic.co/t/elasticsearch-index-mapping-for-dynamic/351325
**Category:** Elasticsearch
**Created:** [January 18, 2024, 7:50am UTC](https://discuss.elastic.co/t/elasticsearch-index-mapping-for-dynamic/351325 "2024-01-18T07:50:37Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Burak\_Karatay](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/burak_karatay/32/123772_2.png) [@Burak\_Karatay](https://discuss.elastic.co/u/Burak_Karatay)
#### Post date: [January 18, 2024, 7:50am UTC](https://discuss.elastic.co/t/elasticsearch-index-mapping-for-dynamic/351325/1 "2024-01-18T07:50:38Z")

</div>

I have Nodejs Application and I want to send request and response to elastic, I have an issue with mapping, so basically my code looks like this:

```auto
static sendLogToElastic = (req, res, next) => {
    const self = this;
    const originalSend = res.send;

    res.send = async function (responseBody) {
      if (typeof responseBody === "object") {
        console.log(responseBody);
        if (req.body?.password) delete req.body.password;
        if (req.body?.passwordConfirmation)
          delete req.body.passwordConfirmation;

        self.esClient
          .index({
            index: process.env.ELASTIC_REQUEST_RESPONSE_INDEX,
            document: {
              user_id: req.user?.id || responseBody?.data?.user?.id,
              date: new Date(),
              request: {
                originalUrl: req.originalUrl,
                params: req.params,
                query: req.query,
                body: req.body,
                clientIp: req.headers["x-client-ip"],
                deviceId: req.headers["x-device-id"],
              },
              response: {
                body: responseBody,
              },
            },
          })
          .then((value) => {
            logger.info(JSON.stringify(value));
          })
          .catch((err) => {
            logger.error(
              `request: ${req.originalUrl}, sendLogToElastic error: ${err}`
            );
          });
      }
      originalSend.apply(res, arguments);
    };
    next();
  };

```

This sends all the requests and responses to the elastic but in the response object, the data field sometimes is an object or a number, or just a string. But Elastic doesn't accept when it's a number or string. I got the following error:  
sendLogToElastic error: ResponseError: document\_parsing\_exception  
Root causes:  
document\_parsing\_exception: [1:213] object mapping for [response.body.data] tried to parse field [data] as object, but found a concrete value

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [January 21, 2024, 7:55pm UTC](https://discuss.elastic.co/t/elasticsearch-index-mapping-for-dynamic/351325/2 "2024-01-21T19:55:54Z")

</div>

> [@Burak\_Karatay](#):
>
> the data field sometimes is an object or a number, or just a string.

Every field in Elasticsearch need to have a consistent mapping, so a field can not sometimes be an object and other times a string or number, as this as per the error you showed results in a mapping conflict. When you use dynamic mapping, the mapping for the field is defined the first time it is encountered and all subsequent documents need to adhere to the mapping.

If you want to index this you will either need to change the structure of the document or map the field as not indexed (which means you can not search on it or anything underneath it).

---

<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 18, 2024, 7:56pm UTC](https://discuss.elastic.co/t/elasticsearch-index-mapping-for-dynamic/351325/3 "2024-02-18T19:56:38Z")

</div>

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