Search at specific index of array of objects

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?

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

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

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.

{
    "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"
      }
    ]
}

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?

1 Like

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.