使用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"
        }
      ]
    }
}

和另一份文件。

我正在尝试按照链接中所述在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
   }
 }

我在这里想念什么?

为什么还要返回文档作为回应?

最佳答案

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

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

我阅读了docs和Facet aggregation has been removed。建议使用Terms aggregation

现在,对于您的问题,您可以选择两种选择:
  • 如果要获取每个值的唯一值: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
            }
        }
    }
    
  • 如果您想获得facet-name和facet-value之间的唯一组合,则可以使用Composite aggregation。如果选择这种方式,则aggs应该如下所示:


  • {
        "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

    关于elasticsearch - elasticsearch面嵌套聚合,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57504862/

    10-15 20:13