Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facet for counting index and type hits in elasticsearch

I'm using the query below to find the word "developer" in a blog index...

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

The query returns 3 hits on user and 1 hit on post types, and I want a facet to reflect those hits to display something like...

Search Results...
Blogs Posts (1)
Users (3)

...but I'm not sure how to combine a facet with a query to count such hits since most examples I found count field hits; I tried using _index to return index hits, but could not get it to work; is there something similar for types, like _type, to count document type hits within an index?

like image 311
raffian Avatar asked Jun 21 '13 19:06

raffian


1 Answers

Ok, figured it out, apparently there is a _type field for facets, based on this...

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

Query

http://localhost:9200/blog/_search

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

Response

{
  ...
  "facets": {
    "type": {
      "_type": "terms",
      "missing": 0,
      "total": 4,
      "other": 0,
      "terms": [
        {
          "term": "user",
          "count": 3
        },
        {
          "term": "post",
          "count": 1
        }
      ]
    }
  }
}
like image 178
raffian Avatar answered Oct 15 '22 19:10

raffian