使用NEST的Elasticsearch动态聚合

时间:2018-01-23 15:06:43

标签: c# elasticsearch nest

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

  "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);

                }


            }

我无法弄清楚如何获得子聚合值

1 个答案:

答案 0 :(得分:0)

经过大量的实验和编辑后,我设法了解了这个

的底部

首先我修改了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);
                }

所有似乎都工作正常,希望这有助于一些人,我的下一个提升领域和过滤所述聚合的挑战。