2014-09-30 94 views
5

是否可以從聚合查詢中排除文檔?我只需要知道「數」和「總和」,不需要點擊。我這樣做是這樣的:如何僅返回ElasticSearch查詢中的聚合統計信息?

{ 
    "query": { 
    "match_all": { 

    } 
    }, 
    "aggs": { 
    "my_agg": { 
     "stats": { 
     "field": "country_id" 
     } 
    } 
    } 
} 
+0

可能[執行彈性搜索聚合而不返回匹配數組](http://stackoverflow.com/questions/27467835/perform-elasticsearch-aggregation-without-returning-hits-array) – 2016-12-11 10:03:18

回答

11

添加到您的查詢?search_type=count。 例如:

GET /my_index/countries/_search?search_type=count 
{ 
"query": { 
    "match_all": { 

    } 
    }, 
    "aggs": { 
    "my_agg": { 
     "stats": { 
     "field": "country_id" 
     } 
    } 
    } 
} 
+2

「在2.0.0-beta1中棄用:count不提供大於0的query_then_fetch的任何好處。「 [鏈接](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-search-type.html#count) – yurez 2016-09-07 11:06:06

7

只注重用MATCH_ALL查詢,你可以簡單地使用「大小」聚集:0(指定要無查詢結果),沒有查詢:

curl -XPOST "http://localhost:9200/indexname/doctype/_search" -d' 
{ 
    "size": 0, 
    "aggs": { 
     "my_agg": { 
      "stats": { 
       "field": "country_id" 
      } 
     } 
    } 
}' 
+0

實際上,安德烈斯特凡的答案是更好的(來源:http:///www.elastic.co/guide/en/elasticsearch/reference/1.x/search-aggregations.html#_returning_only_aggregation_results)。但是,不需要添加match_all查詢。 – 2015-05-08 19:58:07

相關問題