首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >nest ElasticSearch中字典的聚合

nest ElasticSearch中字典的聚合
EN

Stack Overflow用户
提问于 2021-01-20 15:43:05
回答 1查看 302关注 0票数 0

因此,我有一组索引产品,其中包含一个带有一个键的字典和一个值列表,我试图用它构建一个面搜索。然而,我是一个很有弹性的新手。

代码语言:javascript
复制
    Product Product {
    Dictionary<string, List<string>> Properties
    //extra fields removed for simplicity
    }

其中的属性可能是类似于

代码语言:javascript
复制
["Color":["Blue","Yellow","Red"],"Size":["Small","Medium","Large"] 

代码语言:javascript
复制
["Material":["Wood"], "Shape":["Circle","Square"], "Size":["Tiny","Medium","Large","Huge"]

我想写几个聚合,这些聚合将返回键和这些键的值。也就是说,如果要对上面的示例进行索引,第一个聚合将返回一个包含"Color","Size","Material","Shape"的桶

第二个聚合将返回4个桶,每个桶都有每个键的唯一值。即Size:["Tiny","small","medium","large","huge"]

我意识到为此我需要一个嵌套的聚合,但是我的任何尝试都不会带来桶中的任何东西。任何指示都将不胜感激。到目前为止我的情况是这样的。

代码语言:javascript
复制
    var ProductsQuery = client.Search<Product>(s => s
        .Index("products")
        .Query(q => q.MatchAll())
        .Aggregations(a => a
            .Nested("properties", n => n
                .Path(p => p.Properties.Suffix("keyword"))
                    .Aggregations(a => a
                        .Terms("property-keys", t => t
                            .Field(f => f.Properties.Keys.Suffix("keyword"))))));

编辑一些请求的详细信息:

当前的属性映射(它似乎正在为每个键创建一个新的映射,而我不确定这是否是典型的?)我还没有把整个对象映射放在这里,因为它很大。产品有很多领域:

代码语言:javascript
复制
"properties" : {
  "properties" : {
    "Colour" : {
      "type" : "text",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "ignore_above" : 256
        }
      }
    },
    "Equipment" : {
      "type" : "text",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "ignore_above" : 256
        }
      }
    },
    "Football Age" : {
      "type" : "text",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "ignore_above" : 256
        }
      }
    },
    "Football Size" : {
      "type" : "text",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "ignore_above" : 256
        }
      }
    },
    "Frame Weight" : {
      "type" : "text",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "ignore_above" : 256
        }
      }
    },
    "Garment" : {
      "type" : "text",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "ignore_above" : 256
        }
      }
    },
    "Head Shape" : {
      "type" : "text",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "ignore_above" : 256
        }
      }
    },
    "Level" : {
      "type" : "text",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "ignore_above" : 256
        }
      }
    },
    "Product" : {
      "type" : "text",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "ignore_above" : 256
        }
      }
    },
    "Size" : {
      "type" : "text",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "ignore_above" : 256
        }
      }
    },
    "Sport" : {
      "type" : "text",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "ignore_above" : 256
        }
      }
    },
    "Surface" : {
      "type" : "text",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "ignore_above" : 256
        }
      }
    },
    "Type" : {
      "type" : "text",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "ignore_above" : 256
        }
      }
    },
    "Unit" : {
      "type" : "text",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "ignore_above" : 256
        }
      }
    },
    "Weight" : {
      "type" : "text",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "ignore_above" : 256
        }
      }
    },
    "comparer" : {
      "type" : "object"
    },
    "count" : {
      "type" : "integer"
    },
    "item" : {
      "type" : "text",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "ignore_above" : 256
        }
      }
    },
    "keys" : {
      "properties" : {
        "count" : {
          "type" : "integer"
        }
      }
    },
    "values" : {
      "properties" : {
        "count" : {
          "type" : "integer"
        }
      }
    }
  }
}

和一些索引文件

代码语言:javascript
复制
"hits" : [
  {
    "_index" : "products-20-01-2021-13-49-08",
    "_type" : "_doc",
    "_id" : "134550",
    "_score" : 1.0,
    "_source" : {
      "properties" : {
        "Type" : [
          "Sleds"
        ],
        "Product" : [
          "Sleds"
        ],
        "Colour" : [
          "Black"
        ]
      }
    }
  },
  {
    "_index" : "products-20-01-2021-13-49-08",
    "_type" : "_doc",
    "_id" : "134566",
    "_score" : 1.0,
    "_source" : {
      "properties" : {
        "Sport" : [
          "Fitness"
        ],
        "Type" : [
          "Corner",
          "Edge",
          "Middle"
        ],
        "Size" : [
          "10mm",
          "15mm",
          "20mm"
        ],
        "Product" : [
          "Floor Matting"
        ]
      }
    }
  },
  {
    "_index" : "products-20-01-2021-13-49-08",
    "_type" : "_doc",
    "_id" : "134576",
    "_score" : 1.0,
    "_source" : {
      "properties" : {
        "Sport" : [
          "Rugby"
        ],
        "Type" : [
          "Skills Training"
        ],
        "Equipment" : [
          "Rugby Balls"
        ],
        "Size" : [
          "4",
          "5"
        ],
        "Level" : [
          "Skills"
        ]
      }
    }
  },
  {
    "_index" : "products-20-01-2021-13-49-08",
    "_type" : "_doc",
    "_id" : "134579",
    "_score" : 1.0,
    "_source" : {
      "properties" : {
        "Sport" : [
          "Rugby"
        ],
        "Type" : [
          "Match Union"
        ],
        "Equipment" : [
          "Rugby Balls"
        ],
        "Size" : [
          "4",
          "5"
        ],
        "Level" : [
          "Club",
          " School"
        ],
        "Unit" : [
          "12 Pack",
          "Each"
        ],
        "Colour" : [
          "Blue",
          "Red",
          "White"
        ]
      }
    }
  },
  {
    "_index" : "products-20-01-2021-13-49-08",
    "_type" : "_doc",
    "_id" : "134600",
    "_score" : 1.0,
    "_source" : {
      "properties" : {
        "Sport" : [
          "Rugby"
        ],
        "Size" : [
          "Large",
          "Medium",
          "Small",
          "X/Large",
          "X/Small",
          "XX/Small",
          "XXX/Small"
        ],
        "Garment" : [
          "Gloves"
        ],
        "Colour" : [
          "Red"
        ]
      }
    }
  },
  {
    "_index" : "products-20-01-2021-13-49-08",
    "_type" : "_doc",
    "_id" : "134601",
    "_score" : 1.0,
    "_source" : {
      "properties" : {
        "Sport" : [
          "Netball"
        ],
        "Size" : [
          "Large",
          "X/Large",
          "X/Small",
          "XX/Small",
          "XXX/Small"
        ],
        "Garment" : [
          "Gloves"
        ],
        "Colour" : [
          "Red"
        ]
      }
    }
  },
  {
    "_index" : "products-20-01-2021-13-49-08",
    "_type" : "_doc",
    "_id" : "134609",
    "_score" : 1.0,
    "_source" : {
      "properties" : {
        "Sport" : [
          "Netball"
        ],
        "Size" : [
          "Large",
          "Medium",
          "Small",
          "X/Large",
          "X/Small",
          "XXX/Small"
        ],
        "Garment" : [
          "Gloves"
        ],
        "Colour" : [
          "Black",
          "Green"
        ]
      }
    }
  },
  {
    "_index" : "products-20-01-2021-13-49-08",
    "_type" : "_doc",
    "_id" : "134617",
    "_score" : 1.0,
    "_source" : {
      "properties" : {
        "Sport" : [
          "Football"
        ],
        "Type" : [
          "Training"
        ],
        "Football Size" : [
          "2"
        ],
        "Equipment" : [
          "Footballs"
        ],
        "Size" : [
          "4",
          "5"
        ],
        "Unit" : [
          "12 Pack",
          "Each"
        ],
        "Weight" : [
          "290",
          "360"
        ],
        "Surface" : [
          "Grass",
          " Astroturf"
        ],
        "Football Age" : [
          "9-14 years",
          " 14+ years"
        ]
      }
    }
  },
  {
    "_index" : "products-20-01-2021-13-49-08",
    "_type" : "_doc",
    "_id" : "134548",
    "_score" : 1.0,
    "_source" : {
      "properties" : {
        "Type" : [
          "Sleds"
        ],
        "Product" : [
          "Sleds"
        ],
        "Colour" : [
          "Black",
          "Grey"
        ]
      }
    }
  },
  {
    "_index" : "products-20-01-2021-13-49-08",
    "_type" : "_doc",
    "_id" : "134558",
    "_score" : 1.0,
    "_source" : {
      "properties" : {
        "Sport" : [
          "Squash"
        ],
        "Equipment" : [
          "Squash Rackets"
        ],
        "Size" : [
          "27\""
        ],
        "Head Shape" : [
          "Bridged Closed Throat"
        ],
        "Frame Weight" : [
          "Over 160g"
        ]
      }
    }
  }
]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-20 16:44:20

首先,为了获得这些桶,您可以使用Query这样说:

代码语言:javascript
复制
POST products-*/_search
{
  "size": 0,
  "aggs": {
    "by_Colour": {
      "terms": {
        "field": "properties.Colour.keyword"
      }
    },
    "by_Size": {
      "terms": {
        "field": "properties.Size.keyword"
      }
    }
  }
}

然后需要转换成嵌套代码--我相信这里有很多例子。

但是你的观察是正确的-- ES自动创建了很多映射。我建议不要采用当前的数据格式,而是采用以下方法:

代码语言:javascript
复制
{
  "properties": [
    {
      "key": "Type",
      "values": ["Sleds"]
    },
    {
      "key": "Product",
      "values": ["Sleds"]
    },
    {
      "key": "Colour",
      "values": ["Black"]
    }
  ]
}

并制作了propertiesnested

这样,您将查询共享路径properties.keyproperties.values.keyword上的聚合。

请记住,nested字段类型需要它们自己的、实际的nested mapping --如果没有适当的映射,就不能使用嵌套查询。

有关更多上下文,请查看此related questionthis answer

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65812799

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档