Aggregation count filter with type

After searching Elastic search, I want to count the IDs in entities. But I want to count entities.fk_entity_type = 1 or entities.fk_entity_type = 2. How can I do that?

my query
` GET smartinfo/_search

{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "is_original": 1
              }
            },
            {
              "range": {
                "date": {
                  "gte": "2019-12-01 00:00:00",
                  "lte": "2019-12-31 23:59:59"
                }
              }
            },
            {
              "term": {
                "entities.id": "4683313"
              }
            }
          ]
        }
      }
    }
  },
  "sort": [
    {
      "id": {
        "order": "asc"
      }
    }
  ],
  "aggs": {
    "entids": {
      "terms": {
        "field": "entities.id", 
        "size": 10,
        "order": {
          "_count": "desc"
        }
      }
    }
  },
  "from": 0,
  "size": 1
}`

my output
`

{
  "took" : 20,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "smartinfo",
        "_type" : "_doc",
        "_id" : "1416597",
        "_score" : null,
        "_source" : {
          "id" : 1416597,
          "fk_article_type" : 3,
          "fk_article_source" : 11,
          "is_original" : 1,
          "fk_article_source_type" : 1,
          "sentiment" : 0,
          "title" : "“Azərbaycanda oynamaqdan qürur duyacağıq” - Nihat Özdemir",
          "link" : "https://qafqazinfo.az/news/detail/azerbaycanda-oynamaqdan-qurur-duyacagiq-nihat-ozdemir-271085",
          "date" : "2019-12-01 06:12:17",
          "entities" : [
            {
              "id" : 4683312,
              "fk_entity_type" : 2
            },
            {
              "id" : 4683325,
              "fk_entity_type" : 2
            },
            {
              "id" : 4683314,
              "fk_entity_type" : 2
            },
            {
              "id" : 4683313,
              "fk_entity_type" : 2
            },
            {
              "id" : 4684170,
              "fk_entity_type" : 2
            },
            {
              "id" : 4704179,
              "fk_entity_type" : 1
            },
            {
              "id" : 4689748,
              "fk_entity_type" : 3
            },
            {
              "id" : 4178204,
              "fk_entity_type" : 4
            },
            {
              "id" : 4689840,
              "fk_entity_type" : 3
            },
            {
              "id" : 3985091,
              "fk_entity_type" : 4
            }
          ]
        },
        "sort" : [
          1416597
        ]
      }
    ]
  },
  "aggregations" : {
    "entids" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 140334,
      "buckets" : [
        {
          "key" : 4683313,
          "doc_count" : 10592
        },
        {
          "key" : 4683314,
          "doc_count" : 3240
        },
        {
          "key" : 3970247,
          "doc_count" : 1766
        },
        {
          "key" : 4683425,
          "doc_count" : 1653
        },
        {
          "key" : 4683312,
          "doc_count" : 1516
        },
        {
          "key" : 4683342,
          "doc_count" : 1476
        },
        {
          "key" : 4683602,
          "doc_count" : 1149
        },
        {
          "key" : 4683316,
          "doc_count" : 1137
        },
        {
          "key" : 4683362,
          "doc_count" : 901
        },
        {
          "key" : 4683356,
          "doc_count" : 812
        }
      ]
    }
  }
}`

check out the filters aggregation, that would allow you to aggregate based on filters - this way you could keep your result set as is, but just add the aggregations.

if you also want to reduce the result set, another bool query added to the existing must clauses, that consists of a should query with two clauses, one for each entity type (this would become an or in this context).

if fix it with nested mapping.

"enttype1": {
  "nested": {
    "path": "entities"
  },
  "aggs": {
    "inner": {
      "filter": {
        "terms": {
          "entities.fk_entity_type": [
            1
          ]
        }
      },
      "aggs": {
        "entids": {
          "terms": {
            "field": "entities.id",
            "order": {
              "_count": "desc"
            },
            "size": 10000
          }
        }
      }
    }
  }
}

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