2014-09-23 10 views
11

से मैं कुछ दस्तावेज है:Elasticsearch फिल्टर दस्तावेज़ समूह क्षेत्र

{"name": "John", "district": 1}, 
{"name": "Mary", "district": 2}, 
{"name": "Nick", "district": 1}, 
{"name": "Bob", "district": 3}, 
{"name": "Kenny", "district": 1} 

मैं फिल्टर कैसे/जिले से अलग दस्तावेज़ों का चयन कर सकते हैं?

{"name": "John", "district": 1}, 
{"name": "Mary", "district": 2}, 
{"name": "Bob", "district": 3} 

एसक्यूएल में, मैं ग्रुप बाय का उपयोग कर सकता हूं। मैंने शर्तों को एकत्रीकरण करने की कोशिश की लेकिन यह केवल गिनती अलग हो गई।

"aggs": { 
    "distinct": { 
    "terms": { 
     "field": "district", 
     "size": 0 
    } 
    } 
} 

आपकी मदद के लिए धन्यवाद! :-)

+0

मेरा उत्तर है अपनी समस्या को हल करें –

उत्तर

29

में रूपांतरित कर सकते हैं अपने ElasticSearch संस्करण 1.3 या इसके बाद के संस्करण है, तो आप ऐसा कर सकते है, तो आप प्रकार top_hits के subaggregation इस्तेमाल कर सकते हैं जो आपको (डिफ़ॉल्ट रूप से) आपके क्वेरी स्कोर पर क्रमबद्ध शीर्ष तीन मिलान दस्तावेज देगा (यहां, 1 जैसा कि आप match_all क्वेरी का उपयोग करते हैं)।

आप एक से अधिक 3.

करने के लिए size पैरामीटर सेट कर सकते हैं निम्नलिखित डाटासेट और क्वेरी:

POST /test/districts/ 
{"name": "John", "district": 1} 

POST /test/districts/ 
{"name": "Mary", "district": 2} 

POST /test/districts/ 
{"name": "Nick", "district": 1} 

POST /test/districts/ 
{"name": "Bob", "district": 3} 

POST test/districts/_search 
{ 
    "size": 0, 
    "aggs":{ 
    "by_district":{ 
     "terms": { 
     "field": "district", 
     "size": 0 
     }, 
     "aggs": { 
     "tops": { 
      "top_hits": { 
      "size": 10 
      } 
     } 
     } 
    } 
    } 
} 

विल उत्पादन दस्तावेजों जिस तरह से आप चाहते हैं:

{ 
    "took": 5, 
    "timed_out": false, 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 4, 
     "max_score": 0, 
     "hits": [] 
    }, 
    "aggregations": { 
     "by_district": { 
     "buckets": [ 
      { 
       "key": 1, 
       "key_as_string": "1", 
       "doc_count": 2, 
       "tops": { 
        "hits": { 
        "total": 2, 
        "max_score": 1, 
        "hits": [ 
         { 
          "_index": "test", 
          "_type": "districts", 
          "_id": "XYHu4I-JQcOfLm3iWjTiOg", 
          "_score": 1, 
          "_source": { 
           "name": "John", 
           "district": 1 
          } 
         }, 
         { 
          "_index": "test", 
          "_type": "districts", 
          "_id": "5dul2XMTRC2IpV_tKRRltA", 
          "_score": 1, 
          "_source": { 
           "name": "Nick", 
           "district": 1 
          } 
         } 
        ] 
        } 
       } 
      }, 
      { 
       "key": 2, 
       "key_as_string": "2", 
       "doc_count": 1, 
       "tops": { 
        "hits": { 
        "total": 1, 
        "max_score": 1, 
        "hits": [ 
         { 
          "_index": "test", 
          "_type": "districts", 
          "_id": "I-9Gd4OYSRuexhP1dCdQ-g", 
          "_score": 1, 
          "_source": { 
           "name": "Mary", 
           "district": 2 
          } 
         } 
        ] 
        } 
       } 
      }, 
      { 
       "key": 3, 
       "key_as_string": "3", 
       "doc_count": 1, 
       "tops": { 
        "hits": { 
        "total": 1, 
        "max_score": 1, 
        "hits": [ 
         { 
          "_index": "test", 
          "_type": "districts", 
          "_id": "bti2y-OUT3q2mBNhhI3xeA", 
          "_score": 1, 
          "_source": { 
           "name": "Bob", 
           "district": 3 
          } 
         } 
        ] 
        } 
       } 
      } 
     ] 
     } 
    } 
} 
+0

बढ़िया, तुम मेरी जिंदगी बचाओ !! – Geany

+0

अरे @ThomasC, किसी भी विचार कैसे भी रिकॉर्ड है कि इस तरह एकत्रित होने की हैं फिल्टर करने के लिए? मैं पहले से ही आधे घंटे की कोशिश कर रहा हूं। धन्यवाद ! – lisak

+0

हाय @ लीसाक! आप top_hits के तहत एकत्रीकरण घोंसला नहीं कर सकते हैं, हालांकि, विपरीत संभव है। एक फिल्टर एकत्रीकरण और घोंसला का उपयोग करने के लिए एक top_hits का उपयोग करने का प्रयास करें। या, आप क्वेरी अनुभाग – ThomasC

2

लोचदार खोज अद्वितीय मूल्य से मूल्य या समूह पर अलग-अलग दस्तावेज़ प्रदान नहीं करती है। लेकिन इस के लिए चारों ओर काम करता है, तो आप जावा ग्राहक का उपयोग कर रहे हैं या अपने उपयुक्त भाषा

SearchResponse response = client.prepareSearch().execute().actionGet(); 
SearchHits hits = response.getHits(); 

Iterator<SearchHit> iterator = hits.iterator(); 
Map<String, SearchHit> distinctObjects = new HashMap<String,SearchHit>(); 
while (iterator.hasNext()) { 
    SearchHit searchHit = (SearchHit) iterator.next(); 
    Map<String, Object> source = searchHit.getSource(); 
    if(source.get("district") != null){ 
     distinctObjects.put(source.get("district").toString(),source); 
    } 

} 
+0

क्या होगा यदि आप पेजिनेशन का उपयोग कर रहे हैं? क्या आपको 8 परिणामों के साथ पेज मिल रहे हैं, अन्य 10 के साथ और दूसरे 7 के साथ यदि आपको प्रति पृष्ठ 10 परिणाम मिलते हैं? –

संबंधित मुद्दे