1

It is possible to include a "date and time" field in a document that receives elasticsearch without it being previously defined. The date and time corresponds to the one received by the json to elasticsearch

This is the mapping:

{
  "mappings": {
    "properties": {
      "entries":{"type": "nested"
      }
    } 
  }
}

Is it possible that it can be defined in the mapping field so that elasticsearch includes the current date automatically?

2 Answers 2

7

What you can do is to define an ingest pipeline to automatically add a date field when your document are indexed.

First, create a pipeline, like this (_ingest.timestamp is a built-in field that you can access):

PUT _ingest/pipeline/add-current-time
{
  "description" : "automatically add the current time to the documents",
  "processors" : [
    {
      "set" : {
        "field": "@timestamp",
        "value": "{{{_ingest.timestamp}}}"
      }
    }
  ]
}

Then when you index a new document, you need to reference the pipeline, like this:

PUT test-index/_doc/1?pipeline=add-current-time
{
   "my_field": "test"
}

After indexing, the document would look like this:

GET test-index/_doc/1
=>

{
   "@timestamp": "2020-08-12T15:48:00.000Z",
   "my_field": "test"
}

UPDATE:

Since you're using index templates, it's even easier because you can define a default pipeline to be run for each indexed documents.

In your index templates, you need to add this to the index settings:

{
  "order": 1,
  "index_patterns": [
    "attom"
  ],
  "aliases": {},
  "settings": {
    "index": {
      "number_of_shards": "5",
      "number_of_replicas": "1",
      "default_pipeline": "add-current-time"     <--- add this
    }
  },
  ...

Then you can keep indexing documents without referencing the pipeline, it will be automatic.

Sign up to request clarification or add additional context in comments.

4 Comments

Awesome. It could be possible do the same but in a index template? I would like to do the same for the all documents indexing automatically
Thank you very much for your answers, you always find the solution
Hey @val, you need to replace "_ingest.timestamp" with "{{_ingest.timestamp}}"
@vintagexav well spotted, thanks, fixed ;-)
0
"value": "{{{_ingest.timestamp}}}"

Source

3 Comments

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
@BillTür: This is clearly more than just a link; it should not be treated as a link-only answer. That said, it would be useful to offer more context and explanation.
@panda: You are correct. I’ve now fixed the URL on your behalf.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.