ES聚合查询实例

查询特定渠道分享数量最大的30个文章的uuid:

{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "ul_actType": [
              1030
            ],
            "boost": 1
          }
        },
        {
          "terms": {
            "ul_distChannel": [
              2000,
              1000,
              1001,
              1019
            ],
            "boost": 1
          }
        }
      ],
      "disable_coord": false,
      "adjust_pure_negative": true,
      "boost": 1
    }
  },
  "explain": false,
  "aggregations": {
    "aggs_targetId": {
      "terms": {
        "field": "ul_targetId",
        "size": 30,
        "min_doc_count": 1,
        "shard_min_doc_count": 0,
        "show_term_doc_count_error": false,
        "order": [
          {
            "_count": "desc"
          },
          {
            "_term": "asc"
          }
        ]
      }
    }
  }
}
View Code

 

 

查询特定渠道30篇文章的阅读数,推荐数,点赞数,分享数:

{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "ul_targetId": [
              "94a803d3e883f96fc236c136de7fca2a",
              "dedff564f1e67d86fda44fb8dce6cbc5",
              "1b5ae5c0cfe26374af883e87d2b2f7c0",
              "3c5c5b3cc698aae419cad8d6eb02cbf2",
              "a7886af3f7d46dce4e5396dce3ed2b97",
              "35140f8d3f96bd48b3f6724c712150b0",
              "84801260669eec369d18225c6e63b01b",
              "10dc5b9bb733049a028b4ada6e7a4b84",
              "3587229434114e2fc3b3380643239f21",
              "93956578579740bc8b812eec57631f71",
              "9b9e248c892e3127ddcf3d5a440ab7a6",
              "4a9eb484d0595f210fbc49fdb1fb1eb5",
              "42f47d23cc154a4a6baddd5192f74f9c",
              "9b640b026fd85fe3eaa738dca483ff73",
              "2085b778ae6627cb236ae3cd6ba08acf",
              "3a0e3bab19b8d1902e1359a7d330f675",
              "4a10a6f8ddd78ae47bf3ff79bbc4a929",
              "83f179c72918e0ac424c6f08cb628922",
              "c642a8b80a5f72b426c1c30547e34ae3",
              "7d2994d2b17c331a4d14eb16362b41b7",
              "eaff7777a93fe1cfd05ce83dac3a07c4",
              "f239bd18ddb31a9b38305a3fa8135583",
              "ba3f535eb515c2f5948c902aa78e6733",
              "3fd4a262c96d17c2df22ede68c013fc8",
              "7bc7e72fdfb2f89200a975d8bec3323e",
              "a56fd1fc1cde98355cafb321bf6b7fa5",
              "1a21061052d3c72c5bff7c658fc9e656",
              "2cd32bc12d56168dc7143a756697a831",
              "70881bbbed12c5930af37de3d4c8cb6f",
              "8c66080ac50992e25a0a3b9f06eba89e"
            ],
            "boost": 1
          }
        },
        {
          "terms": {
            "ul_actType": [
              2000,
              1000,
              1010,
              1030
            ],
            "boost": 1
          }
        },
        {
          "terms": {
            "ul_distChannel": [
              2000,
              1000,
              1001,
              1019
            ],
            "boost": 1
          }
        }
      ],
      "disable_coord": false,
      "adjust_pure_negative": true,
      "boost": 1
    }
  },
  "explain": false,
  "aggregations": {
    "aggs_targetId": {
      "terms": {
        "field": "ul_targetId",
        "size": 30,
        "min_doc_count": 1,
        "shard_min_doc_count": 0,
        "show_term_doc_count_error": false,
        "order": [
          {
            "_count": "desc"
          },
          {
            "_term": "asc"
          }
        ]
      },
      "aggregations": {
        "aggs_actType": {
          "terms": {
            "field": "ul_actType",
            "size": 4,
            "min_doc_count": 1,
            "shard_min_doc_count": 0,
            "show_term_doc_count_error": false,
            "order": [
              {
                "_count": "desc"
              },
              {
                "_term": "asc"
              }
            ]
          }
        }
      }
    }
  }
}
View Code

 

 

 

聚合例子:

{
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "ul_actType": [
              1001
            ]
          }
        },
        {
          "terms": {
            "ul_actFrom": [
              1
            ]
          }
        },
        {
          "wildcard": {
            "ul_langMode": "*1*"
          }
        },
        {
          "range": {
            "ul_addTime": {
              "lt": 1494432000000,
              "gte": 1494345600000
            }
          }
        }
      ],
      "must_not": [
        {
          "wildcard": {
            "ul_userId": "temp*"
          }
        },
        {
          "term": {
            "ul_version.keyword": -1
          }
        }
      ]
    }
  },
  "size": 0,
  "aggregations": {
    "aggs_targetId": {
      "terms": {
        "field": "ul_targetId",
        "size": 50,
        "min_doc_count": 1000,
        "order": [
          {
            "_count": "desc"
          },
          {
            "_term": "asc"
          }
        ]
      },
      "aggs": {
        "rare_targetId": {
          "bucket_selector": {
            "buckets_path": {
              "doc_count": "_count"
            },
            "script": {
              "inline": "params.doc_count < 3000"
            }
          }
        }
      }
    }
  }
}
View Code

 

 

 多字段聚合例子:(2字段)

{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "image_count": {
              "from": "0",
              "to": null,
              "include_lower": false,
              "include_upper": true,
              "boost": 1
            }
          }
        },
        {
          "term": {
            "atype": {
              "value": "0",
              "boost": 1
            }
          }
        },
        {
          "term": {
            "flag_pool": {
              "value": "0",
              "boost": 1
            }
          }
        },
        {
          "term": {
            "flag_ql": {
              "value": "0",
              "boost": 1
            }
          }
        },
        {
          "term": {
            "flag_off": {
              "value": "0",
              "boost": 1
            }
          }
        },
        {
          "terms": {
            "domain": [
              "goldenmob.com"
            ],
            "boost": 1
          }
        },
        {
          "range": {
            "add_time": {
              "from": "0",
              "to": "2017-06-15",
              "include_lower": true,
              "include_upper": true,
              "boost": 1
            }
          }
        }
      ],
      "disable_coord": false,
      "adjust_pure_negative": true,
      "boost": 1
    }
  },
  "size": 0,
  "aggregations": {
    "agg_author_count": {
      "terms": {
        "field": "author"
      },
      "aggregations": {
        "agg_cate": {
          "terms": {
            "field": "app_category"
          }
        }
      }
    }
  }
}
View Code

 

 多字段聚合例子:(3字段)

{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "image_count": {
              "from": "0",
              "to": null,
              "include_lower": false,
              "include_upper": true,
              "boost": 1
            }
          }
        },
        {
          "term": {
            "atype": {
              "value": "0",
              "boost": 1
            }
          }
        },
        {
          "term": {
            "flag_pool": {
              "value": "0",
              "boost": 1
            }
          }
        },
        {
          "term": {
            "flag_ql": {
              "value": "0",
              "boost": 1
            }
          }
        },
        {
          "term": {
            "flag_off": {
              "value": "0",
              "boost": 1
            }
          }
        },
        {
          "terms": {
            "domain": [
              "goldenmob.com"
            ],
            "boost": 1
          }
        },
        {
          "range": {
            "add_time": {
              "from": "0",
              "to": "2017-06-15",
              "include_lower": true,
              "include_upper": true,
              "boost": 1
            }
          }
        }
      ],
      "disable_coord": false,
      "adjust_pure_negative": true,
      "boost": 1
    }
  },
  "size": 0,
  "aggs": {
    "agg_author_count": {
      "terms": {
        "field": "author"
      },
      "aggs": {
        "agg_cate": {
          "terms": {
            "field": "app_category"
          },
          "aggs": {
            "agg_lang": {
              "terms": {
                "field": "lang"
              }
            }
          }
        }
      }
    }
  }
}
View Code

注意:这里如果要进行多个字段的聚合,需要注意第二个aggs的位置,是在第一个的terms之后,而不是在第一个agg_author_count之后. aggregations = aggs

 

 

JSON转CSV:  https://json-csv.com/

posted @ 2017-03-04 16:41  0xcafedaddy  阅读(431)  评论(0编辑  收藏  举报