Hariharan
Hariharan

Reputation: 881

Filter by count in elasticsearch

Consider the following data

POST programming/languages/1
{
  "name":"java",
  "type":"general_purpose"
}
POST programming/languages/2
{
  "name":"javascript",
  "type":"scripting"
}
POST programming/languages/3
{
  "name":"c",
  "type":"general_purpose"
}

GET programming/languages/_search
{
  "query": {
   "match": {
    "type":"general_purpose"
   }
  }
}

If I need to find the docs which has more than one match of the field type, how do that in elasticsearch ?

Normally using group by with having clause we can achieve this in SQL. Can we achieve this in elasticsearch?

Upvotes: 2

Views: 2689

Answers (1)

Jettro Coenradie
Jettro Coenradie

Reputation: 4733

Hi you can use the top_hits query, it does not suit situation where you expect to find a lot of documents for a term. Try it in your own situation. But I think this query can find what you want.

GET /programming/languages/_search?search_type=count
{
  "aggs": {
    "byTop": {
      "terms": {
        "field": "type",
        "min_doc_count": 2,
        "size": 10
      },
      "aggs": {
        "theTop": {
          "top_hits": {
            "size":5
          }
        }
      }
    }
  }
}

Upvotes: 4

Related Questions