# It is that possible to I return in my Elasticsearch query a new field , that does not exist in the mapping . With a new format from other field?

**URL:** <https://discuss.elastic.co/t/it-is-that-possible-to-i-return-in-my-elasticsearch-query-a-new-field-that-does-not-exist-in-the-mapping-with-a-new-format-from-other-field/339616>\
**Category:** Elasticsearch\
**Created:** [July 30, 2023, 1:20pm UTC](https://discuss.elastic.co/t/it-is-that-possible-to-i-return-in-my-elasticsearch-query-a-new-field-that-does-not-exist-in-the-mapping-with-a-new-format-from-other-field/339616 "2023-07-30T13:20:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Murilo\_Livorato](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/murilo_livorato/32/123848_2.png) [@Murilo\_Livorato](https://discuss.elastic.co/u/Murilo_Livorato)\
**Post date:** [July 30, 2023, 1:20pm UTC](https://discuss.elastic.co/t/it-is-that-possible-to-i-return-in-my-elasticsearch-query-a-new-field-that-does-not-exist-in-the-mapping-with-a-new-format-from-other-field/339616/1 "2023-07-30T13:20:57Z")

</div>

It is that possible to I return in my Elasticsearch query a new field ( that does not exist in the mapping ), with a new format from other field ?  
something like that -

This is my Mapping -

```auto
PUT /user-product-2023-06-02
{
  "mappings": {
      "properties": {
        "count_product_add": {
          "type": "float"
        },
        "timestamp": {
          "type": "date",
          "format": "yyyy-MM-dd"
        }
      }
    }
}

```

If I make a search it will returns -

```auto
  {
        "_index": "user-product-2023-06-02",
        "_id": "epSBjYkB3MOuw-Z1lEDX",
        "_score": 2,
        "_source": {
          "timestamp": "2023-06-03",
          "count_product_add": 2,
        }
      },
      {
        "_index": "user-product-2023-06-02",
        "_id": "e5SBjYkB3MOuw-Z1lEDX",
        "_score": 2,
        "_source": {
         "timestamp": "2023-07-05",
          "count_product_add": 5,
        }
      },

```

I wold like to return a new field with the day and month , and get those values from the time stamp . something like that -

```auto
  {
        "_index": "user-product-2023-06-02",
        "_id": "epSBjYkB3MOuw-Z1lEDX",
        "_score": 2,
        "_source": {
          "timestamp": "2023-06-03",
          "day": "03",
          "month": "06",
          "count_product_add": 2,
        }
      },
      {
        "_index": "user-product-2023-06-02",
        "_id": "e5SBjYkB3MOuw-Z1lEDX",
        "_score": 2,
        "_source": {
         "timestamp": "2023-06-05",
         "day": "06",
          "month": "05",
          "count_product_add": 5,
        }
      },

```

It is that possible ?

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [July 30, 2023, 2:46pm UTC](https://discuss.elastic.co/t/it-is-that-possible-to-i-return-in-my-elasticsearch-query-a-new-field-that-does-not-exist-in-the-mapping-with-a-new-format-from-other-field/339616/2 "2023-07-30T14:46:45Z")

</div>

Hi @Murilo_Livorato maybe [runtime fields](https://www.elastic.co/guide/en/elasticsearch/reference/8.9/runtime-search-request.html) help you.

---

<div class="post-metadata">

**Author:** ![Murilo\_Livorato](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/murilo_livorato/32/123848_2.png) [@Murilo\_Livorato](https://discuss.elastic.co/u/Murilo_Livorato)\
**Post date:** [July 30, 2023, 6:03pm UTC](https://discuss.elastic.co/t/it-is-that-possible-to-i-return-in-my-elasticsearch-query-a-new-field-that-does-not-exist-in-the-mapping-with-a-new-format-from-other-field/339616/3 "2023-07-30T18:03:11Z")

</div>

hello @RabBit_BR , I saw those example, and I think that is the way .

I saw this example -

> **[Explore your data with runtime fields | Elasticsearch Guide \[8.9\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/8.9/runtime-examples.html)**

but I still dont know how I could do it , I dont know JAVA , and those functior are createded in java function .

```auto
GET /user-product-2023-06-02/_search
 {
   "size": 100,
   "runtime_mappings": {
    "date.month": {
      "type": (I DONT KNOW WHAT GOES HERE)) ,
      "script": """
        String date= (I DONT KNOW WHAT GOES HERE).extract(doc["timestamp"].value)?.date;
        if (date != null) emit(date);
      """
    }
  },
   "query": {
    "match_all": {}
  },
  "fields" : ["date.month"]
}

```

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [July 31, 2023, 1:28pm UTC](https://discuss.elastic.co/t/it-is-that-possible-to-i-return-in-my-elasticsearch-query-a-new-field-that-does-not-exist-in-the-mapping-with-a-new-format-from-other-field/339616/4 "2023-07-31T13:28:52Z")

</div>

try this:

```auto
{
  "runtime_mappings": {
    "date.month": {
      "type": "long",
      "script": {
        "source": "emit(doc['timestamp'].value.getMonthValue())"
      }
    },
    "date.day": {
      "type": "long",
      "script": {
        "source": "emit(doc['timestamp'].value.getDayOfMonth())"
      }
    }
  },
  "fields": [
    "date.month", "date.day"
  ], 
  "query": {
    "match_all": {}
  }
}

```

A more performative option is to index the day and month fields. You can even use a pipeline for each indexing the fields are created.

---

<div class="post-metadata">

**Author:** ![Murilo\_Livorato](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/murilo_livorato/32/123848_2.png) [@Murilo\_Livorato](https://discuss.elastic.co/u/Murilo_Livorato)\
**Post date:** [July 31, 2023, 6:46pm UTC](https://discuss.elastic.co/t/it-is-that-possible-to-i-return-in-my-elasticsearch-query-a-new-field-that-does-not-exist-in-the-mapping-with-a-new-format-from-other-field/339616/5 "2023-07-31T18:46:41Z")

</div>

Thanks

I solved like that -

```auto
  "script": {
          "source": "emit(params['_source']['timestamp'].substring(8,10))"
        }

```

---

<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:** [August 28, 2023, 6:47pm UTC](https://discuss.elastic.co/t/it-is-that-possible-to-i-return-in-my-elasticsearch-query-a-new-field-that-does-not-exist-in-the-mapping-with-a-new-format-from-other-field/339616/6 "2023-08-28T18:47:29Z")

</div>

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