ECommerce Shop / Neast属性的弹性搜索存储桶列表

时间:2017-09-01 11:01:24

标签: php elasticsearch

你可以帮帮我吗?我有一个拥有1000多种产品的电子商务网站。每个产品都有一堆选项,如“颜色”,“大小”和其他规格...但我不知道所有的属性。所以我用这个映射定义了一个文档:

"mappings" : { 
   "article" : { 
      "properties": { 
           "options":     {    
                 "type": "nested",
                 "include_in_parent":"true",
                 "properties": {
                      "id": {"type": "string"}, 
                      "name": {"type": "string"},
                       "values": {"type": "string"}
                  }
            }
      }
}   

这是我的查询以获取Bucket列表:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "categorie_id": "f52330ce2669dfab884c2d60468b8466"
          }
        }
      ],
      "must_not": [],
      "should": []
    }
  },
  "from": 0,
  "size": 1,
  "sort": [
    {
      "sorttype": {
        "order": "desc"
      }
    },
    "_score"
  ],
  "aggs": {
    "baked_goods": {
      "nested": {
        "path": "options"
      },
      "aggs": {
        "name": {
          "terms": {
            "field": "id"
          },
          "aggs": {
            "name": {
              "terms": {
                "field": "values"
              }
            }
          }
        }
      }
    }
  }
}

我得到了文件,但是桶的结果是空的......

"aggregations": {
  "baked_goods": {
    "doc_count": 3331,
    "name": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [ ]
    }
  }
}

我想要类似的东西:

"color" => "red" (4)
"color" => "blue" (2)
"size" => "X" (11)

...

你能帮我吗?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。

映射:

"options":     {    
                                "type": "nested",
                                "include_in_parent": true,
                                "properties": {
                                    "name": { "type": "text"  , "analyzer": "whitespace", "fielddata": true},
                                    "values": { "type": "text"  , "analyzer": "whitespace", "fielddata": true}
                                }
                            }

查询:

"aggs": {
                                "facets": {
                                  "nested": {
                                    "path": "options"
                                  },
                                  "aggs": {
                                    "name": {
                                      "terms": {
                                        "field": "options.name"
                                      },
                                      "aggs": {
                                        "name": {
                                          "terms": {
                                            "field": "options.values"
                                          }
                                        }
                                      }
                                    }
                                  }
                                } }