[英]Elasticsearch: filter for a substring in the value of a document field?
[英]Elasticsearch filter document group by field
我有一些文件:
{"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}
在SQL中,我可以使用GROUP BY。 我嘗試了條件聚合,但返回的計數卻不同。
"aggs": {
"distinct": {
"terms": {
"field": "district",
"size": 0
}
}
}
感謝您的幫助! :-)
如果您的ElasticSearch版本為1.3或更高版本,則可以使用top_hits類型的子聚合 ,默認情況下,它將為您提供按查詢得分排序的前三個匹配文檔(此處為1,因為您使用match_all查詢)。
您可以將size
參數設置為大於3。
以下數據集和查詢:
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
}
}
]
}
}
}
]
}
}
}
彈性搜索不會提供有關價值的獨特文檔或按唯一價值分組的文檔。 但是有解決方法,如果您使用的是Java客戶端,則可以執行此操作,也可以將其轉換為適合的語言
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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.