弹性搜索组和聚合嵌套值 [英] Elasticsearch group and aggregate nested values

查看:87
本文介绍了弹性搜索组和聚合嵌套值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想收到一个请求数据来构建这样的东西:

 分类:
- 笔记本电脑)
- 配件(50)
- 显示器(10)
- 以上部分很容易 -

实际类别的属性ex。笔记本电脑:
- 卡片恢复:
- MMC(1)
- SD(5)
- 分辨率:
- 1024x768(2)
- 2048x1536 (3)

首先我在我的弹性搜索上进行映射:

  {
mappings:{
product:{
properties:{
name :{
type:string
},
categoryName:{
type:string,
index not_analyzed
},
priceBrutto:{
type:float
},
categoryCode:{
type :$$$$$$$$$$$$$$$ b $ btype:string
},
值:{
index:not_analyzed,
type:string
}
}
}
}
}
}
}

然后我添加对象,如下所示。
productAttributeFields 中将有很多属性。如果笔记本电脑有很多端口,每个端口都是另一个数组,在 productAttributeFields 中。

  Array 

[name] => Macbook Pro
[categoryCode] => 123
[categoryName] =>笔记本
[priceBrutto] => ; $ 1500
[productAttributeFields] => Array

[0] => Array

[name] =>分辨率
[值] => 2048x1536


[1] =>数组

[name] =>内存读取器
[ => MMC

[2] => Array

[name] =>内存读取器
[value] => SD



现在我想得到这样的结果:

 数组

[已] => 132
[timed_out] =>
[_shards] =>数组

[总] => 1
[成功] => 1
[失败] => 0


[hits] => Array

[total] => 631
[max_score] => 0
[hits] => Array





[聚集] => Array

[attrs] => Array

[doc_count_error_upper_bound] => 0
[sum_other_doc_count] => 4608
[ ] => Array

[0] => Array

[key] =>分辨率
[doc_count] => 619
[attrsValues] => Array

[doc_count_error_upper_bound] => 0
[sum_other_doc_count] => 14199
[buckets] => Array

[0] =>数组

[key] => 2048x1536
[doc_count] => 123


[1] =>数组

[key] => 1024x768
[doc_count] => 3








[1] => Array

[key] =>内存读取器
[doc_count] => 618
[wartosci] =>数组

[ doc_count_error_upper_bound] => 0
[sum_other_doc_count] => 14185
[buckets] => Array

[0] => Array
b $ b [key] => MMC
[doc_count] => 431


[1] =>数组

[key] => SD
[doc_count] => 430













/ pre>

我很接近解决问题(在我的查询之下),但是在二级聚合中,我拥有所有的值(例如,在resolution中有 2048x1536 MMC SD )。我想在分辨率2048x15361024x768和其他具有分辨率的值,读卡器MMCSD和具有密钥读卡器的其他值

 'body'=> [
'query'=> [
'match'=> [
categoryCode = 123
],
],
'aggs'=> [
'attrs'=> [
'terms'=> [
'field'=> 'productAttributeFields.name',
],
'aggs'=> [
'attrsValues'=> [
'terms'=> [
'field'=> 'productAttributeFields.value',
'size'=> 100,
],
],
],
],
],
]
pre>

解决方案

您需要更改映射,并使 productAttributeFields a 嵌套字段,以便您可以保留 productAttributeFields.name productAttributeFields.value



映射应如下所示:

  {
mappings:{
product:{
properties:{
name:{
type
},
categoryName:{
type:string,
index:not_analyzed
},
priceBrutto :{
type:float
},
categoryCode:{
type:integer
},
productAttributeFields:{
type:nested,
include_in_parent:true,
properties:{
name:{
index:not_analyzed,
type:string
},
value:{
index:not_analyzed,
type:string
}
}
}
}
}
}
}

查询更改为

  {
查询:{
match:{
categoryCode:123
}
},
aggs:{
attrs_root:{
nested:{
path :productAttributeFields
},
aggs:{
attrs:{
terms:{
field:productAttributeFields.name
},
aggs:{
attrsValues:{
terms:{
field:productAttributeFields.value,
size:100
}
}
}
}
}
}
}
}


I want get in one request data to build something like this:

Categories:
 - laptops (5)
 - accessories (50)
 - monitors (10)
 -- above part is easy --

Attributest for actual category ex. laptops:
 - card reder:
  - MMC (1)
  - SD (5)
 - resolution:
  - 1024x768 (2)
  - 2048x1536 (3)

First I make mapping on my Elasticsearch like this:

{
    "mappings": {
    "product": {
        "properties": {
            "name": {
                "type": "string"
            },
            "categoryName": {
                "type": "string",
               "index": "not_analyzed"
            },
            "priceBrutto": {
                "type": "float"
            },
            "categoryCode": {
                "type": "integer"
            },
            "productAttributeFields" : {
                "properties" : {
                    "name" : {
                        "index" : "not_analyzed",
                        "type" : "string"
                    },
                    "value" : {
                        "index" : "not_analyzed",
                        "type" : "string"
                    }
                }
            }
         }
      }
   }
}

Then I add objects looks like below. In productAttributeFields will be many attributes. If laptop has many ports, every port is another array in productAttributeFields.

Array
(
    [name] => Macbook Pro
    [categoryCode] => 123
    [categoryName] => Notebooks
    [priceBrutto] => 1500
    [productAttributeFields] => Array
        (
            [0] => Array
                (
                    [name] => Resolution
                    [value] => 2048x1536
                )

            [1] => Array
                (
                    [name] => Memory Readers
                    [value] => MMC
                )
            [2] => Array
                (
                    [name] => Memory Readers
                    [value] => SD
                )
        )
)

Now I want get result like this:

Array
(
    [took] => 132
    [timed_out] => 
    [_shards] => Array
        (
            [total] => 1
            [successful] => 1
            [failed] => 0
        )

    [hits] => Array
        (
            [total] => 631
            [max_score] => 0
            [hits] => Array
                (
                )

        )

    [aggregations] => Array
        (
            [attrs] => Array
                (
                    [doc_count_error_upper_bound] => 0
                    [sum_other_doc_count] => 4608
                    [buckets] => Array
                        (
                            [0] => Array
                                (
                                    [key] => Resolution
                                    [doc_count] => 619
                                    [attrsValues] => Array
                                        (
                                            [doc_count_error_upper_bound] => 0
                                            [sum_other_doc_count] => 14199
                                            [buckets] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [key] => 2048x1536
                                                            [doc_count] => 123
                                                        )

                                                    [1] => Array
                                                        (
                                                            [key] => 1024x768
                                                            [doc_count] => 3
                                                        )

                                                )

                                        )

                                )

                            [1] => Array
                                (
                                    [key] => Memory Readers
                                    [doc_count] => 618
                                    [wartosci] => Array
                                        (
                                            [doc_count_error_upper_bound] => 0
                                            [sum_other_doc_count] => 14185
                                            [buckets] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [key] => MMC
                                                            [doc_count] => 431
                                                        )

                                                    [1] => Array
                                                        (
                                                            [key] => SD
                                                            [doc_count] => 430
                                                        )

                                                )

                                        )

                                )

                        )

                )
        )
)

I'm close to solving the problem (below my query), but in second level aggregation I have all of values (ex. in "resolution" I have 2048x1536, MMC and SD). I want to have in "resolution" only "2048x1536", "1024x768" and other values which has key "resolution", on "card readers" only "MMC", "SD" and other values which has key "card readers".

'body' => [
    'query' => [
        'match' => [
            categoryCode = 123
        ],
    ],
    'aggs' => [
        'attrs' => [
            'terms' => [
                'field' => 'productAttributeFields.name',
            ],
            'aggs' => [
                'attrsValues' => [
                    'terms' => [
                        'field' => 'productAttributeFields.value',
                        'size' => 100,
                    ],
                ],
            ],
        ],
    ],
]

解决方案

You need to change your mapping and make productAttributeFields a nested field so that you can retain the association between productAttributeFields.name and productAttributeFields.value.

The mapping should look like this:

{
  "mappings": {
    "product": {
      "properties": {
        "name": {
          "type": "string"
        },
        "categoryName": {
          "type": "string",
          "index": "not_analyzed"
        },
        "priceBrutto": {
          "type": "float"
        },
        "categoryCode": {
          "type": "integer"
        },
        "productAttributeFields": {
          "type": "nested",
          "include_in_parent": true, 
          "properties": {
            "name": {
              "index": "not_analyzed",
              "type": "string"
            },
            "value": {
              "index": "not_analyzed",
              "type": "string"
            }
          }
        }
      }
    }
  }
}

And the query changes to

{
  "query": {
    "match": {
      "categoryCode": 123
    }
  },
  "aggs": {
    "attrs_root": {
      "nested": {
        "path": "productAttributeFields"
      },
      "aggs": {
        "attrs": {
          "terms": {
            "field": "productAttributeFields.name"
          },
          "aggs": {
            "attrsValues": {
              "terms": {
                "field": "productAttributeFields.value",
                "size": 100
              }
            }
          }
        }
      }
    }
  }
}

这篇关于弹性搜索组和聚合嵌套值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆