【问题标题】:Elasticsearch Dynamic Aggregations with NEST使用 NEST 进行 Elasticsearch 动态聚合
【发布时间】:2018-01-26 00:53:52
【问题描述】:

您好,我有以下弹性产品映射 我正在尝试从产品规范中的名称/值数据创建聚合我认为我需要实现的是嵌套聚合但我正在努力实现

  "mappings": {
"product": {
  "properties": {
    "productSpecification": {
      "properties": {
        "productSpecificationId": {
          "type": "long"
        },
        "specificationId": {
          "type": "long"
        },
        "productId": {
          "type": "long"
        },
        "name": {
          "fielddata": true,
          "type": "text"
        },
        "value": {
          "fielddata": true,
          "type": "text"
        }
      }
    },
        "name": {
          "type": "text",
          "fields": {
            "keyword": {
              "ignore_above": 256,
              "type": "keyword"
            }
          }
        },
        "value": {
          "type": "text",
          "fields": {
            "keyword": {
              "ignore_above": 256,
              "type": "keyword"
            }
          }
        }
      }
    },
    "description": {
      "type": "text",
      "fields": {
        "keyword": {
          "ignore_above": 256,
          "type": "keyword"
        }
      }
    },
    "reviewRatingCount": {
      "type": "integer"
    },
    "productId": {
      "type": "integer"
    },
    "url": {
      "type": "text",
      "fields": {
        "keyword": {
          "ignore_above": 256,
          "type": "keyword"
        }
      }
    },
    "dispatchTimeInDays": {
      "type": "integer"
    },
    "productCode": {
      "type": "text",
      "fields": {
        "keyword": {
          "ignore_above": 256,
          "type": "keyword"
        }
      }
    },
    "name": {
      "type": "text",
      "fields": {
        "keyword": {
          "ignore_above": 256,
          "type": "keyword"
        }
      }
    },

我现在已经更改了下面的代码,我取得了一些成功

.Aggregations(a => a
            .Terms("level1",t => t
                .Field(f=> f.ProductSpecification.First().Name)
                .Aggregations(snd => snd
                    .Terms("level2", f2 => f2.Field(f3 => f3.ProductSpecification.First().Value))
                    )))

通过使用此代码,我现在返回名称值

 var myagg = response.Aggs.Terms("level1");
            if(response.Aggregations != null)
            {
                rtxAggs.Clear();

                rtxAggs.AppendText(Environment.NewLine);
                foreach(var bucket in myagg.Buckets)
                {
                    rtxAggs.AppendText(bucket.Key);

                }


            }

我想不通的是如何获得子聚合值

【问题讨论】:

  • 当您希望在属于映射为 nested 类型的字段的字段上进行聚合时,需要嵌套聚合。在您的示例中,属于 productSpecification. 的任何字段
  • 嗨@RussCam 我已更新问题以包含我的代码
  • @RussCam 进一步更新提供了一些进展
  • 您可以从每个bucket 获取子聚合,例如bucket.Terms("level2")foreach。你见过elastic.co/guide/en/elasticsearch/client/net-api/current/… 吗?
  • 在 Cast 你看到这个@RussCam 我正在寻找匹配我在多个字段上的查询但想要以不同方式提升字段我读到的所有内容似乎都建议使用 .OnFieldsWithBoost 但最新版本的巢似乎没有支持这个

标签: c# elasticsearch nest


【解决方案1】:

在经过大量的试验和编辑后,我设法弄清了这个问题

首先我将 productSpecification 修改回嵌套,然后在聚合中使用以下内容

.Aggregations(a => a
            .Nested("specifications", n => n
                .Path(p => p.ProductSpecification)
                .Aggregations(aa => aa.Terms("groups", sp => sp.Field(p => p.ProductSpecification.Suffix("name"))
                    .Aggregations(aaa => aaa
                        .Terms("attribute", tt => tt.Field(ff => ff.ProductSpecification.Suffix("value"))))
                        )
                        )
                    )
                )

然后使用以下内容获取值。

var groups = response.Aggs.Nested("specifications").Terms("groups");

                foreach(var bucket in groups.Buckets)
                {
                    rtxAggs.AppendText(bucket.Key);
                    var values = bucket.Terms("attribute");
                    foreach(var valBucket in values.Buckets)
                    {
                        rtxAggs.AppendText(Environment.NewLine);
                        rtxAggs.AppendText("  " + valBucket.Key + "(" + valBucket.DocCount + ")");
                    }
                    rtxAggs.AppendText(Environment.NewLine);
                }

一切似乎都运行良好,希望这可以帮助一些人,迎接我的下一个挑战,即提升字段和过滤所述聚合。

【讨论】:

    猜你喜欢
    • 2017-12-26
    • 1970-01-01
    • 2014-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 2018-10-01
    相关资源
    最近更新 更多