代码之家  ›  专栏  ›  技术社区  ›  David Ambler

进一步过滤聚合

  •  0
  • David Ambler  · 技术社区  · 7 年前

    {
      "_index": "products",
      "_type": "product",
      "_id": "ID-12345",
      "_score": 1,
      "_source": {
        "created_at": "2017-08-04T17:56:44.592Z",
        "updated_at": "2017-08-04T17:56:44.592Z",
        "product_information": {
          "sku": "12345",
          "name": "Product Name",
          "price": 25,
          "brand": "Brand Name",
          "url": "URL"
        },
        "product_detail": {
          "description": "Product description text here.",
          "string_facets": [
            {
              "facet_name": "Colour",
              "facet_value": "Grey"
            },
            {
              "facet_name": "Category",
              "facet_value": "Linen"
            },
            {
              "facet_name": "Category",
              "facet_value": "Throws & Blanket"
            },
            {
              "facet_name": "Keyword",
              "facet_value": "Contemporary"
            },
            {
              "facet_name": "Keyword",
              "facet_value": "Sophisticated"
            }
          ]
        }
      }
    }
    

    product_detail.string_facets product_detail.string_facets.facet_name .

    这是我目前的查询,它正在返回数据,但并不像我预期的那样。首先是查询(这只是为了尝试获取颜色):

    {
      "from": 0,
      "size": 12,
      "query": {
        "bool": {
          "should": [
            {
              "multi_match": {
                "query": "Rug",
                "fields": ["product_information.name", "product_detail.string_facets.facet_value"]
              }
            },
            {
              "multi_match": {
                "query": "Blue",
                "fields": ["product_information.name", "product_detail.string_facets.facet_name"]
              }
            }
          ],
          "minimum_should_match": "100%"
        }
      },
      "aggs": {
        "suggestions": {
          "filter": { "term": { "product_detail.string_facets.facet_name.keyword": "Colour" }},
          "aggs": {
            "colours": {
              "terms": {
                "field": "product_detail.string_facets.facet_value.keyword",
                "size": 10
              }
            }
          }
        }
      }
    }
    

    "aggregations": {
        "suggestions": {
          "doc_count": 21,
          "colours": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 23,
            "buckets": [
              {
                "key": "Rug",
                "doc_count": 21
              },
              {
                "key": "Blue",
                "doc_count": 18
              },
              {
                "key": "Bold",
                "doc_count": 7
              },
              {
                "key": "Modern",
                "doc_count": 6
              },
              {
                "key": "Multi-Coloured",
                "doc_count": 5
              },
              {
                "key": "Contemporary",
                "doc_count": 4
              },
              {
                "key": "Traditional",
                "doc_count": 4
              },
              {
                "key": "White",
                "doc_count": 4
              },
              {
                "key": "Luxurious",
                "doc_count": 3
              },
              {
                "key": "Minimal",
                "doc_count": 3
              }
            ]
          }
        }
      }
    

    它给了我所有的结果 facet_name facet_type 颜色和我想象的一样。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Joanna    7 年前

    你没有展示地图的样子,但我想 product_detail.string_facets

    {
      "product_detail.string_facets.facet_name": ["Colour", "Category", "Keyword"],
      "product_detail.string_facets.facet_value": ["Grey", "Linen", "Throws & Blanket", "Contemporary", "Sophisticated"]
    }
    

    如您所见,基于这种结构,Elasticsearch不知道如何聚合数据。

    让它工作 product\u detail.string\u镶嵌面 nested .映射 string_facets 应与此类似(注 "type": "nested" ):

    "string_facets": {
        "type": "nested",
        "properties": {
            "facet_name": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "facet_value": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            }
        }
    }
    

    {
        "created_at": "2017-08-04T17:56:44.592Z",
        "updated_at": "2017-08-04T17:56:44.592Z",
        "product_information": {
          "sku": "12345",
          "name": "Rug",
          "price": 25,
          "brand": "Brand Name",
          "url": "URL"
        },
        "product_detail": {
          "description": "Product description text here.",
          "string_facets": [
            {
              "facet_name": "Colour",
              "facet_value": "Blue"
            },
            {
              "facet_name": "Colour",
              "facet_value": "Red"
            },
            {
              "facet_name": "Category",
              "facet_value": "Throws & Blanket"
            },
            {
              "facet_name": "Keyword",
              "facet_value": "Contemporary"
            }
          ]
        }
    }
    

    现在,为了将颜色建议聚合为单独的桶,您可以尝试这个查询(我简化了 bool query

    {
      "from": 0,
      "size": 12,
      "query": {
        "bool": {
          "should": [
            {
              "multi_match": {
                "query": "Rug",
                "fields": ["product_information.name", "product_detail.string_facets.facet_value"]
              }
            }
          ]
        }
      },
      "aggs": {
        "facets": {
            "nested" : {
                "path" : "product_detail.string_facets"
            },
            "aggs": {
                "suggestions": {
                  "filter": { "term": { "product_detail.string_facets.facet_name.keyword": "Colour" }},
                  "aggs": {
                    "colours": {
                      "terms": {
                        "field": "product_detail.string_facets.facet_value.keyword",
                        "size": 10
                      }
                    }
                  }
                }
            }
          }
        }
    }
    

    结果:

    {
        ...,
        "hits": {
        ...
        },
        "aggregations": {
            "facets": {
                "doc_count": 5,
                "suggestions": {
                    "doc_count": 2,
                    "colours": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": "Blue",
                                "doc_count": 1
                            },
                            {
                                "key": "Red",
                                "doc_count": 1
                            }
                        ]
                    }
                }
            }
        }
    }