生若直木,不语斧凿.

ES basic

Posted on By xiaoyongsheng
Email: xiaoyongsheng@hotmail.com
Views:

统计去重后数量

去重数量统计: https://www.elastic.co/guide/cn/elasticsearch/guide/current/cardinality.html 百分位点的统计: https://www.elastic.co/guide/cn/elasticsearch/guide/current/percentiles.html

{
    "size" : 0,
    "aggs" : {
        "distinct_cnt" : {
            "cardinality" : {
              "field" : "field_name"
            }
        }
    }
}
# 统计各个非空枚举值及其数量
{
  "size": 0,
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "highestSchool",
        "size": 20
      }
    }
  }
}

统计userPhone=”185l$no7926”的不同firstPhone

{
  "query": {
    "term": {
      "userPhone": "185l$no7926"
    }
  },
  "aggs": {
    "count": {
      "cardinality": {
        "field": "firstPhone"
      }
    }
  }
}

统计值(最大/最小/平均值/和)

{
  "size": 0,
  "aggs": {
    "grades_stats": {
      "stats": {
        "field": "field_name"
      }
    }
  }
}

扩展统计值(方差/标准差等)

{
    "size": 0,
    "aggs" : {
        "grades_stats" : { "extended_stats" : { "field" : "grade" } }
    }
}

嵌套查询

可以同时支持嵌套内的条件查询和嵌套外的条件查询.

{
  "query": {
    "bool": {
      "must": [
        {
          "exists": {
            "field": "mobile"
          }
        },
        {
          "nested": {
            "path": "probExpectLocationList",
            "query": {
              "bool": {
                "must": [
                  {
                    "range": {
                      "probExpectLocationList.probability": {
                        "gte": 0.5
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ],
      "must_not": [],
      "should": [],
      "filter": [
        {
          "range": {
            "age": {
              "gte": 22,
              "lte": 45
            }
          }
        },
        {
          "range": {
            "personalScore.degreeRank": {
              "gte": 4
            }
          }
        },
        {
          "range": {
            "workAge": {
              "gte": 1
            }
          }
        },
        {
          "terms": {
            "highestProbExpectLocation": [
              "河北省+石家庄市",
              "辽宁省+沈阳市",
              "湖北省+武汉市",
              "四川省+成都市",
              "四川省+自贡市",
              "重庆市+重庆市",
              "山东省+济南市",
              "江苏省+苏州市",
              "浙江省+嘉兴市",
              "广东省+广州市"
            ]
          }
        }
      ]
    }
  },
  "from": 0,
  "size": 0,
  "sort": [],
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "highestProbExpectLocation",
        "size": 20
      }
    }
  }
}

参考文献