2015-06-19 125 views
2

基本上,我在這裏要做的是從層級存儲的字符串中獲取第二層下來的類別。問題在於層級的級別不同,一個產品類別可能有六個級別,另一個只有四個級別,否則我會實施預定義的級別。Elasticsearch聚合部分字符串,而不是全部字符串

我有一些產品類別,像這樣:

[ 
    { 
    title: 'product one', 
    categories: [ 
     'clothing/mens/shoes/boots/steel-toe' 
    ] 
    }, 
    { 
    title: 'product two', 
    categories: [ 
     'clothing/womens/tops/sweaters/open-neck' 
    ] 
    }, 
    { 
    title: 'product three', 
    categories: [ 
     'clothing/kids/shoes/sneakers/light-up' 
    ] 
    }, 
    { 
    title: 'product etc.', 
    categories: [ 
     'clothing/baby/bibs/super-hero' 
    ] 
    }, 
    ... more products 
] 

我試圖讓聚集桶像這樣:

buckets: [ 
    { 
    key: 'clothing/mens', 
    ... 
    }, 
    { 
    key: 'clothing/womens', 
    ... 
    }, 
    { 
    key: 'clothing/kids', 
    ... 
    }, 
    { 
    key: 'clothing/baby', 
    ... 
    }, 
] 

我試圖尋找過濾器的前綴,包括和排除根據條款,但我找不到任何有用的東西。請有人指出我正確的方向。

回答

2

您的category字段應該使用自定義分析器進行分析。也許你有與category一些其他的計劃,所以我就只添加用於聚合子字段:

{ 
    "settings": { 
    "analysis": { 
     "filter": { 
     "category_trimming": { 
      "type": "pattern_capture", 
      "preserve_original": false, 
      "patterns": [ 
      "(^\\w+\/\\w+)" 
      ] 
     } 
     }, 
     "analyzer": { 
     "my_analyzer": { 
      "tokenizer": "keyword", 
      "filter": [ 
      "category_trimming", 
      "lowercase" 
      ] 
     } 
     } 
    } 
    }, 
    "mappings": { 
    "test": { 
     "properties": { 
     "category": { 
      "type": "string", 
      "fields": { 
      "just_for_aggregations": { 
       "type": "string", 
       "analyzer": "my_analyzer" 
      } 
      } 
     } 
     } 
    } 
    } 
} 

測試數據:

POST /index/test/_bulk 
{"index":{}} 
{"category": "clothing/womens/tops/sweaters/open-neck"} 
{"index":{}} 
{"category": "clothing/mens/shoes/boots/steel-toe"} 
{"index":{}} 
{"category": "clothing/kids/shoes/sneakers/light-up"} 
{"index":{}} 
{"category": "clothing/baby/bibs/super-hero"} 

查詢本身:

GET /index/test/_search?search_type=count 
{ 
    "aggs": { 
    "by_category": { 
     "terms": { 
     "field": "category.just_for_aggregations", 
     "size": 10 
     } 
    } 
    } 
} 

結果:

"aggregations": { 
     "by_category": { 
     "doc_count_error_upper_bound": 0, 
     "sum_other_doc_count": 0, 
     "buckets": [ 
      { 
       "key": "clothing/baby", 
       "doc_count": 1 
      }, 
      { 
       "key": "clothing/kids", 
       "doc_count": 1 
      }, 
      { 
       "key": "clothing/mens", 
       "doc_count": 1 
      }, 
      { 
       "key": "clothing/womens", 
       "doc_count": 1 
      } 
     ] 
     } 
    } 
+0

我只是在看那些,並認爲可能有一個更簡單的方法,但應該工作。謝謝你,先生! – user1828780

+0

感謝Andrei的回答,第二個想法。它看起來像圖案只會深入兩層。有沒有辦法做到這一點,所以我可以聚合任何層深?你看,在一種情況下,我可能只需要深入'level1/level2',另一種情況下我可能需要'level1/level2/level3'甚至'level1/level2/level3/level4'。 – user1828780

+0

如果您重新閱讀帖子,這是您的要求。 –