ES 聚合去重、控制输出结果字段、去除_index等默认字段

1.聚合去重

{
  "query" :{ "match_all":{}},
  "aggs": {
    "distinct_docField": {
      "cardinality": {
        "field": "docField"
      }
    }
  },
  "track_total_hits": true
}

针对docField 作去重统计,类似SQL:

  select count(distinct docField ) from tab

2.控制输出_source中输出字段

{
 "query" :{ "match_all":{}},
   "_source" : {
    "includes" : [ "docField"],
    "excludes" : [ "" ]  
  }
}

includes 为需要保留的结果字段,只对_source中的字段有效

查询结果类似:

{
   "_index" : "index_name",
   "_type" : "_doc",
   "_id" : "10017313046",
   "_score" : 2.0,
   "hits" : {
     "hits" : [
       {
         "_source" : {
           "docField" : 10017313046
         }
       }, ...
     ]
  }
}

3.去除_index等默认字段(或只显示需要的字段)

POST INDEX_NAME/_search?filter_path=hits.hits._source.docField
{
 "query" :{ "match_all":{}},
   "_source" : {
    "includes" : [ "docField"],
    "excludes" : [ "" ]  
  }
}

查询结果类似:

{
  "hits" : {
    "hits" : [
      {
        "_source" : {
          "docField" : 10017313046
        }
      }, ...
      ]
      }
}

filter_path 参数控制json的过滤路径

你可能感兴趣的:(菜鸟,Demo,问题处理,经验分享,elasticsearch)