簡體   English   中英

彈性搜索 - 產品選項的聚合過濾器

[英]Elastic search - aggregation filter for product options

我有一個產品目錄,其中每個產品的索引如下(從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,但是這些允許我為彈性搜索創建原始查詢。

這種聚合的先決條件是將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僅出現一次。 例如,在titleMaterialoption下只能存在一個嵌套文檔。

查詢聚合:

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
                }
              ]
            }
          }
        ]
      }
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM