# How can I append an object in elastic array?

**URL:** <https://discuss.elastic.co/t/how-can-i-append-an-object-in-elastic-array/278845>\
**Category:** Elasticsearch\
**Created:** [July 16, 2021, 2:52am UTC](https://discuss.elastic.co/t/how-can-i-append-an-object-in-elastic-array/278845 "2021-07-16T02:52:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Duong\_Phan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/duong_phan/32/91711_2.png) [@Duong\_Phan](https://discuss.elastic.co/u/Duong_Phan)\
**Post date:** [July 16, 2021, 2:52am UTC](https://discuss.elastic.co/t/how-can-i-append-an-object-in-elastic-array/278845/1 "2021-07-16T02:52:49Z")

</div>

Assume I have an index is defined like this:

```auto
{
    "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

```auto
"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

```auto
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?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [July 16, 2021, 8:41am UTC](https://discuss.elastic.co/t/how-can-i-append-an-object-in-elastic-array/278845/2 "2021-07-16T08:41:40Z")

</div>

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

---

<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 13, 2021, 8:42am UTC](https://discuss.elastic.co/t/how-can-i-append-an-object-in-elastic-array/278845/3 "2021-08-13T08:42:33Z")

</div>

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