2017-08-04 8 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フィールド内のカテゴリやキーワードなどの製品情報を格納しています。私は集計のためにこれをColor/Material/Category/Keywordの提案を得るために使うが、別々のバケットとして使いたいと思う。つまり、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 
      } 
     } 
     } 
    } 
    } 
} 

これは私のような出力を与えている以下の:それはむしろ私にfacet_typeのそれらをすべてfacet_nameの結果を与えている

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

私が思ったように色。

ご協力いただければ幸いです。 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である必要があります。これは("type": "nested"に注意してください)にstring_facetsのマッピングは類似しているはず:

"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

本当に助けてくれてありがとう! –