2014-07-17 9 views
9

मैं elasticsearch के माध्यम से गतिशील क्षेत्रों (विभिन्न दस्तावेजों के लिए अलग) पर कुल करने की कोशिश कर रहा हूँ। दस्तावेज़ निम्नलिखित की तरह कर रहे हैं:elasticsearch में गतिशील क्षेत्रों में कैसे एकत्र करें?

[{ 
    "name": "galaxy note", 
    "price": 123, 
    "attributes": { 
     "type": "phone", 
     "weight": "140gm" 
    } 
},{ 
    "name": "shirt", 
    "price": 123, 
    "attributes": { 
     "type": "clothing", 
     "size": "m" 
    } 
}] 

आप विशेषताओं दस्तावेज़ों में बदल देख सकते हैं। क्या मैं हासिल करने की कोशिश इन विशेषताओं के क्षेत्र एकत्र करने के लिए है, इसलिए की तरह है:

{ 
    aggregations: { 
     types: { 
      buckets: [{key: 'phone', count: 123}, {key: 'clothing', count: 12}] 
     } 
    } 
} 

मैं इस लक्ष्य को हासिल करने के लिए elasticsearch की aggregation सुविधा कोशिश कर रहा हूँ, लेकिन सही तरीका ढूँढने में सक्षम नहीं। क्या समेकन के माध्यम से हासिल करना संभव है? या मुझे facets में देखना शुरू करना चाहिए, ऐसा लगता है कि यह अविकसित प्रतीत होता है।

+0

में अनुरोध आपको लगता है कि किसी भी समाधान मिला देता है? मेरे साथ भी वही दिक्कत है। –

उत्तर

0

सुनिश्चित नहीं है कि यह आपका क्या मतलब है, लेकिन यह बुनियादी एकत्रीकरण कार्यक्षमता के साथ काफी सरल है। सावधान रहें कि मैंने मैपिंग को शामिल नहीं किया है, इसलिए कई शब्दों के प्रकार के साथ आपको डबल परिणाम मिल रहे हैं।

POST /product/atype 
{ 
    "name": "galaxy note", 
    "price": 123, 
    "attributes": { 
     "type": "phone", 
     "weight": "140gm" 
    } 
} 

POST /product/atype 
{ 
    "name": "shirt", 
    "price": 123, 
    "attributes": { 
     "type": "clothing", 
     "size": "m" 
    } 
} 

GET /product/_search?search_type=count 
{ 
    "aggs": { 
    "byType": { 
     "terms": { 
     "field": "attributes.type", 
     "size": 10 
     } 
    } 
    } 
} 
+0

मैं इसे आजमा रहा था, लेकिन समस्या यह है कि गुण निश्चित नहीं हैं (और यह भी तय नहीं किया जा सकता है), क्या यह गुणों की तरह कुछ करना संभव है। *? –

+0

तो आप कह रहे हैं कि कुंजी 'विशेषताएँ' तय नहीं है? या कुंजी 'attributes.type' तय नहीं है? आपके उदाहरण से, सब कुछ मेरे लिए बहुत निश्चित लग रहा है। बिल्कुल ठीक नहीं है क्या? – vaidik

+0

वही यहाँ! इस सवाल का जवाब चाहिए। – Darby

12

आप अपने मानचित्रण में नेस्ट के रूप में विशेषताओं को परिभाषित करने और निश्चित लेआउट के लिए एक विशेषता मान का लेआउट परिवर्तित करने के लिए है { key: DynamicKey, value: DynamicValue }

PUT /catalog 
{ 
    "settings" : { 
    "number_of_shards" : 1 
    }, 
    "mappings" : { 
    "article": { 
     "properties": { 
     "name": { 
      "type" : "string", 
      "index" : "not_analyzed" 
     }, 
     "price": { 
      "type" : "integer" 
     }, 
     "attributes": { 
      "type": "nested", 
      "properties": { 
      "key": { 
       "type": "string" 
      }, 
      "value": { 
       "type": "string" 
      } 
      } 
     } 
     } 
    } 
    } 
} 

आप अनुक्रमित कर सकते हैं की तुलना में इस

POST /catalog/article 
{ 
    "name": "shirt", 
    "price": 123, 
    "attributes": [ 
    { "key": "type", "value": "clothing"}, 
    { "key": "size", "value": "m"} 
    ] 
} 

POST /catalog/article 
{ 
    "name": "galaxy note", 
    "price": 123, 
    "attributes": [ 
    { "key": "type", "value": "phone"}, 
    { "key": "weight", "value": "140gm"} 
    ] 
} 
की तरह अपने लेख

इसके बाद आप नेस्टेड विशेषताओं

पर एकत्र करने में सक्षम हैं
GET /catalog/_search 
{ 
    "query":{ 
    "match_all":{} 
    },  
    "aggs": { 
    "attributes": { 
     "nested": { 
     "path": "attributes" 
     }, 
     "aggs": { 
     "key": { 
      "terms": { 
      "field": "attributes.key" 
      }, 
      "aggs": { 
      "value": { 
       "terms": { 
       "field": "attributes.value" 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 

कौन सा तो आप जानकारी आप थोड़ा अलग रूप

[...] 
"buckets": [ 
    { 
    "key": "type", 
    "doc_count": 2, 
    "value": { 
     "doc_count_error_upper_bound": 0, 
     "sum_other_doc_count": 0, 
     "buckets": [ 
     { 
     "key": "clothing", 
     "doc_count": 1 
     }, { 
     "key": "phone", 
     "doc_count": 1 
     } 
     ] 
    } 
    }, 
[...] 
संबंधित मुद्दे