# Search at specific index of array of objects

**URL:** <https://discuss.elastic.co/t/search-at-specific-index-of-array-of-objects/365753>\
**Category:** Elasticsearch\
**Created:** [August 29, 2024, 10:10am UTC](https://discuss.elastic.co/t/search-at-specific-index-of-array-of-objects/365753 "2024-08-29T10:10:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ducky1](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@ducky1](https://discuss.elastic.co/u/ducky1)\
**Post date:** [August 29, 2024, 10:10am UTC](https://discuss.elastic.co/t/search-at-specific-index-of-array-of-objects/365753/1 "2024-08-29T10:10:57Z")

</div>

Hello,

I'm new to Elasticsearch and I'm exploring ways to search for a specific index within an array of objects.

For instance, I've indexed the following document, but I want to perform a query that matches the `authors` array only if the elements are in a specific order.

I understand that Elasticsearch treats arrays as multi-value fields, and I've attempted to compare the order of authors using a script, but I'd prefer to avoid using one.

Is there another approach to achieve this?

```auto
{
    "id": "b-8M7a24GHPj8L2Y_zBKJk8Tv",
    "title": "The Great Adventure - The Final Journey",
    "authors": [
      {
        "name": "J.K. Rowling"
      },
      {
        "name": "George R.R. Martin"
      }
    ]
}

```

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [August 29, 2024, 10:30am UTC](https://discuss.elastic.co/t/search-at-specific-index-of-array-of-objects/365753/2 "2024-08-29T10:30:17Z")

</div>

As far as I know the only way to do this would be through a script.

---

<div class="post-metadata">

**Author:** ![ducky1](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@ducky1](https://discuss.elastic.co/u/ducky1)\
**Post date:** [August 29, 2024, 11:54am UTC](https://discuss.elastic.co/t/search-at-specific-index-of-array-of-objects/365753/3 "2024-08-29T11:54:31Z")

</div>

I was also considering setting up an ingest pipeline to extract each author into separate fields, such as author\_1 and author\_2, and then creating a query to match these specific fields.

```auto
{
    "id": "b-8M7a24GHPj8L2Y_zBKJk8Tv",
    "title": "The Great Adventure - The Final Journey",
    "author_1": "J.K. Rowling", 
    "author_2": "George R.R. Martin",
    "authors": [
      {
        "name": "J.K. Rowling"
      },
      {
        "name": "George R.R. Martin"
      }
    ]
}

```

---

<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:** [August 29, 2024, 12:03pm UTC](https://discuss.elastic.co/t/search-at-specific-index-of-array-of-objects/365753/4 "2024-08-29T12:03:43Z")

</div>

Or adding a order field within the author object?  
And then using nested field type for the array.

Also you could prefix the author name by "01-", "02-"... but it really depends on the use case. What do you want to do then? What type of result are you looking for?

---

<div class="post-metadata">

**Author:** ![ducky1](https://avatars.discourse-cdn.com/v4/letter/d/c77e96/32.png) [@ducky1](https://discuss.elastic.co/u/ducky1)\
**Post date:** [August 29, 2024, 12:12pm UTC](https://discuss.elastic.co/t/search-at-specific-index-of-array-of-objects/365753/5 "2024-08-29T12:12:18Z")

</div>

My use case involves indexing the document and receiving a request from an external source containing similar, if not identical, information.

I need to ensure that a match is found, and one of my key requirements is that the order of the authors in the request must be the same as in the document.

I think the order field you mentioned inside the author object would also be a viable solution.
