How to get a single count only per document when using nested term aggregation?

Mapping

"work_rights": {
  "type": "nested",
  "properties": {
    "country_tid": {
      "type": "keyword"
    },
    "work_right_type": {
      "properties": {
        "tid": {
          "type": "keyword"
        }
      }
    }
  }
}

Data

Document1
{
  "doc": {
    "work_rights": [
      {
        "country_tid": "11",
        "work_right_type": [
          {
            "tid": "222"
          }
        ]
      }
    ]
  }
}

Document2
{
  "doc": {
    "work_rights": [
      {
        "country_tid": "12",
        "work_right_type": [
          {
            "tid": "222"
          }
        ]
      },
      {
        "country_tid": "13",
        "work_right_type": [
          {
            "tid": "222"
          },
          {
            "tid": "223"
          }
        ]
      }
    ]
  }
}

Aggregation

"aggs": {
  "work_right_tid": {
    "nested": {
      "path": "work_rights"
    },
    "aggs": {
      "work_right_type_tid": {
        "terms": {
          "field": "work_right_tid.work_right_type.tid",
          "size": 10
        }
      }
    }
  }
}

Result

{
  "buckets": [
    {
      "key": "222",
      "doc_count": 3
    },
    {
      "key": "223",
      "doc_count": 1
    }
  ]
}

Question
The result is correct since nested data type will maintain the relationship of country_tid and work_right_type. Thus, key:222 resulted to doc_count:3.

Is there a way to get only one correct result per document while maintaining the data type as nested? (Since it is doable when the data type is set to object)
Example: key:222 resulted to doc_count:2.

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