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?

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 -

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 -

  {
        "_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 -

  {
        "_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 ?

Hi @Murilo_Livorato maybe runtime fields help you.

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

I saw this example -

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

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"]
}

try this:

{
  "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.

Thanks

I solved like that -

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

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