elasticsearch聚合结果键字段来自字段

时间:2016-06-27 05:33:41

标签: elasticsearch aggregation

我正在使用elasticsearch 1.7,我必须找到过滤器不能来自聚合键值 以下是结构:

{"RU": "2016-06-25T15:07:46.144","zt":"bl","zi":"z101"} 
{"RU": "2016-06-25T15:07:46.144","zt":"bl","zi":"z102"}
{"RU": "2016-06-25T15:07:46.144","zt":"bl","zi":"z103"}
{"RU": "2016-06-25T15:07:46.144","zt":"un","zi":"z201"}
{"RU": "2016-06-25T15:07:46.144","zt":"un","zi":"z202"}
{"RU": "2016-06-25T15:07:46.144","zt":"g1","zi":"z101"}
{"RU": "2016-06-25T15:07:46.144","zt":"g1","zi":"z502"}
{"RU": "2016-06-25T15:07:46.144","zt":"g2","zi":"z201"}
{"RU": "2016-06-25T15:07:46.144","zt":"g2","zi":"z503"}
My query :
    {"size": 0,
       "aggs": {
          "findunique": {
             "filter": {
                "bool": {
                   "must_not": [
                      {
                         "terms": {
                            "zt": [
                               "bl",
                               "un"
                            ]
                         }
                      }
                   ],
                   "must": [
                      {
                         "terms": {
                            "zt": [
                               "g1",
                               "g2"
                            ]
                         }
                      }
                   ]
                }
             },
             "aggs": {
                "uniquezi": {
                   "terms": {
                      "field": "zi"
                   }
                }
             }
          }
       }
 }
-------------------------------------------------------
output :
 {"aggregations": {
      "findunique": {
         "doc_count": 4,
         "uniquezi": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
               {
                  "key": "z101",
                  "doc_count": 1
               },
               {
                  "key": "z201",
                  "doc_count": 1
               },
               {
                  "key": "z502",
                  "doc_count": 1
               },
               {
                  "key": "z503",
                  "doc_count": 1
               }
            ]
         }
      }
   }
}}

现在我希望不显示zi = z101和z201不应该列入属于zt = bl和zt = un的列表 请建议我谢谢!

1 个答案:

答案 0 :(得分:0)

作为建议,您可以尝试在" zt"上设置文件管理器时添加两个聚合。领域。 通过这种方式,您将获得两组,并且可以从" Wanted"中提取所有内容。不在"不需要的"稍后在代码中。

{
  "size": 0,
  "aggs" : {
    "messages" : {
      "filters" : {
        "filters" : {
          "wanted" :   { "terms" : { "zt" : [ "g1", "g2" ] }},
          "unwanted" : { "terms" : { "zt" : [ "bl", "un" ] }}
        }
      },
      "aggs" : {
        "monthly" : {
            "terms": {"field" : "zi"}
        }
      }
    }
  }
}

回应:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 9,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "messages": {
      "buckets": {
        "wanted": {
          "doc_count": 4,
          "distinctValuesAgg": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "z101",
                "doc_count": 1
              },
              {
                "key": "z201",
                "doc_count": 1
              },
              {
                "key": "z502",
                "doc_count": 1
              },
              {
                "key": "z503",
                "doc_count": 1
              }
            ]
          }
        },
        "unwanted": {
          "doc_count": 5,
          "distinctValuesAgg": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "z101",
                "doc_count": 1
              },
              {
                "key": "z102",
                "doc_count": 1
              },
              {
                "key": "z103",
                "doc_count": 1
              },
              {
                "key": "z201",
                "doc_count": 1
              },
              {
                "key": "z202",
                "doc_count": 1
              }
            ]
          }
        }
      }
    }
  }
}