简体   繁体   中英

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" }
            }
        }
    }
}

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM