ElasticSearch Aggs Query


(Rakesh) #1

I have a following elasticsearch query and result. I want something like group by with count(*) =1 in sql statement

{
 "size": 0,
  "aggs": {
    "group_by_RequestID": {
      "terms": {
        "field": "RequestID"
      } 
  }
}
}


{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 12,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "group_by_RequestID": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "20160209 132857.249_5420_1_ATL",
          "doc_count": 2
        },
        {
          "key": "20160209 132857.249_5420_1_DEN1100",
          "doc_count": 2
        },
        {
          "key": "20160209 132857.249_5420_1_LAS",
          "doc_count": 2
        },
        {
          "key": "20160209 132857.249_5420_1_PHX1300",
          "doc_count": 2
        },
        {
          "key": "20160209 132857.249_5420_1_PHX1400",
          "doc_count": 2
        },
        {
          "key": "20160209 132857.249_5420_1_SFO",
          "doc_count": 2
        }
      ]
    }
  }
}

I want my result to be back where "doc_count": 1, can you guide me how to get that result ?


(SK) #2

https://www.elastic.co/guide/en/elasticsearch/reference/1.5/search-aggregations-metrics-top-hits-aggregation.html

Try top hits Query


(Rakesh) #3

Thanks for your reply. As I am new to elasticsearch so trying to figure it out exact query..

POST test/_search?search_type=count
{
"aggs": {
    "group_by_RequestID": {
        "terms": {
            "field": "RequestID"
        },
        "aggs": {
            "group_by_RequestID_docs": {
                "top_hits": {
                    "size": 1
                }
            }
        }
    }
}
}

but it's not giving correct answer. I want only those records where there is only 1 records for RequestID

Thanks again..


(system) #4