Elasticsearch(rails)嵌套聚合,如何在映射中聚合深度嵌套的字段

时间:2018-12-17 11:16:04

标签: ruby-on-rails elasticsearch ruby-on-rails-5


我陷入了弹性搜索中的深度聚合查询。我想学习一些高级筛选器/聚合。 所以我有很多变种的产品模型。每个变体都有许多variant_options,每个variant_option有一个选项。
产品模型方法的定义如下(GITLAB - LINK TO MODEL):

mapping do
    indexes :id, type: 'keyword'
    indexes :name, type: 'text'
    indexes :slug, type: 'text'
    indexes :created_at, type: 'date'
    indexes :updated_at, type: 'date'
    indexes :variants, type: 'nested' do
      indexes :id, type: 'keyword'
      indexes :sku, type: 'text'
      indexes :price, type: 'float'
      indexes :sale_price, type: 'float'
      indexes :sale_date_start, type: 'date'
      indexes :sale_date_end, type: 'date'
      indexes :quantity, type: 'integer'

      indexes :variant_options, type: 'nested' do
        indexes :id, type: 'keyword'
        indexes :value, type: 'keyword'

        indexes :option, type: 'nested' do
          indexes :id, type: 'keyword'
          indexes :name, type: 'keyword'
        end
      end

      indexes :variant_properties, type: 'nested' do
        indexes :id, type: 'keyword'
        indexes :value, type: 'text'

        indexes :property, type: 'nested' do
          indexes :id, type: 'keyword'
          indexes :name, type: 'keyword'
        end
      end

    end
  end

  def as_indexed_json(options = {})
    as_json(only: ['id', 'name', 'slug', 'created_at', 'updated_at'],
            include: {
                variants: {
                  only: ['id', 'sku', 'price', 'sale_price', 'sale_date_start', 'sale_date_end', 'quantity'],
                  include: {
                      variant_properties: {
                          only: ['id', 'value'],
                          include: {
                            property: {
                                only: :name
                            }
                          }
                      },
                      variant_options: {
                          only: ['id', 'value'],
                          include: {
                              option: {
                                  only: :name
                              }
                          }
                      }
                  }
                }
            })
  end

这是在Elasticsearch中索引的示例产品(如来自kibana的json):

{
  "_index": "products",
  "_type": "_doc",
  "_id": "28fae61a-d2d9-4d98-a029-84980f1eb9ee",
  "_version": 1,
  "_score": null,
  "_source": {
    "id": "28fae61a-d2d9-4d98-a029-84980f1eb9ee",
    "name": "Male Krzeslo",
    "slug": "male-krzeslo",
    "created_at": "2018-12-17T09:39:16.812Z",
    "updated_at": "2018-12-17T09:39:16.812Z",
    "variants": [
      {
        "id": "e6b18ecb-87c3-4c74-b0ca-b89a69fa61b5",
        "sku": "SNSKU2",
        "price": "202.0",
        "sale_price": null,
        "sale_date_start": "2018-12-16T09:39:16.807Z",
        "sale_date_end": "2019-01-17T09:39:16.807Z",
        "quantity": 7,
        "variant_properties": [
          {
            "id": "00cfa185-356f-40ee-a92a-d10e817e7831",
            "value": "red",
            "property": {
              "name": "color"
            }
          },
          {
            "id": "ee24f306-af2a-4593-9ad9-ab2d839bf882",
            "value": "custom",
            "property": {
              "name": "size"
            }
          }
        ],
        "variant_options": [
          {
            "id": "a9341132-ff7b-4e46-96b7-dae5c4a2de66",
            "value": "xl",
            "option": {
              "name": "size"
            }
          },
          {
            "id": "df85173a-eb8d-4438-9554-9c5cabc99394",
            "value": "red",
            "option": {
              "name": "color"
            }
          }
        ]
      },
      {
        "id": "b392d371-9957-4fb3-9aaf-9c0708d9e1e5",
        "sku": "SNSKU2",
        "price": "202.0",
        "sale_price": null,
        "sale_date_start": "2018-12-16T09:39:16.807Z",
        "sale_date_end": "2019-01-17T09:39:16.807Z",
        "quantity": 7,
        "variant_properties": [
          {
            "id": "afc3d3c7-77ad-4454-b115-e485790d736c",
            "value": "red",
            "property": {
              "name": "size"
            }
          },
          {
            "id": "5f0a6fcc-8ee8-4567-a4df-a893cad731f2",
            "value": "custom",
            "property": {
              "name": "size"
            }
          }
        ],
        "variant_options": [
          {
            "id": "35031bb5-9e3d-45b1-b9d1-b2e616fbb12c",
            "value": "red",
            "option": {
              "name": "color"
            }
          },
          {
            "id": "03939236-bca3-47ca-ac47-a789d88c1998",
            "value": "s",
            "option": {
              "name": "size"
            }
          }
        ]
      }
    ]
  },
  "fields": {
    "updated_at": [
      "2018-12-17T09:39:16.812Z"
    ],
    "created_at": [
      "2018-12-17T09:39:16.812Z"
    ]
  },
  "sort": [
    1545039556812
  ]
}

我可以对变体中的字段执行一些基本的聚合查询 for example

当我想通过深度嵌套的字段进行聚合时,查询不返回任何内容。我不知道它是否与映射错误有关,否则我将需要额外的查询,因为嵌套对象位于数组中。 example of wrong query

我将需要它进行汇总,例如按variant_property值的数量进行汇总,以将其显示为过滤器。 我试图为每个嵌套级别添加到查询嵌套路径,这没有帮助。 如果有人可以解释我的错误,我将不胜感激。
干杯:)
ps。对不起,我的英语不是我的母语languange

0 个答案:

没有答案