我是ElasticSearch的新手,目前使用的是v6.2,在尝试向查询中添加一些聚合时似乎遇到了问题。我试图理解各种类型的聚合,以及存储数据的最佳方法。
当查询运行时,我有一些希望聚合的变量属性,然后作为过滤器返回给用户。例如,一个字符可能具有“大小”、“形状”和“颜色”的属性,而另一个字符只有“形状”和“颜色”的属性。
属性的完整列表是未知的,所以我不认为我能够以这种方式构造查询。
我的数据目前的结构如下:
{
id : 1,
title : 'New Character 1',
group : 1,
region : 1,
attrs : [
moves : 2,
# These would be dynamic, would only apply to some rows, not others.
var_colours : ['Blue', Green', 'Red'],
var_shapes : ['Round', 'Square', 'Etc'],
effects : [
{ id : 1, value: 20},
{ id : 2, value: 60},
{ id : 3, value: 10},
]
]
}我目前有一个组和区域的聚合,看起来像这样。它似乎工作得很好,我想为属性添加一些类似的东西。
[
'aggs' => [
'group_ids' => [
'terms' => [
'field' => 'group',
'order' => [ '_count' => 'desc' ]
]
],
'region_ids' => [
'terms' => [
'field' => 'region',
'order' => [ '_count' => 'desc' ]
]
]
]
]我希望得到如下所示的结果。我也不确定数据结构是否以最好的方式设置,如果需要,我可以在那里进行更改。
[aggregations] => [
[groups] => [
[doc_count_error_upper_bound] => 0
[sum_other_doc_count] => 0
[buckets] => [
[0] => [
[key] => 5
[doc_count] => 27
],
[1] => [
[key] => 2
[doc_count] => 7
]
]
],
[var_colours] => [
[doc_count_error_upper_bound] => 0
[sum_other_doc_count] => 0
[buckets] => [
[0] => [
[key] => 'Red'
[doc_count] => 27
],
[1] => [
[key] => 'Blue'
[doc_count] => 7
]
]
],
[var_shapes] => [
[doc_count_error_upper_bound] => 0
[sum_other_doc_count] => 0
[buckets] => [
[0] => [
[key] => 'Round'
[doc_count] => 27
],
[1] => [
[key] => 'Polygon'
[doc_count] => 7
]
]
]
// ...
]任何人都可以提供的任何见解都将非常感谢。
发布于 2019-10-24 08:27:46
您应该在PHP脚本中执行此操作。
我可以想到以下几点:
默认情况下,当在文档中找到以前看不到的字段时,Elasticsearch会将新字段添加到类型映射中。通过将dynamic参数设置为false (忽略新字段)或strict (如果遇到未知字段,则抛出异常),可以在文档和对象级别禁用此行为。
Advantage:
Disadvantages:
我希望这对你有帮助!:D
https://stackoverflow.com/questions/58528993
复制相似问题