Compare two nested fields inside one document

I have an index with two nested documents.
Is there any possibility two compare two fields of the same document that are in different nested objects?

This is my sample mapping:

  "mappings": {
    "person": {
      "properties": {
        "id": {
          "type": "keyword"
        },
        "cars": {
          "type": "nested",
          "properties": {
            "code": {
              "type": "keyword"
            }
          }
        },
        "bikes": {
          "type": "nested",
          "properties": {
            "code": {
              "type": "keyword"
            }
          }
        }
      }
    }
  }

These are my test data:

{
  "id": "1",
  "cars": {
    "code": "A"
  },
  "bikes": {
    "code": "A"
  }
}

{
  "id": "2",
  "cars": {
    "code": "A"
  },
  "bikes": {
    "code": "B"
  }
}

{
  "id": "3",
  "cars": [
    {
      "code": "B"
    },
    {
      "code": "A"
    }
  ],
  "bikes": [
    {
      "code": "A"
    },
    {
      "code": "B"
    }
  ]
}

I think I have do execute a query like the following but it doesn't work:

{
  "query": {
    "bool": {
      "filter": {
        "script": {
          "script": "doc['cars.code'].value == doc['bikes.code'].value"
        }
      }
    }
  }
}

When trying it with a nested query it also doesn't work.

Nested documents are stored as separate documents behind the scenes, so I do not believe that is possible.

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