使用 min_doc_count=0 的弹性搜索聚合返回与查询结果或点击不相关的所有存储桶

2024-01-19

这是我的查询 -

{
    "from": 0,
    "size": 100,
    "query": {
        "bool": {
            "filter": [
                {
                    "terms": {
                        "folderId.keyword": [
                            "ff98505e-cdff-43aa-8b05-197bc3f3265e"
                        ],
                        "boost": 1
                    }
                },
                {
                    "terms": {
                        "objectType.keyword": [
                            "File"
                        ],
                        "boost": 1
                    }
                },
                {
                    "term": {
                        "tenantId": {
                            "value": "34202",
                            "boost": 1
                        }
                    }
                }
            ],
            "adjust_pure_negative": true,
            "boost": 1
        }
    },
      "aggs":{
        "_byformat":{
            "terms":{
               "field":"format.keyword",
               "min_doc_count":0,
               "size":200
            }
        }
   }
}

min_doc_count = 0 的结果 -


  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "plnesdv1-34202-1",
        "_type" : "_doc",
        "_id" : "6adbda83-53ad-457f-a2ab-d5b04c643005",
        "_score" : 0.0,
        "_source" : {
          "format" : "vnd.openxmlformats-officedocument.spreadsheetml.sheet",
          "externalSharing" : "N",
          "description" : null,
          "dateModified" : null,
          "type" : "application",
          "folderId" : "ff98505e-cdff-43aa-8b05-197bc3f3265e",
          "tags" : [ ],
          "objectType" : "File",
          "dateCreated" : null,
          "name" : "New XLSX file",
          "tenantId" : "34202",
          "modifiedBy" : "rdt001",
          "id" : "6adbda83-53ad-457f-a2ab-d5b04c643005",
          "status" : "active",
          "expirationDate" : null
        }
      },
      {
        "_index" : "plnesdv1-34202-1",
        "_type" : "_doc",
        "_id" : "b1000a15-2d80-41f4-a5df-ba5c27f8e9c6",
        "_score" : 0.0,
        "_source" : {
          "format" : "vnd.ms-excel",
          "externalSharing" : "N",
          "description" : null,
          "dateModified" : null,
          "type" : "application",
          "folderId" : "ff98505e-cdff-43aa-8b05-197bc3f3265e",
          "tags" : [ ],
          "objectType" : "File",
          "dateCreated" : null,
          "name" : "New XLS file",
          "tenantId" : "34202",
          "modifiedBy" : "rdt001",
          "id" : "b1000a15-2d80-41f4-a5df-ba5c27f8e9c6",
          "status" : "active",
          "expirationDate" : null
        }
      },
      {
        "_index" : "plnesdv1-34202-1",
        "_type" : "_doc",
        "_id" : "630e9f49-3368-408d-a091-03f253127004",
        "_score" : 0.0,
        "_source" : {
          "format" : "msword",
          "externalSharing" : "N",
          "description" : null,
          "dateModified" : null,
          "type" : "application",
          "folderId" : "ff98505e-cdff-43aa-8b05-197bc3f3265e",
          "tags" : [ ],
          "objectType" : "File",
          "dateCreated" : null,
          "name" : "New DOC file",
          "tenantId" : "34202",
          "modifiedBy" : "rdt001",
          "id" : "630e9f49-3368-408d-a091-03f253127004",
          "status" : "active",
          "expirationDate" : null
        }
      }
    ]
  },
  "aggregations" : {
    "_byformat" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "msword",
          "doc_count" : 1
        },
        {
          "key" : "vnd.ms-excel",
          "doc_count" : 1
        },
        {
          "key" : "vnd.openxmlformats-officedocument.spreadsheetml.sheet",
          "doc_count" : 1
        },
        {
          "key" : "bmp",
          "doc_count" : 0
        },
        {
          "key" : "gif",
          "doc_count" : 0
        },
        {
          "key" : "html",
          "doc_count" : 0
        }       
      ]
    }
  }
}
  

min_doc_count = 1 的结果 -

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "plnesdv1-34202-1",
        "_type" : "_doc",
        "_id" : "6adbda83-53ad-457f-a2ab-d5b04c643005",
        "_score" : 0.0,
        "_source" : {
          "format" : "vnd.openxmlformats-officedocument.spreadsheetml.sheet",
          "externalSharing" : "N",
          "description" : null,
          "dateModified" : null,
          "type" : "application",
          "folderId" : "ff98505e-cdff-43aa-8b05-197bc3f3265e",
          "tags" : [ ],
          "objectType" : "File",
          "dateCreated" : null,
          "name" : "New XLSX file",
          "tenantId" : "34202",
          "modifiedBy" : "rdt001",
          "id" : "6adbda83-53ad-457f-a2ab-d5b04c643005",
          "status" : "active",
          "expirationDate" : null
        }
      },
      {
        "_index" : "plnesdv1-34202-1",
        "_type" : "_doc",
        "_id" : "b1000a15-2d80-41f4-a5df-ba5c27f8e9c6",
        "_score" : 0.0,
        "_source" : {
          "format" : "vnd.ms-excel",
          "externalSharing" : "N",
          "description" : null,
          "dateModified" : null,
          "type" : "application",
          "folderId" : "ff98505e-cdff-43aa-8b05-197bc3f3265e",
          "tags" : [ ],
          "objectType" : "File",
          "dateCreated" : null,
          "name" : "New XLS file",
          "tenantId" : "34202",
          "modifiedBy" : "rdt001",
          "id" : "b1000a15-2d80-41f4-a5df-ba5c27f8e9c6",
          "status" : "active",
          "expirationDate" : null
        }
      },
      {
        "_index" : "plnesdv1-34202-1",
        "_type" : "_doc",
        "_id" : "630e9f49-3368-408d-a091-03f253127004",
        "_score" : 0.0,
        "_source" : {
          "format" : "msword",
          "externalSharing" : "N",
          "description" : null,
          "dateModified" : null,
          "type" : "application",
          "folderId" : "ff98505e-cdff-43aa-8b05-197bc3f3265e",
          "tags" : [ ],
          "objectType" : "File",
          "dateCreated" : null,
          "name" : "New DOC file",
          "tenantId" : "34202",
          "modifiedBy" : "rdt001",
          "id" : "630e9f49-3368-408d-a091-03f253127004",
          "status" : "active",
          "expirationDate" : null
        }
      }
    ]
  },
  "aggregations" : {
    "_byformat" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "msword",
          "doc_count" : 1
        },
        {
          "key" : "vnd.ms-excel",
          "doc_count" : 1
        },
        {
          "key" : "vnd.openxmlformats-officedocument.spreadsheetml.sheet",
          "doc_count" : 1
        }
      ]
    }
  }
}

当 min_doc_count = 1 且仅获取与命中相关的存储桶时,聚合是正确的。

谁能告诉我为什么当设置 min_doc_count = 0 时聚合会获取所有存储桶。我已经浏览了弹性搜索文档,它指出这种行为是设计使然,任何其他方式都可以仅获取命中且计数为零的聚合存储桶。


首先你要明白看见的意义是什么计数为零的桶.

以下是摘录自术语聚合 https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html link:

设置 min_doc_count=0 还将返回以下术语的存储桶:没有 匹配任何命中。但是,某些返回的条款具有 文档计数零可能只属于已删除的文档或 其他类型的文档,因此不能保证 match_all 查询将找到这些术语的正文档计数。

所以很可能它是已删除文档的计数。

请注意,聚合只会根据查询过滤的文档进行计算。

然而你需要记住,虽然 ES 不断在幕后合并索引段(在删除过程中发生),count 为 0 的结果可能不一致,并且在一段时间内可能最终会一致(如果没有进一步的情况) dos 从那时起被删除)一旦合并过程完成,您可能根本不会获得任何计数为 0 的条款。

因此,在某种程度上,可以安全地对您的业务主管说,他们是业务的重中之重已删除的文档你可以把上面的论点推给他们。如果他们说需要对已删除文档的文档/术语进行计数,就像找到索引中不存在的文档/术语一样,甚至没有任何意义。

至于为什么这仍然显示,这可能是由于 ES 中发生的段合并过程,并且它是设计使然的。

所以不,您不能对已删除的文档应用查询/过滤器(退后一步想象一下),因此您无法控制与首先不可用的文档相关的数据。

聚合查询:

您可以使用以下聚合,它将根据评论中提到的要求为您提供:

POST <your_index_name>/_search
{
  "size": 0,
  "aggs": {
    "myaggs_count_zero": {                       <--- Agg for count 0
      "terms": {
        "field": "format.keyword"
      },
      "aggs": {
        "document_counts": {
          "value_count" : {
            "field" : "format.keyword"
          }
        },
        "by_account_filtered": {
          "bucket_selector": {
            "buckets_path": {
              "totalDocs": "document_counts"
            },
            "script": "params.totalDocs == 0"
          }
        }
      }
    },
    "myaggs_count_not_zero": {                  <--- Agg for normal count
      "terms": {
        "field": "format.keyword",
        "min_doc_count": 1
      }
    }
  }
}

请注意,我已经利用了Terms https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html, 值计数 https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html and 桶选择器聚合 https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-selector-aggregation.html

这可能不是您正在寻找的内容,但我希望对您有所帮助!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 min_doc_count=0 的弹性搜索聚合返回与查询结果或点击不相关的所有存储桶 的相关文章

随机推荐