【问题标题】:NEST elastic search: how to return certain fields along with aggregated results?NEST 弹性搜索:如何返回某些字段以及聚合结果?
【发布时间】:2015-12-14 18:24:32
【问题描述】:

我需要在聚合后查询某些字段。

文档结构是:

{
  id: 1,
  type: AA,
  hashValue: "qweqeqwdwwew"
    ...and many more fields
}

我想按“hashValue”进行聚合,这样我就只能得到唯一的 hashValues 并且返回结果也应该具有该类型。我需要有关 NEST 查询的帮助。

当前要聚合的查询是:

var result = esClient.Search < EType > (q => q
.Index(esClient.Index)
.Routing(id.ToString(CultureInfo.InvariantCulture))
.Aggregations(ag => ag
  .Terms("Hash", ee => ee
    .Field(f => f.hashValue)))));

如何将它的返回类型字段与 hashValue 一起扩展?

谢谢。

【问题讨论】:

  • “返回结果也应该有类型”是什么意思?你能提供一个解释预期输出的小例子吗?
  • @bittusarkar:类似于键值对,其中键是“hashValue”,值将包含“类型”字段。
  • 还是不清楚。您希望输出是每个 Hash 字段值每个 Type 字段值的文档计数,还是您希望每个字段有两个单独的值列表?
  • 我希望输出包含“散列”值和该散列的“类型”。例如:{"hashkey1": ["type1"], "haskey2": ["type1", "type2"], "haskey3": ["type4"], "hashkey4": ["type6"]}跨度>

标签: c# elasticsearch nest elasticsearch-net


【解决方案1】:

从您的 cmets 看来,您似乎希望按哈希值聚合每种类型的文档。为此,您需要的 Nest 查询如下:

var result = esClient.Search<EType>(q => q
    .Index(esClient.Index)
    .Routing(id.ToString(CultureInfo.InvariantCulture)
    .Aggregations(agHash => agHash
        .Terms("Hash", eeHash => eeHash
            .Field(fHash => fHash.hashValue)
        .Aggregations(agType => agType
            .Terms("Types", eeType => eeType
                .Field(fType => fType.typeValue))))));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 2019-11-08
    • 2016-07-28
    相关资源
    最近更新 更多