将基数聚合与“何处”条件相结合?

时间:2014-09-08 16:23:26

标签: elasticsearch cardinality elasticsearch-aggregation

我正在尝试根据字段创建不同的计数点击次数,但我被卡住了。

如何结合基数和"select distinct(lastname) from table where name like 'George%'"等条件?

如何使用基数的'where'条件?

{
    "aggs" : {
        "test_count" : {
            "cardinality" : {
                "field" : "lastname"
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您是否尝试过以下内容来获取不同的lastNames计数,其中firstName类似于' George'

curl -X POST "http://localhost:9200/gccount/Customer/_search?pretty=true" -d'                       
{                                                                                                   
   "query": {                                                                                       
    "fuzzy_like_this_field" : {                                                                     
        "firstName" : {                                                                                  
            "like_text" : "George",                                                               
            "max_query_terms" : 12                                                                  
        }                                                                                           
    }                                                                                               
   },                                                                                               
   "aggs": {                                                                                        
      "distinct_lastNames": {                                                                       
         "terms": {                                                                                 
            "field": "lastName"                                                                     
         }                                                                                          
      }                                                                                             
   }                                                                                                
}'  

我在弹性搜索字段analyzer中使用默认firstName对我有用。

Es Response

  "aggregations": {
      "distinct_lastNames": {
         "buckets": [
            {
               "key": "Upd",
               "doc_count": 1
            }, 
            {
               "key": "Bale",
               "doc_count": 2
            }
         ]
      }
   }

参考文献,

https://github.com/prayagupd/gccount/blob/master/gccount-analytics/scripts/search_customer_like.sh#L21

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-flt-field-query.html