# Is it possible to create a single document with an array of objects?

**URL:** <https://discuss.elastic.co/t/is-it-possible-to-create-a-single-document-with-an-array-of-objects/310616>\
**Category:** Logstash\
**Created:** [July 26, 2022, 9:40am UTC](https://discuss.elastic.co/t/is-it-possible-to-create-a-single-document-with-an-array-of-objects/310616 "2022-07-26T09:40:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wellington\_Bezerra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wellington_bezerra/32/106676_2.png) [@Wellington\_Bezerra](https://discuss.elastic.co/u/Wellington_Bezerra)\
**Post date:** [July 26, 2022, 9:40am UTC](https://discuss.elastic.co/t/is-it-possible-to-create-a-single-document-with-an-array-of-objects/310616/1 "2022-07-26T09:40:51Z")

</div>

I'm creating a query to feed my index that has a field in common and I wanted to include all values ​​with the same field in a single document.

Example SQL DATA:  
ID NAME CODE ACTIVE QTD PROD  
181304447641860 VALUE1 43111 0 2 JUICE  
181304447641860 VALUE2 43111 0 2 POTATO  
000000007641860 VALUE3 43111 0 2 GRAPE  
000000007641860 VALUE4 43111 0 2 APPLE

I'm trying to do this:  
\</\>[{  
"\_id": "181304447641860",  
"\_score": 1,  
"\_source": [  
[  
{  
"NAME": "VALUE1",  
"CODE": 43111,  
"ACTIVE": 0,  
"QTD": 2,  
"PROD": "JUICE"  
},  
{  
"NAME": "VALUE2",  
"CODE": 43111,  
"ACTIVE": 0,  
"QTD": 2,  
"PROD": "POTATO"  
}  
]  
]  
},  
{  
"\_id": "000000007641860",  
"\_score": 1,  
"\_source": [  
[  
{  
"NAME": "VALUE3",  
"CODE": 43111,  
"ACTIVE": 0,  
"QTD": 2,  
"PROD": "GRAPE"  
},  
{  
"NAME": "VALUE4",  
"CODE": 43111,  
"ACTIVE": 0,  
"QTD": 2,  
"PROD": "APPLE"  
}  
]  
]  
}]\</\>

I tried to use HashMap() to create the array of objects, but the values ​​weren't being included. Any documentation or example suggestions I can use?

---

<div class="post-metadata">

**Author:** ![Ofir\_Edi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ofir_edi/32/78181_2.png) [@Ofir\_Edi](https://discuss.elastic.co/u/Ofir_Edi)\
**Post date:** [July 28, 2022, 3:09pm UTC](https://discuss.elastic.co/t/is-it-possible-to-create-a-single-document-with-an-array-of-objects/310616/2 "2022-07-28T15:09:05Z")

</div>

Hi @Wellington_Bezerra ,  
If i understand you correctly you are getting multiple separated events from SQL DB and you want to merge events with the same id under the same document in Elasticsearch.  
In order to do so you should include `document_id` in elasticsearch output in logstash as well as including a script that should run when document should be updated.  
hereby is an example of such output:

```auto
elasticsearch {
        hosts => ["yourhost:9200"]
        cacert => "path to cacert if required"
        index => "<your index name>"
        doc_as_upsert => "true"
        action => "update"
        script_type => "indexed"
        script_lang => ""
        script => "<script name on elasticsearch>"
        document_id => "%{[ID]}"
        user => "user"
        password => "pass"
    }

```

- notice that action is update and doc\_as\_upsert is set to true indicating that if ID does not exist on Elasticsearch index it should be created (without the script).
- you should create stored script as indicated here: [Create or update stored script API | Elasticsearch Guide [8.3] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/create-stored-script-api.html) and use script\_type: indexed, script\_lang: "" and script with script name to refer every update action to the relevant logic.
- In your script you can refer the indexed data on Elasticsearch using `ctx._source` and the new event from logstash as `params.event.get('any field name from event')`
- source data can't be an array as you posted and must be an object. assuimg you are going to call the array `records` - you should manipulate each record in Logstash to look like the following:

```auto
{
  "records": [
    {
         "NAME": "<name value>",
         "CODE": <code value>,
          "ACTIVE": <active value>,
          "QTD": <qtd>,
         "PROD": "<prod name>"
    }
  ],
  "ID": <the record id>
}

```

- then in your script you can do something like:  
`ctx.source.records.addAll(params.event.get('records'));`

- Also i would suggest to index records as type nested for more appropriate quering capabilities.

---

<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 25, 2022, 3:09pm UTC](https://discuss.elastic.co/t/is-it-possible-to-create-a-single-document-with-an-array-of-objects/310616/3 "2022-08-25T15:09:32Z")

</div>

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