# Compare two nested fields inside one document

**URL:** <https://discuss.elastic.co/t/compare-two-nested-fields-inside-one-document/94903>\
**Category:** Elasticsearch\
**Created:** [July 28, 2017, 5:59am UTC](https://discuss.elastic.co/t/compare-two-nested-fields-inside-one-document/94903 "2017-07-28T05:59:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tokraft](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tokraft/32/14888_2.png) [@tokraft](https://discuss.elastic.co/u/tokraft)\
**Post date:** [July 28, 2017, 5:59am UTC](https://discuss.elastic.co/t/compare-two-nested-fields-inside-one-document/94903/1 "2017-07-28T05:59:08Z")

</div>

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.

---

<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:** [July 30, 2017, 11:40am UTC](https://discuss.elastic.co/t/compare-two-nested-fields-inside-one-document/94903/2 "2017-07-30T11:40:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 27, 2017, 11:40am UTC](https://discuss.elastic.co/t/compare-two-nested-fields-inside-one-document/94903/3 "2017-08-27T11:40:54Z")

</div>

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