Search code examples
ruby-on-railselasticsearchelasticsearch-rails

How to set "search_type" to "count" in elasticsearch-rails?


Here's the query I'd like to get working with elasticsearch-rails. (The query works in Sense). My goal is to return all the buckets for items that have a person whose name begins with the letter B. My first stumbling block is that I can't figure out how to specify that the search_type should be set to count.

GET _search?search_type=count
{    
    "query": {
        "prefix": {
           "person": "B"
        }
   },
    "aggs" : {
        "facets" : {
            "terms" : {
                "field" : "person",
                "size" : 0,
                "order" : { "_term" : "asc" }
            }
        }
    }
}

Solution

  • According to this issue, this doesn't seem supported yet.

    An alternative that works is simply setting size: 0 in your query, like this:

    {    
        "size": 0,                    <--- add this
        "query": {
            "prefix": {
               "person": "B"
            }
       },
        "aggs" : {
            "facets" : {
                "terms" : {
                    "field" : "person",
                    "size" : 0,
                    "order" : { "_term" : "asc" }
                }
            }
        }
    }
    

    It is worth noting, though, that search_type=count is going to be deprecated is now deprecated in ES 2.0 and the recommendation will be to simply set size: 0 in your query as mentioned above. Doing so would make you ES 2.0-compliant... at least for that query, that is :)