弹性搜索范围方面的count和total_count之间有什么区别?

Luc*_*nti 5 range facet faceted-search elasticsearch

我正在使用范围方面进行搜索:

{
"query": {
    "match_all": {}
},
"facets": {
    "prices": {
        "range": {
            "field": "product_price",
            "ranges": [
                {"from": 0, "to": 200},
                {"from": 200, "to": 400},
                {"from": 400, "to": 600},
                {"from": 600, "to": 800},
                {"from": 800}
            ]
        }
    }
}
}
Run Code Online (Sandbox Code Playgroud)

正如预期的那样,我得到了范围的响应:

[
  {
    "from": 0.0,
    "to": 200.0,
    "count": 0,
    "total_count": 0,
    "total": 0.0,
    "mean": 0.0
  },
  {
    "from": 200.0,
    "to": 400.0,
    "count": 1,
    "min": 399.0,
    "max": 399.0,
    "total_count": 1,
    "total": 399.0,
    "mean": 399.0
  },
  {
    "from": 400.0,
    "to": 600.0,
    "count": 5,
    "min": 499.0,
    "max": 599.0,
    "total_count": 5,
    "total": 2886.0,
    "mean": 577.2
  },
  {
    "from": 600.0,
    "to": 800.0,
    "count": 3,
    "min": 690.0,
    "max": 790.0,
    "total_count": 3,
    "total": 2179.0,
    "mean": 726.3333333333334
  },
  {
    "from": 800.0,
    "count": 2,
    "min": 899.0,
    "max": 990.0,
    "total_count": 2,
    "total": 1889.0,
    "mean": 944.5
  }
]
Run Code Online (Sandbox Code Playgroud)

在所有回复中count,total_count它们是相同的.有谁知道它们之间有什么区别?我应该使用哪一个?

jav*_*nna 10

非常好的问题!这部分是棘手,因为你看到的大部分时间相同的值,但是......当你使用key_field,并value_field可以计算基于字段和范围的汇总数据(min,max,total_count,totalmean在另一场).例如,您可以计算受欢迎字段的范围,并查看价格字段中的汇总数据,以查看每个受欢迎程度的价格.也许人们喜欢便宜的产品,或许不是?

让我们想象一下你的产品可以有多个价格的,比方说,例如每个国家不同的价格......这是当你有count来自不同total_count.我们来看一个例子吧.

让我们索引一些包含受欢迎字段和价格字段的文档,这些文档可以有多个值:

{
  "popularity": 50,
  "price": [28,30,32]
}
Run Code Online (Sandbox Code Playgroud)

{
    "popularity": 120,
    "price": [50,54]
}
Run Code Online (Sandbox Code Playgroud)

现在让我们运行以下搜索请求,该请求使用流行度字段作为键并使用价格字段作为值来构建范围构面:

{
    "query": {
        "match_all": {}
    },
    "facets": {
        "popularity_prices": {
            "range": {
                "key_field": "popularity",
                "value_field": "price",
                "ranges": [
                    {"to": 100},
                    {"from": 100}
                ]
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是获得的方面:

{
    "popularity_prices": {
      "_type": "range",
      "ranges": [
        {
          "to": 100,
          "count": 1,
          "min": 28,
          "max": 32,
          "total_count": 3,
          "total": 90,
          "mean": 30
        },
        {
          "from": 100,
          "count": 1,
          "min": 50,
          "max": 54,
          "total_count": 2,
          "total": 104,
          "mean": 52
        }
      ]
    }
}
Run Code Online (Sandbox Code Playgroud)

现在它应该更清楚了total_count.它涉及value_field(价格):3个不同的价格值属于第一个范围,但它们来自同一个文件.另一方面count是属于该范围的文档数量.

既然我们也理解了count关于文档而total_count关于字段值,我们会期望与正常范围方面相同的行为,如果字段包含多个值...对吗?不幸的是,目前没有发生,范围方面将仅考虑每个字段的第一个值.不确定这是不是一个bug.因此,counttotal_count总是一样的.