【问题标题】:Aggregation count filter with type具有类型的聚合计数过滤器
【发布时间】:2020-04-26 18:09:18
【问题描述】:

搜索弹性搜索后,我想计算实体中的 ID。但我想计算entities.fk_entity_type = 1 或entities.fk_entity_type = 2。我该怎么做?

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
}

我的弹性搜索输出

{
  "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
        }
      ]
    }
  }
}

我来自弹性搜索的查询。 请帮助我。我需要用实体过滤聚合计数。 fk_entity_type.如何做到这一点?

【问题讨论】:

    标签: php elasticsearch filter kibana aggregation


    【解决方案1】:

    我修复它。我为实体添加新映射。 “类型”:“嵌套”。

    我的过滤器

    GET smartinfo3/_search
    {
      "query": {
        "bool": {
          "filter": {
            "bool": {
              "must": [
                {
                  "term": {
                    "is_original": 1
                  }
                }
              ]
            }
          }
        }
      },
      "sort": [
        {
          "id": {
            "order": "asc"
          }
        }
      ],
      "aggs": {
        "entids": {
          "nested": {
            "path": "entities"
          },
          "aggs": {
            "inner": {
              "filter": {
                "terms": {
                  "entities.fk_entity_type": [
                    1
                  ]
                }
              },
              "aggs": {
                "events.id": {
                  "terms": {
                    "field": "entities.id",
                    "size": 100,
                    "order": {
                      "_count": "desc"
                    }
                  }
                }
              }
            }
          }
        }
      },
      "from": 0,
      "size": 0
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-03
      • 1970-01-01
      • 1970-01-01
      • 2020-09-17
      相关资源
      最近更新 更多