# How to query on a specific array index within elasticsearch?

**URL:** <https://discuss.elastic.co/t/how-to-query-on-a-specific-array-index-within-elasticsearch/216557>\
**Category:** Elasticsearch\
**Created:** [January 26, 2020, 4:49am UTC](https://discuss.elastic.co/t/how-to-query-on-a-specific-array-index-within-elasticsearch/216557 "2020-01-26T04:49:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![meallhour](https://avatars.discourse-cdn.com/v4/letter/m/ccd318/32.png) [@meallhour](https://discuss.elastic.co/u/meallhour)\
**Post date:** [January 26, 2020, 4:49am UTC](https://discuss.elastic.co/t/how-to-query-on-a-specific-array-index-within-elasticsearch/216557/1 "2020-01-26T04:49:26Z")

</div>

```
Mapping: abc-data-type/_mapping/field/sData.status

{
    "abc_history": {
        "mappings": {
            "abc-data-type": {
                "sData.status": {
                    "full_name": "sData.status",
                    "mapping": {
                        "status": {
                            "type": "keyword"
                        }
                    }
                }
            }
        }
    }
}

```

My Json Response looks as below:

```
{
  "took": 41,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": null,
    "hits": [
      {
        "_index": "abc_history",
        "_type": "abc-data-type",
        "_id": "5e29cbb7965809fe6cb22a7b",
        "_score": null,
        "_source": {
          "sData": [
            {
              "status": "In Progress"
            },
            {
              "status": "Started"
            },
            {
              "status": "Finished"
            }
          ]
        },
        "sort": [
          1579797431366
        ]
      }
    ]
  }
}

```

My ES query looks as below which is returning the above response.

```
{
  "from": 0, 
  "size": 5, 
  "_source": ["sData.status"],
        "query": {
          "bool": {
            "must":[
              {
                "wildcard": {
                  "server": "*ABC2501*"
                }
              },         
              {
                "wildcard": {
                  "sData.status": "*Finish*"
                }
              }
            ]
          }
        },
        "sort": [
          { "requestDate": {"order": "desc"}}
        ]
}

```

I want to modify the query so that ES performs search in `third element` of `sData` which is `sData[2]`  
I have modified the query as below and performing wildcard search for `sData[2].status` but it is not returning anything.

```
{
  "from": 0, 
  "size": 5, 
  "_source": ["sData.status"],
        "query": {
          "bool": {
            "must":[
              {
                "wildcard": {
                  "server": "*ABC2501*"
                }
              },         
              {
                "wildcard": {
                  "sData[2].status": "*Finish*"
                }
              }
            ]
          }
        },
        "sort": [
          { "requestDate": {"order": "desc"}}
        ]
}
```

---

<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:** [January 27, 2020, 9:56am UTC](https://discuss.elastic.co/t/how-to-query-on-a-specific-array-index-within-elasticsearch/216557/2 "2020-01-27T09:56:51Z")

</div>

Hey,

if you only want to search in the last element of an array, how about storing that value in an additional field named `most_recent_status` and just search in there?

--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:** [February 24, 2020, 9:57am UTC](https://discuss.elastic.co/t/how-to-query-on-a-specific-array-index-within-elasticsearch/216557/3 "2020-02-24T09:57:02Z")

</div>

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