Search code examples
scalaelasticsearchelastic4s

How to make elastic4s store _timestamp field?


I'm able to enable timestamp on a mapping like this :

"someType" as (
   "someField" typed StringType
) timestamp true

But to be able to retrieve it when searching using "fields": ["_timestamp"] it also needs to have store attribute set to true. But if I do this :

"someType" as (
   "someField" typed StringType,
   "_timestamp" typed LongType/DateType store true
) timestamp true

Then it is not returned by _search:

GET /myIndex/someType/_search 
{
  "fields": ["_timestamp"],
  "query" : {
     "match_all" : {}
    }
}

The resulting mapping looks like this :

"someType": {
    "dynamic": "dynamic",
    "_timestamp": {
       "enabled": true
    },
    "properties": {
        "_timestamp": {
          "store": "yes",
          "type": "long"
       }
    }

}

But I've got a feeling that it should be like this :

"someType": {
    "dynamic": "dynamic",
    "_timestamp": {
       "enabled": true,
       "store": true
    },
    "properties": {
        "_timestamp": {
          "store": "yes",
          "type": "long"
       }
    }
    }

Which cannot be done using elastic4s Dsl because it doesn't have a special handling for fields named _timestamp so that the field goes to properties instead of fields in that mapping...


Solution

  • Elastic4s version 1.5.7 allows you to set the timestamp like this:

    create index("myindex") mappings(
      mapping name "foo" timestamp {
        timestamp enabled true format "qwerty" store true path "somepath"
      }
    )
    

    Path, format, and store are optional.