【问题标题】:Elastic search - aggregation filter for product options弹性搜索 - 产品选项的聚合过滤器
【发布时间】:2021-03-14 12:11:43
【问题描述】:

我有一个产品目录,其中每个产品的索引如下(从http://localhost:9200/products/_doc/1 查询)作为示例:

{
  "_index": "products_20201202145032789",
  "_type": "_doc",
  "_id": "1",
  "_version": 1,
  "_seq_no": 0,
  "_primary_term": 1,
  "found": true,
  "_source": {
    "title": "Roncato Eglo",
    "description": "Amazing LED light made of wood and description continues.",
    "price": 3990,
    "manufacturer": "Eglo",
    "category": [
      "Lights",
      "Indoor lights"
    ],
    "options": [
      {
        "title": "Mount type",
        "value": "E27"
      },
      {
        "title": "Number of bulps",
        "value": "4"
      },
      {
        "title": "Batteries included",
        "value": "true"
      },
      {
        "title": "Ligt temperature",
        "value": "warm"
      },
      {
        "title": "Material",
        "value": "wood"
      },
      {
        "title": "Voltage",
        "value": "230"
      }
    ]
  }
}

每个选项都包含不同的值,所以有很多Mount type值,Light temperature值,Material值等等。

如何创建一个聚合(过滤器),让客户可以在各种安装类型选项之间进行选择:

[ ] E27
[X] E14
[X] GU10
...

或者让他们从显示为复选框的不同材料选项中进行选择:

[X] Wood
[ ] Metal
[ ] Glass
...

创建存储桶后,我可以在前端处理它。我正在努力为这些选项创建不同的存储桶。

我已经成功地为CategoryManufacturer 和其他基本的聚合创建并显示和使用。这些产品选项存储在数据库中的has_many_through 关系中。我正在使用 Rails + searchkick gem,但它们允许我为弹性搜索创建原始查询。

【问题讨论】:

    标签: ruby-on-rails elasticsearch filter aggregation searchkick


    【解决方案1】:

    这种聚合的先决条件是options字段为nested

    示例索引映射:

    PUT test
    {
      "mappings": {
        "properties": {
          "title": {
            "type": "keyword"
          },
          "options": {
            "type": "nested",
            "properties": {
              "title": {
                "type": "keyword"
              },
              "value": {
                "type": "keyword"
              }
            }
          }
        }
      }
    }
    

    示例文档:

    PUT test/_doc/1
    {
      "title": "Roncato Eglo",
      "options": [
        {
          "title": "Mount type",
          "value": "E27"
        },
        {
          "title": "Material",
          "value": "wood"
        }
      ]
    }
    
    PUT test/_doc/2
    {
      "title": "Eglo",
      "options": [
        {
          "title": "Mount type",
          "value": "E27"
        },
        {
          "title": "Material",
          "value": "metal"
        }
      ]
    }
    

    假设:对于给定的文档,option 下的title 仅出现一次。例如在option 下只能存在一个嵌套文档,其中titleMaterial

    聚合查询:

    GET test/_search
    {
      "size": 0, 
      "aggs": {
        "OPTION": {
          "nested": {
            "path": "options"
          },
          "aggs": {
            "TITLE": {
              "terms": {
                "field": "options.title",
                "size": 10
              },
              "aggs": {
                "VALUES": {
                  "terms": {
                    "field": "options.value",
                    "size": 10
                  }
                }
              }
            }
          }
        }
      }
    }
    

    回应:

    {
      "took" : 2,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 2,
          "relation" : "eq"
        },
        "max_score" : null,
        "hits" : [ ]
      },
      "aggregations" : {
        "OPTION" : {
          "doc_count" : 4,
          "TITLE" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "Material",
                "doc_count" : 2,
                "VALUES" : {
                  "doc_count_error_upper_bound" : 0,
                  "sum_other_doc_count" : 0,
                  "buckets" : [
                    {
                      "key" : "metal",
                      "doc_count" : 1
                    },
                    {
                      "key" : "wood",
                      "doc_count" : 1
                    }
                  ]
                }
              },
              {
                "key" : "Mount type",
                "doc_count" : 2,
                "VALUES" : {
                  "doc_count_error_upper_bound" : 0,
                  "sum_other_doc_count" : 0,
                  "buckets" : [
                    {
                      "key" : "E27",
                      "doc_count" : 2
                    }
                  ]
                }
              }
            ]
          }
        }
      }
    }
    

    【讨论】:

    • 非常感谢!
    猜你喜欢
    • 2020-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    相关资源
    最近更新 更多