How can I append an object in elastic array?

Assume I have an index is defined like this:

{
    "settings":{
        "number_of_shards":1,
        "number_of_replicas":1
    },
    "mappings":{
        "properties":{
            "name":{
                "type":"text"
            },
            "dates":{
                "type": "object"
            }
        }
    }
}

I want dates as an array object. So date stores in elastic look like this

"name": "app1",
"dates": [
          {
           "start_at": "2021-07-16 09:35:57.913", //something date type
           "end_at": "2023-07-16 09:35:57.913"
          }
]

This is what I did in go to append an date object to dates array

es.Update().
            Index("apps").
            Id(strId).
            Script(elastic.NewScript("if (ctx._source.dates == null) {ctx._source.dates = new ArrayList();} if (!ctx._source.dates.contains(params.date)){ctx._source.dates.add(params.date);}").
                Params(
                    map[string]interface{}{
                        "date": struct {
                            start_at time.Time
                            end_at   time.Time
                        }{"date1", "date2"},
                    },
                )).
            Do(context.Background())

Maybe later I need to update it as well.
And Is it possible to search some thing like: start_at < now < end_at in elastic?

hey,

not sure you are referring to searches here or do updates. For searches you can just go with two range queries, one for each field.

--Alex

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