elasticsearch 切面嵌套聚合

elasticsearch facet nested aggregation

使用 elasticsearch 7.0.0.

我正在关注这个link

我有一个索引 test_products,其中 mapping:

{
"settings": {
    "number_of_shards": 1
},
"mappings": {
    "dynamic_templates": [
        {
            "search_result_data": {
                "mapping": {
                    "type": "keyword"
                },
                "path_match": "search_result_data.*"
            }
        }
        ],
    "properties": {
        "search_data": {
            "type": "nested",
            "properties": {
                "full_text": {
                    "type": "text"
                },
                "string_facet": {
                    "type": "nested",
                    "properties": {
                        "facet-name": {
                            "type": "keyword"
                        },
                        "facet-value": {
                            "type": "keyword"
                        }
                    }
                }
            }
        }
    }
}
}

并插入了以下格式的文档:

    {
  "search_result_data": {
    "sku": "wheel-6075-90092",
    "gtin": null,
    "name": "Matte Black Wheel Fuel Ripper",
    "preview_image": "abc.jg",    
    "url": "9836817354546538796",
    "brand": "Fuel Off-Road"
  },
  "search_data": 
    {
      "full_text": "Matte Black Wheel Fuel Ripper",
      "string_facet": [
        {
          "facet-name": "category",
          "facet-value": "Motor Vehicle Rims & Wheels"
        },
        {
          "facet-name": "brand",
          "facet-value": "Fuel Off-Road"
        }
      ]
    }    
}

和另外一份文件..

我正在尝试按照 link 中提到的 string_facet 进行汇总。

"aggregations": {


"agg_string_facet": {
    "nested": {
      "path": "string_facet"
    },
    "aggregations": {
      "facet_name": {
        "terms": {
          "field": "string_facet.facet-name"
        },
        "aggregations": {
          "facet_value": {
            "terms": {
              "field": "string_facet.facet-value"
            }
          }
        }
      }
    }
  }
}

但是我得到了所有(两个)文档返回:

"aggregations": {
    "agg_string_facet": {
      "doc_count": 0
   }
 }

我在这里错过了什么?

另外,为什么将文档作为响应返回?

文档作为响应返回,因为它们与您的查询匹配。如果您希望它们消失,您可以将 "size" 字段设置为 0。默认情况下,它设置为 10。

query{
...
}, 
"size" = 0

我阅读了文档并 Facet aggregation has been removed. The recommendation is to use the Terms aggregation

现在,对于您的问题,您可以选择两个选项:

  1. 如果您想获取每个方面的唯一值:facet-value 和 facet-name,您可以执行以下操作:

"aggs":{
    "unique facet-values":{
        "terms":{
            "field": "facet-value.keyword",
            "size": 30 #By default is 10, maximum recommended is 10,000
        }
    },
    "unique facet-names":{
        "terms":{
            "field": "facet-name.keyword"
            "size": 30 #By default is 10, maximum recommended is 10,000
        }
    }
}
  1. 如果你想获得 facet-name 和 facet-value 之间的唯一组合,你可以使用 Composite aggregation。如果您选择这种方式,您的聚合应该如下所示:

{
    "aggs":{
       "unique-facetvalue-and-facetname-combination":{
            "composite":{
                "size": 30, #By default is 10, maximum recommended is 10,000. No matter what size you choose, you can paginate.
                "sources":[
                    { 
                         "value":
                             { 
                                 "terms":{ 
                                     "field": "facet-value.keyword" 
                                  } 
                             } 
                    },
                    { 
                         "name":
                             { 
                                 "terms":{ 
                                     "field": "facet-name.keyword" 
                                  } 
                             } 
                    }
                ]
            }
        }
    }
}

使用 Composite 优于 Terms 的优势在于,Composite 允许您使用 After key 对结果进行分页。因此您的集群性能不会受到影响。

希望对您有所帮助! :D