gpt4 book ai didi

php - 如何在Elasticsearch 6.x中按动态或未知字段进行聚合

转载 作者:行者123 更新时间:2023-12-03 02:37:46 24 4
gpt4 key购买 nike

我是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
]
]
]

// ...
]

任何人都可以提供的任何见解将不胜感激。

最佳答案

您应该在PHP脚本中执行此操作。

我可以想到以下几点:

  • 使用Dynamic field mapping作为索引。

  • By default, when a previously unseen field is found in a document, Elasticsearch will add the new field to the type mapping. This behaviour can be disabled, both at the document and at the object level, by setting the dynamic parameter to false (to ignore new fields) or to strict (to throw an exception if an unknown field is encountered).


  • 获取索引中的所有现有字段。为此使用Get mapping API
  • 遍历第2步的结果,以便可以获取索引中的所有现有字段。例如,您可以将它们存储在列表(或数组)中。
  • 您可以为列表(或数组)中的每个字段创建一个PHP Elasticsearch terms aggregation。这是:创建一个不带任何术语聚合的空查询或基本查询,并为从步骤3中获得的每个元素添加一个术语。
  • missing field添加到每个术语中,并带有一个空的空字符串(“”)。
  • 就是这样。之后,您将以这样一种方式创建查询,无论您要搜索的是什么索引,您都将获得包含所有现有字段的术语集合。

  • 的优点:
  • 您的条款聚合将与所有现有字段一起动态生成。
  • 对于每个不包含任何字段的文档,将显示一个空字符串。

  • 的缺点:
  • 遍历GET映射API的结果可能会有些令人沮丧(但我相信您)。
  • 您在映射中找到的每个新字段的性能(时间和资源)都会受到影响。

  • 我希望这是有帮助的! :D

    关于php - 如何在Elasticsearch 6.x中按动态或未知字段进行聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58528993/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com