ruby-on-railselasticsearchelasticsearch-aggregationsearchkick

Elasticsearch get results count of other indices with same query


I have a cluster setup with multiple indicies, generated from a rails server. Each index is a model in my server.

Looking to search a single index, but get counts for the other indices (models) based on my query json. Is this possible?

The query im using is generated from searchkick and quite large so I'm refraining from posting here. I'm trying to figure out if aggs does what I need, something like. I'd like to refrain from doing a 2nd query if possible

POST blog_index/_search
{ 
  "query": { ... }
  "aggs": {
    "results_count": { ... }
  }
}
=> What I would like to receive
{
  "aggregations": {
    "results_count": {
      "blogs": 123,
      "users": 234,
    }
  }
}

Example: If do a { "term": { "type": "funny" } } on blog_index/_search, and there are 3 users with that type, I'd like to get that count as part of my blog_index query


Solution

  • Solution: You can aggregate on field _index.

    "group_by_index": {
      "terms": {
        "field": "_index",
        "size": "30"
      }
    }