2017-08-04 36 views
0

我有一个关于弹性搜索聚集的问题。我有类似下面的文档:聚集的进一步过滤

{ 
    "_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定义为每个string_facet类型的单独的桶。

这是查询我在它返回数据的那一刻,而不是如我所料。首先查询(这只是尝试,并得到颜色):

{ 
    "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颜色,我认为会。

任何帮助将不胜感激。 Elasticsearch看起来非常强大,但文档相当令人生畏!

回答

0

你没有说明映射是怎么样的,但我想product_detail.string_facets字段只是一个内部对象字段,这就是你得到这种结果的原因。通过这种类型的映射,Elasticsearch将数组变为一个简单的字段名称和值列表。在你的情况下,它变成了:

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

正如你所看到的,在此基础上的结构,Elasticsearch不知道如何汇总数据。

要使其工作product_detail.string_facets字段应该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 
         } 
        ] 
       } 
      } 
     } 
    } 
} 
+0

感谢这么多,确实帮助! –