1

I want to search an object on only the first element in an array and ignore the rest. For example, for data below if I wanted to only search the model for the first element in the vehicle array.

{
  "drivers" : [
    {
        "last_name" : "McQueen",
        "vehicle" : [
            {
                "make" : "Powell Motors",
                "model" : "Canyonero"
            },
            {
                "make" : "Miller-Meteor",
                "model" : "Ecto-1"
            }
        ]
    },
    {
        "last_name" : "Buzz",
        "vehicle" : [
            {
                "make" : "Powell Motors",
                "model" : "Canyonero"
            },
            {
                "make" : "Miller-Meteor",
                "model" : "Ecto-1"
            }
        ]
    }
}

I know I can do a nested query like this but the problem here is that all the vehicles are searched. I only want the 0th element in the vehicle list to be searched and the rest to be ignored.

{
  "query": {
    "nested": {
      "path": "driver",
      "query": {
        "nested": {
          "path": "driver.vehicle",
          "query": {
            "bool": {
              "must": [
                { "match": { "drivers.vehicle.model": "Canyonero" } }
              ]
            }
          }
        }
      }
    }
  }
}

Ideally I want something like { drivers.vehicle[0].model": "Canyonero" } to search for the first vehicle in the list everytime. Is there any way I can effectively do this?

Meesam
  • 41
  • 4

1 Answers1

0

Check out this answer -- doing this is certainly possible but quite onerous.

I'd instead recommend using

  • ingest pipelines whereby the first vehicle will be extracted to a new root-level field or
  • updating by query where you do the same as above but execute at will as opposed to only upon ingest.
Joe - GMapsBook.com
  • 15,787
  • 4
  • 23
  • 68