2013-06-21 15 views
9

我使用下面的查詢來查找在博客索引詞「開發商」統計指標和類型匹配......小面對的elasticsearch

http://localhost:9200/blog/_search 
{ 
    "query": { 
    "query_string": { 
     "query": "developer" 
     } 
    } 
} 

查詢返回的user和1次命中3次命中在post類型,我想一個方面來反映這些點擊顯示類似...

搜索結果...
博客文章(1)
用戶(3)

...但我不知道如何結合一個方面與查詢來計數這樣的命中,因爲我發現大多數例子計數字段命中;我嘗試使用_index返回索引點擊,但無法使其正常工作;有沒有類似的東西類似,如_type,計算索引內的文檔類型命中?

回答

11

好了,想通了,顯然有針對面一個_type場,在此基礎上...

http://elasticsearch-users.115913.n3.nabble.com/enabled-quot-index-quot-does-not-allow-me-to-get-facet-values-td1056215.html

查詢

http://localhost:9200/blog/_search 

    { 
     "size" : 0, 
     "query" : { 
     "query_string" : { 
      "query" : "developer"} 
     }, 
     "facets" : { 
     "type" : { 
      "terms" : { "field" : "_type"} 
     } 
     } 
    } 

響應

{ 
    ... 
    "facets": { 
    "type": { 
     "_type": "terms", 
     "missing": 0, 
     "total": 4, 
     "other": 0, 
     "terms": [ 
     { 
      "term": "user", 
      "count": 3 
     }, 
     { 
      "term": "post", 
      "count": 1 
     } 
     ] 
    } 
    } 
}