简体   繁体   中英

Elasticsearch Dynamic Aggregations with NEST

Hi there I have the following mapping for product in elastic I am trying to create aggregations from the Name / Value data in product specifications I think what i need to achieve is with Nested aggregations but im struggling with the implementation

  "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"
        }
      }
    },

I have now changed the code below and I am getting some success

.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))
                    )))

by using this code i am now returning the Name values

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

                }


            }

What i cant figure out is how to then get the sub aggregation values

Right after much experimenting and editing Ive managed to get to the bottom of this

First up I Modified productSpecification back to nested and then used the following in the aggregation

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

Then used the following to get the values.

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

All seems to be working fine hopefully this helps some people, on to my next challenge of boosting fields and filtering on said aggregations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM