2

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?

1 Answer 1

1

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.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.