user8510613
user8510613

Reputation: 1282

How to diversify the result of top-hits aggregation?

Let's start with a concrete example. I have a document with these fields:

{
  "template": {
    "mappings": {
      "template": {
        "properties": {
          "tid": {
            "type": "long"
          },
          "folder_id": {
            "type": "long"
          },
          "status": {
            "type": "integer"
          },
          "major_num": {
            "type": "integer"
          }
        }
      }
    }
  }
}

I want to aggregate the query result by field folder_id, and for each group divided by folder_id, retrieve the top-N documents' _source detail. So i write query DSL like:

GET /template/template/_search
{
  "size": 0,
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "status": 1
          }
        }
      ]
    }
  }, 
  "aggs": {
    "folder": {
      "terms": {
        "field": "folder_id",
        "size": 10
      },
      "aggs": {
        "top_hit":{
          "top_hits": {
            "size": 5,
            "_source": ["major_num"]
          }
        }
      }
    }
  }
}

However, now comes a requirement that the top hits documents for each folder_id must be diversified on the field major_num. For each folder_id, the top hits documents retrieve by the sub top_hits aggregation under the terms aggregation, must be unique on field major_num, and for each major_num value, return at most 1 document in the sub top hits aggregation result.

top_hits aggregation cannot accept sub-aggregations, so how should i solve the question?

Upvotes: 0

Views: 2190

Answers (1)

Val
Val

Reputation: 217564

Why not simply adding another terms aggregation on the major_num field ?

GET /template/template/_search
{
  "size": 0,
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "status": 1
          }
        }
      ]
    }
  },
  "aggs": {
    "folder": {
      "terms": {
        "field": "folder_id",
        "size": 10
      },
      "aggs": {
        "majornum": {
          "terms": {
            "field": "major_num",
            "size": 10
          },
          "aggs": {
            "top_hit": {
              "top_hits": {
                "size": 1
              }
            }
          }
        }
      }
    }
  }
}

Upvotes: 1

Related Questions