Elasticsearch NEST在keyvalue对

时间:2018-03-26 18:37:05

标签: elasticsearch nest

我在keyvalue对上使用聚合时遇到了一些困难。这甚至可能吗?或者我是否必须重写结构以获得更具体的命名字段?

给出以下对象

[ElasticsearchType(IdProperty = "ProductIDRemote")]
public class Product
{
    public string UrlID { get; set; }
    public string ProductIDRemote { get; set; }

    public DateTime Created { get; set; }
    public DateTime Modified { get; set; }
    public string ProductName { get; set; }
    public string ProductDescription { get; set; }
    public List<ProductShop> Shops { get; set; }
    public List<FeatureGroup> FeatureGroups { get; set; }
    public List<Feature> Properties { get; set; }
    public string DefaultImage { get; set; }
    public List<Image> Images { get; set; }
    public List<Document> Documents { get; set; }
    public List<String> EANs { get; set; }
    public string Supplier { get; set; }

    public List<string> Labels { get; set; }

    public DateTime? EndDate { get; set; }
    public List<String> Categories { get; set; } = new List<string>();
}


public class ProductShop
{
    public string ShopName { get; set; }
    public decimal? Price { get; set; }
    public decimal? PriceFrom { get; set; }
    public decimal ShippingCosts { get; set; }
    public string DeliveryTime { get; set; }
    public string LinkABC { get; set; }
    public string LinkCP { get; set; }
    //public string Stock { get;set }
}

public class FeatureGroup
{
    public string Name { get; set; }
    public List<Feature> Features { get; set; }
}
public class Feature
{
    public string Name { get; set; }
    public string Value { get; set; }
}

我可以使用以下查询获取不同的商店。

var response = client.Search<Product>
    (s => s
    .Query(
        q =>
        q.Bool(
            b => b.Must(
                m => m.Term(t => t.Field(f => f.Labels).Value(testlabel))
                )
            )
     )
     .Size(100)
     .Aggregations(a => a

           .Terms("shops", st => st.Field(f => f.Shops.First().ShopName.Suffix("keyword")).Size(100).Order(o => o.CountDescending()))

           .Terms("categories", st => st.Field(f => f.Categories.First().Suffix("keyword")))
           .Terms("brands",
                   b => b.Field(f => f.Properties.Where(p => p.Name == "brand").First().Value.Suffix("keyword")))
           .Terms("featuregroups", fg => fg.Field(f => f.FeatureGroups.First().Suffix("keyword"))
                .Aggregations(sa =>
                    sa.Terms("feature", ft => ft.Field(f => f.FeatureGroups.First().Features.First().Name.Suffix("keyword")))))

         )
    );
return response;

但我仍然坚持通过类似品牌的键来进行物业过滤的混合。

如果成功,我将尝试获取按功能组分组的功能。

根据要求提供示例产品

{
  "took": 18,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 13.978779,
    "hits": [
      {
        "_index": "catalog_nl",
        "_type": "product",
        "_id": "0001",
        "_score": 13.978779,
        "_source": {
          "productIDRemote": "0001",
          "created": "0001-01-01T00:00:00",
          "modified": "0001-01-01T00:00:00",
          "productName": "Dummy product 001",
          "productDescription": "tralalalal",
          "featureGroups": [
            {
              "name": "Dimensions",
              "features": [
                {
                  "name": "Length",
                  "value": "1m"
                },
                {
                  "name": "Width",
                  "value": "1m"
                },
                {
                  "name": "Heigth",
                  "value": "2m"
                }
              ]
            },
            {
              "name": "Dummy",
              "features": [
                {
                  "name": "Length",
                  "value": "10m"
                },
                {
                  "name": "Width",
                  "value": "10m"
                },
                {
                  "name": "Heigth",
                  "value": "1m"
                }
              ]
            }
          ],
          "properties": [
            {
              "name": "brand",
              "value": "brandname1"
            },
            {
              "name": "property2",
              "value": "property 2 value"
            }
          ],
          "categories": []
        }
      }
    ]
  }
}

我想要做的是获取名称字段为&#39;品牌&#39;的属性的聚合。 。我不确定这是不是正确的方法。或者定义品牌字段并在该字段上汇总

由于我导入了许多不同的属性,因此键值对更容易,而不是定义多个字段。

我还想要在功能组和功能上进行嵌套聚合,而不是按特定功能名称进行过滤。 所以给定一组产品,它们具有哪些功能。这些功能按功能组分组

Dimension
    length
    width
    height

修正了第1部分!

通过以下json,我获得了品牌。小记。我必须使用嵌套聚合,所以我必须将properties属性更改为[Nested]

POST catalog_nl/product/_search
{
    "from": 0,
    "size": 12,

    "aggs": {
      "nested_tags": {
         "nested": {
            "path": "properties"
         },
         "aggs": {
            "properties": {
               "filter": {
                  "term": {
                     "properties.name": "brand"
                  }
               },
               "aggs": {
                  "tags_name_terms": {
                     "terms": {
                        "field": "properties.name.keyword"
                     }
                  },
                  "tags_value_terms": {
                     "terms": {
                        "field": "properties.value.keyword"
                     }
                  }
               }
            }
         }
      }
   }    


}

返回以下内容:

{
  "took": 771,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 668858,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "nested_tags": {
      "doc_count": 1016645,
      "properties": {
        "doc_count": 397242,
        "tags_name_terms": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 0,
          "buckets": [
            {
              "key": "brand",
              "doc_count": 397242
            }
          ]
        },
        "tags_value_terms": {
          "doc_count_error_upper_bound": 1393,
          "sum_other_doc_count": 358172,
          "buckets": [
            {
              "key": "Noppies",
              "doc_count": 6288
            },
            {
              "key": "name it",
              "doc_count": 5474
            },
            {
              "key": "Tumble 'N Dry",
              "doc_count": 4881
            },
            {
              "key": "Blue Seven",
              "doc_count": 4588
            },
            {
              "key": "Merkloos",
              "doc_count": 3746
            },
            {
              "key": "Dirkje",
              "doc_count": 2915
            },
            {
              "key": "NIKE",
              "doc_count": 2873
            },
            {
              "key": "Disney",
              "doc_count": 2796
            },
            {
              "key": "ADIDAS",
              "doc_count": 2772
            },
            {
              "key": "Difuzed",
              "doc_count": 2737
            }
          ]
        }
      }
    }
  }
}

现在找出c#代码来获取此请求。

0 个答案:

没有答案