# Return field value from array of objects

**URL:** <https://discuss.elastic.co/t/return-field-value-from-array-of-objects/122704>\
**Category:** Elasticsearch\
**Created:** [March 6, 2018, 11:22am UTC](https://discuss.elastic.co/t/return-field-value-from-array-of-objects/122704 "2018-03-06T11:22:37Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![yishain11](https://avatars.discourse-cdn.com/v4/letter/y/df705f/32.png) [@yishain11](https://discuss.elastic.co/u/yishain11)\
**Post date:** [March 6, 2018, 11:22am UTC](https://discuss.elastic.co/t/return-field-value-from-array-of-objects/122704/1 "2018-03-06T11:22:37Z")

</div>

I have the following document that is being loaded into elasticseatch 6:

```
{
"DataChangeInfo" : "Archive Log Set archiveLogSet.2,
"documentsList" : [
{
  "commandScn": "25933758",
  "commandCommitScn": "0",
  "commandSequence": "3",
  "commandType": "INSERT",
  "commandTimestamp": "2017-12-07 05:09:54+03:00",
  "objectDBName": "D",
  "objectSchemaName": "CFTNAXDEV",
  "objectId": "NERNAL",
  "changedFieldsList": [
    {
      "fieldId": "PK_NEWJOURNAL",
      "fieldType": "NUMBER",
      "fieldValue": "NULL",
      "fieldChanged": "Y"
    },
    {
      "fieldId": "DESCRIPTION",
      "fieldType": "VARCHAR2",
      "fieldValue": "hello there",
      "fieldChanged": "Y"
    },
    {
      "fieldId": "STATUS",
      "fieldType": "VARCHAR2",
      "fieldValue": "NULL",
      "fieldChanged": "Y"
    },
{
      "fieldId": "REFERENCE",
      "fieldType": "VARCHAR2",
      "fieldValue": "NULL",
      "fieldChanged": "Y"
    },
    {
      "fieldId": "CITY",
      "fieldType": "VARCHAR",
      "fieldValue": "NY",
      "fieldChanged": "Y"
    },
    {
      "fieldId": "ACTIONID2",
      "fieldType": "NUMBER",
      "fieldValue": "NULL",
      "fieldChanged": "Y"
    },
    {
      "fieldId": "LOG_DATE2",
      "fieldType": "TIMESTAMP(6) WITH TIME ZONE",
      "fieldValue": "2017-12-06 22:09:54.941-05:00",
      "fieldChanged": "Y"
    }
  ],
  "conditionFieldsList": []
}
]
}

```

I want to search for a specific field value, when certain fieldId condition is met.  
for example, I want all the cities in all documents. Meaning: I want all fieldValue when fieldId is equal to "CITY".

What kind of mapping to I need to make this an option?  
What kind of query will yeild the result I want?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 6, 2018, 3:13pm UTC](https://discuss.elastic.co/t/return-field-value-from-array-of-objects/122704/2 "2018-03-06T15:13:16Z")

</div>

If you have a limited number of fields like this, you should reconsider your model.  
If you have a lot of possible fields (more than 1000), then may be. But be careful here because you will index all fields as text which is meaningless in some cases like numbers, dates.

To answer your question, you need to define `changedFieldsList` as a `nested` type.

---

<div class="post-metadata">

**Author:** ![yishain11](https://avatars.discourse-cdn.com/v4/letter/y/df705f/32.png) [@yishain11](https://discuss.elastic.co/u/yishain11)\
**Post date:** [March 6, 2018, 3:32pm UTC](https://discuss.elastic.co/t/return-field-value-from-array-of-objects/122704/3 "2018-03-06T15:32:08Z")

</div>

Hi and thanks for the answer.  
Even if I change the type to nested, how will the query look like?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 6, 2018, 4:11pm UTC](https://discuss.elastic.co/t/return-field-value-from-array-of-objects/122704/4 "2018-03-06T16:11:47Z")

</div>

A bool query with 1 filter clause on the `fieldId` and one must clause on the `fieldValue` I guess.

---

<div class="post-metadata">

**Author:** ![yishain11](https://avatars.discourse-cdn.com/v4/letter/y/df705f/32.png) [@yishain11](https://discuss.elastic.co/u/yishain11)\
**Post date:** [March 7, 2018, 9:58am UTC](https://discuss.elastic.co/t/return-field-value-from-array-of-objects/122704/5 "2018-03-07T09:58:40Z")

</div>

Hi, I'm pretty new to elasticsearch queries, can you give me an example in writing of how this looks like?  
Thanks, and sorry for the trouble...

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 7, 2018, 4:20pm UTC](https://discuss.elastic.co/t/return-field-value-from-array-of-objects/122704/6 "2018-03-07T16:20:56Z")

</div>

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

---

<div class="post-metadata">

**Author:** ![yishain11](https://avatars.discourse-cdn.com/v4/letter/y/df705f/32.png) [@yishain11](https://discuss.elastic.co/u/yishain11)\
**Post date:** [March 13, 2018, 7:38am UTC](https://discuss.elastic.co/t/return-field-value-from-array-of-objects/122704/7 "2018-03-13T07:38:01Z")

</div>

> [@dadoonet](#):
>
> recreation script

Providing this is a bit tricky, because accualy I plan on running this queries from kibana dev tools.  
Anyway, can you provide my with example?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 13, 2018, 10:44am UTC](https://discuss.elastic.co/t/return-field-value-from-array-of-objects/122704/8 "2018-03-13T10:44:19Z")

</div>

Here is an example of a bool query: [https://www.elastic.co/guide/en/elasticsearch/reference/6.2/query-dsl-bool-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/query-dsl-bool-query.html)

And in case you need it, an example of a nested query: [https://www.elastic.co/guide/en/elasticsearch/reference/6.2/query-dsl-nested-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/query-dsl-nested-query.html)

---

<div class="post-metadata">

**Author:** ![yishain11](https://avatars.discourse-cdn.com/v4/letter/y/df705f/32.png) [@yishain11](https://discuss.elastic.co/u/yishain11)\
**Post date:** [March 13, 2018, 10:55am UTC](https://discuss.elastic.co/t/return-field-value-from-array-of-objects/122704/9 "2018-03-13T10:55:02Z")

</div>

Thank you so much!  
Cheers from Israel

---

<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:** [April 10, 2018, 10:55am UTC](https://discuss.elastic.co/t/return-field-value-from-array-of-objects/122704/10 "2018-04-10T10:55:07Z")

</div>

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