# Array of objects datatype exists

**URL:** <https://discuss.elastic.co/t/array-of-objects-datatype-exists/91524>\
**Category:** Elasticsearch\
**Created:** [July 1, 2017, 5:36pm UTC](https://discuss.elastic.co/t/array-of-objects-datatype-exists/91524 "2017-07-01T17:36:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![WisdomPill](https://avatars.discourse-cdn.com/v4/letter/w/dfb087/32.png) [@WisdomPill](https://discuss.elastic.co/u/WisdomPill)\
**Post date:** [July 1, 2017, 5:36pm UTC](https://discuss.elastic.co/t/array-of-objects-datatype-exists/91524/1 "2017-07-01T17:36:03Z")

</div>

I have a field called alarm\_images where I store a list of objects with three fields download\_date,  
index and url. I wanted to lighten my mapping since I was using lot of memory for lot of fields and I wanted to disable doc\_values and index for all values since I only have to retrieve the list with the rest of the document every once in a while. My problem is that I have to check if alarm\_images object exists and using a mapping like the following I always get that alarm\_images does not exist while it does.

```
        "alarm_images": {
           "type": "nested",
           "properties": {
              "download_date": {
                 "type": "date",
                 "index": false,
                 "doc_values": false
              },
              "index": {
                 "type": "integer",
                 "index": false,
                 "doc_values": false
              },
              "url": {
                 "type": "keyword",
                 "index": false,
                 "doc_values": false
              }
           }

```

is there any way to use nested object without index and doc\_values and still preserve the capability to check if the field exists?

---

<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:** [July 1, 2017, 5:55pm UTC](https://discuss.elastic.co/t/array-of-objects-datatype-exists/91524/2 "2017-07-01T17:55:15Z")

</div>

I believe that one of the easiest way is to add a boolean value.

You can compute it may be at index time using an ingest script processor.

---

<div class="post-metadata">

**Author:** ![WisdomPill](https://avatars.discourse-cdn.com/v4/letter/w/dfb087/32.png) [@WisdomPill](https://discuss.elastic.co/u/WisdomPill)\
**Post date:** [July 1, 2017, 7:31pm UTC](https://discuss.elastic.co/t/array-of-objects-datatype-exists/91524/3 "2017-07-01T19:31:06Z")

</div>

I am new to ingest node, I've only used it with the basic pipeline plugins. How can I add a field based on a condition?  
I tried using script pipeline but I got stuck adding the new field to the document.  
Should I use another pipeline?

This is what I got so far.

```json
POST _ingest/pipeline/_simulate
{
   "pipeline": {
      "description": "checks if the image field is populated",
      "processors": [
         {
            "script": {
               "lang": "painless",
               "inline": "if(ctx._source?.image != null){ctx._source.has_image = true} else {ctx._source.has_image = false}"
            }
         }
      ]
   },
   "docs": [
      {
         "_index": "sensor-2017-06-30",
         "_type": "status",
         "_id": "id",
         "_source": {
            "image": {
               "url": "image.jpg",
               "download_type": "last",
               "download_date": "2017-06-30T06:33:56.092611+00:00"
            }
         }
      }
   ]
}

```

---

<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:** [July 1, 2017, 7:50pm UTC](https://discuss.elastic.co/t/array-of-objects-datatype-exists/91524/4 "2017-07-01T19:50:14Z")

</div>

Is it a typo?

```
ctx._source?.image != null

```

Should it be

```
ctx._source.image != null
```

---

<div class="post-metadata">

**Author:** ![WisdomPill](https://avatars.discourse-cdn.com/v4/letter/w/dfb087/32.png) [@WisdomPill](https://discuss.elastic.co/u/WisdomPill)\
**Post date:** [July 1, 2017, 8:14pm UTC](https://discuss.elastic.co/t/array-of-objects-datatype-exists/91524/5 "2017-07-01T20:14:05Z")

</div>

I solved my issue! 🙂  
It was not the typo you suggested.  
With the first one

```auto
ctx._source?.image != null

```

I get always false in the condition while with the second

```auto
ctx._source.image != null

```

I get a NullPointerException,  
This is the solution I came with, actually I don't know why it works since I though I had to add \_source after ctx. Thank you for your quick responsiveness. Have a nice day.

```json
POST _ingest/pipeline/_simulate
{
   "pipeline": {
      "description": "checks if image and alarm_images field is populated",
      "processors": [
         {
            "script": {
               "lang": "painless",
               "inline": "if(ctx.image != null){ctx.has_image = true} else {ctx.has_image = false}"
            }
         },
         {
            "script": {
               "lang": "painless",
               "inline": "if(ctx.alarm_images != null){ctx.has_alarm_image = true} else {ctx.has_alarm_image = false}"
            }
         }
      ]
   },
   "docs": [
      {
         "_index": "sensor-2017-06-30",
         "_type": "status",
         "_id": "id",
         "_source": {
            "image": {
               "url": "image.jpg",
               "download_type": "last",
               "download_date": "2017-06-30T06:33:56.092611+00:00"
            }
         }
      },
      {
         "_index": "sensor-2017-06-30",
         "_type": "status",
         "_id": "id",
         "_source": {
            "message": "painless is not painless"
         }
      },
      {
         "_index": "sensor-2017-06-30",
         "_type": "status",
         "_id": "id",
         "_source": {
            "alarm_images": [
               {
                  "url": "alarm_image.jpg",
                  "index": 0,
                  "download_date": "2017-06-30T06:34:07.744397+00:00"
               },
               {
                  "url": "alarm_image.jpg",
                  "index": 1,
                  "download_date": "2017-06-30T06:34:08.851667+00:00"
               }
            ]
         }
      }
   ]
}

```

---

<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:** [July 29, 2017, 8:14pm UTC](https://discuss.elastic.co/t/array-of-objects-datatype-exists/91524/6 "2017-07-29T20:14:33Z")

</div>

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