elasticsearch 查询实践

GET _search
{
  "query": {
    "match_all": {}
  }
}

#查询所有
GET twitter/_search
{
  "query":{
    "match_all": {}
  }
}

PUT twitter/_doc/10
{
  "name":"test"
}

PUT twitter/article/10
{
  "name":"test"
}

#插入id自增
POST twitter/_doc
{
  "name":"test"
}

GET /

#插入
PUT oschinablog/_doc/1
{
  "title":"你还应该知道的哈希冲突解决策略",
  "url":"https://my.oschina.net/vivotech/blog/4268016",
  "author_url":"https://my.oschina.net/vivotech"
}

#通过title匹配查询
GET oschinablog/_search
{
  "query":{
    "match": {
      "title": "test"
    }
  }
}

#通过id查询返回指定字段
GET oschinablog/_doc/1?_source=title,url

#条件查询
GET _mget
{
  "docs":[
    {"_index":"twitter",
      "_id":1
    }
    ]
}

#查询ids中的document
GET twitter/_doc/_mget
{
  "ids": ["1", "2"]
}

#插入
GET oschinablog/_doc/1
{
  "url":"www.baidu.com"
}

#通过id获取
GET oschinablog/_doc/_mget
{
  "ids":["1"]
}

#更新部分字段
POST oschinablog/_update/1
{
  "doc":{
    "url":"www.baidu.com"
  }
}

#查询后进行更新
POST oschinablog/_update_by_query
{
  "query":{
    "match":{
      "title":"你还应该知道的哈希冲突解决策略"
    }
  },
  "script":{
    "source":"ctx._source.url=params.url;ctx._source.author_url=params.author_url;",
    "lang":"painless",
    "params":{
      "url":"www.google.com",
      "author_url":"www.github.com/forhj"
    }
  }
}

#对于中文字段的文档进行更新
POST edd/_update_by_query
{
  "query": {
    "match": {
      "姓名": "张彬"
    }
  },
  "script": {
    "source": "ctx._source[\"签到状态\"] = params[\"签到状态\"]",
    "lang": "painless",
    "params" : {
      "签到状态":"已签到"
    }
  }
}

#通过script更新,需要指定id
POST oschinablog/_update/1
{
  "script": {
    "source": "ctx._source.url=params.url"
    , "lang": "painless",
    "params": {
      "url":"www.baidu.com"
    }
  }
}

#插入或者更新
POST oschinablog/_update/1
{
  "doc": {
    "title":"test",
    "url":"www.google.com",
    "author_url":"www.immoc.com"
  },
  "doc_as_upsert":"true"
}

#检查一个文档是否存在
HEAD oschinablog/_doc/1

#删除一个文档
DELETE oschinablog/_doc/1

GET oschinablog/_search
{
  "query": {
    "match_all": {}
  }
}

#查询并删除 数组使用多个match
POST oschinablog/_delete_by_query
{
  "query":{
    "match":{
      "ids" : 
            "1"
    }
  }
}

#删除索引
DELETE twitter

#index批量插入,不要带空格用换行符分割,不管数据是否已经存在都会插入,有数据存在则进行覆盖
POST _bulk
{"index":{"_index":"me_blog1","_id":1}}
{"user":"aa","message":"is ok","date":"2020-5-16"}
{"index":{"_index":"me_blog1","_id":2}}
{"user":"bb","message":"is fine","date":"2020-5-17"}
{"index":{"_index":"me_blog1","_id":3}}
{"user":"cc","message":"is fucked","date":"2020-5-18"}

GET me_blog/_search
{
  "query":{
    "match_all": {}
  }
}

#查询数据总条数
GET me_blog/_count

#create批量插入,不能够覆盖已有的数据,如果已有数据存在则抛出异常,不存在则创建
POST _bulk
{"create":{"_index":"me_blog","_id":1}}
{"user":"aa","message":"is ok","date":"2020-5-16"}
{"create":{"_index":"me_blog","_id":2}}
{"user":"bb","message":"is fine","date":"2020-5-17"}
{"create":{"_index":"me_blog","_id":3}}
{"user":"cc","message":"is fucked","date":"2020-5-18"}

#使用_bulk删除
POST _bulk
{"delete":{"_index":"me_blog","_id":"1"}}

#使用_bulk更新
POST _bulk
{"update":{"_index":"me_blog","_id":"2"}}
{"doc":{"user":"bbb"}}

#查询
GET me_blog/_doc/2

#关闭索引
POST me_blog/_close

#开启索引
POST me_blog/_open

#冻结索引
POST me_blog/_freeze

#冻结后进行搜索需要加入ingore_throttled参数为false
GET me_blog/_search?ignore_throttled=false


#解冻索引
POST me_blog/_unfreeze

#统计
GET me_blog/_search
{
  "query":{
    "match": {
      "user": "bbb"
    }
  },
  "aggs":{
    "top_authors":{
      "field":"author"
    }
  }
}

GET .kibana_1/_search

#搜索文档并设定size
GET _search?size=1

#对多个索引进行搜索
POST /.kibana,.kibana_1/_search

#搜索所有文档
GET /_search

POST index1/_doc
{
  "val":1
}

POST _bulk
{"index":{"_index":"index2","_id":"2"}}
{"val":2}
{"index":{"_index":"index3","_id":"3"}}
{"val":3}

#查询多个index
POST /index1,index2,index3/_search

#查询以index开头的索引,排除index3
POST /index*,-index3/_search

#搜索
GET me_blog/_search?ignore_throttled=false

#分页搜索from代表那一页,下标从0开始
GET me_blog1/_search?size=1&from=0

#同上,另一种写法
GET me_blog1/_search
{
  "size": 1,
  "from": 0, 
  "query": {
    "match_all":{}
  }
}

#查询指定要输出的字段
GET me_blog1/_search?filter_path=hits.hits._source.user

#查询并指定返回的字段
GET oschinablog/_search
{
  "_source": ["title","author_url","url"],
  "query": {
    "match_all": {
      
    }
  }
}

#通配符匹配返回的字段
GET oschinablog/_search
{
  "_source":{
    "includes": [
      "tit*",
      "au*"
      ],
      "excludes":["*.lat"]
  },
  "query":{
    "match_all": {}
  }
}

#source为false时不会返回任何的字段
GET oschinablog/_search
{
  "_source": false,
  "query": {
    "match_all": {
      
    }
  }
}

#统计
GET oschinablog/_search
{
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "test1": {
      "script": {
        "lang": "painless",
        "inline": "doc['title'].value"
      }
    }
  }
}

#获取满足条件的count
GET oschinablog/_count
{
  "query": {
    "match": {
      "title": "你还应该知道的哈希冲突解决策略*"
    }
  }
}

#获取所有索引
GET _cat/indices

#获取索引的配置
GET my_blog/_settings

#创建索引并配置索引
PUT test2
{
  "settings": {
    "number_of_replicas": 2
    , "number_of_shards": 2
  }
}



#配置index的mapping,当index插入数据后mapping不能够进行修改,需要重新创建然后预设值mapping
DELETE twitter
PUT twitter
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  }
}
 
PUT twitter/_mapping
{
  "properties": {
    "address": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "age": {
      "type": "long"
    },
    "city": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "country": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "location": {
      "type": "geo_point"
    },
    "message": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "province": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "uid": {
      "type": "long"
    },
    "user": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    }
  }
}
POST _bulk
{ "index" : { "_index" : "twitter", "_id": 1} }
{"user":"双榆树-张三","message":"今儿天气不错啊,出去转转去","uid":2,"age":20,"city":"北京","province":"北京","country":"中国","address":"中国北京市海淀区","location":{"lat":"39.970718","lon":"116.325747"}}
{ "index" : { "_index" : "twitter", "_id": 2 }}
{"user":"东城区-老刘","message":"出发,下一站云南!","uid":3,"age":30,"city":"北京","province":"北京","country":"中国","address":"中国北京市东城区台基厂三条3号","location":{"lat":"39.904313","lon":"116.412754"}}
{ "index" : { "_index" : "twitter", "_id": 3} }
{"user":"东城区-李四","message":"happy birthday!","uid":4,"age":30,"city":"北京","province":"北京","country":"中国","address":"中国北京市东城区","location":{"lat":"39.893801","lon":"116.408986"}}
{ "index" : { "_index" : "twitter", "_id": 4} }
{"user":"朝阳区-老贾","message":"123,gogogo","uid":5,"age":35,"city":"北京","province":"北京","country":"中国","address":"中国北京市朝阳区建国门","location":{"lat":"39.718256","lon":"116.367910"}}
{ "index" : { "_index" : "twitter", "_id": 5} }
{"user":"朝阳区-老王","message":"Happy BirthDay My Friend!","uid":6,"age":50,"city":"北京","province":"北京","country":"中国","address":"中国北京市朝阳区国贸","location":{"lat":"39.918256","lon":"116.467910"}}
{ "index" : { "_index" : "twitter", "_id": 6} }
{"user":"虹桥-老吴","message":"好友来了都今天我生日,好友来了,什么 birthday happy 就成!","uid":7,"age":90,"city":"上海","province":"上海","country":"中国","address":"中国上海市闵行区","location":{"lat":"31.175927","lon":"121.383328"}}

GET twitter/_search
{
  "_source":false,
  "query":{
    "match": {
      "city": "北京"
    }
  }
}

#script条件查询
GET twitter/_search
{
  "query":{
    "script": {
      "source": "doc['city'].contains(params.name)", "lang": "painless",
      "params": {
        "name":"北京"
      }
    }
  }
}
#url查询
GET twitter/_search?q=city:("北京"^5 or "上海") AND user:"张三" AND age:[20 TO 30] &sort=age:desc&_source=user,age&size=2&from=0

#查询包含朝阳区老贾这些字中至少三个字的数据
#"user": "朝阳区-老贾", 等同于 
GET twitter/_search
{
  "query": {
    "match": {
      "user":{
        "query": "朝阳区-老贾",
      "operator":"or",
      "minimum_should_match":"3"
      }
    }
  }
}

#不需要score
GET twitter/_search
{"query": {
  "bool": {
    "filter": {
      "term": {
        "city.keyword":"北京"
      }
    }
  }
}
}

GET twitter/_search
{
  "query":{
    "match":{
      "user": "朝阳区-老贾"
    }
  }
}
#同上,可添加operator提高查询数据的精度
GET twitter/_search
{
  "query": {
    "match": {
      "user": {
        "query": "朝阳区-老贾",
        "operator": "and"
      }
    }
  }
}

#查询含有user,address和message的数据,对address包含朝阳的数据进行三倍加权
GET twitter/_search
{
  "query": {
    "multi_match": {
      "query": "朝阳",
      "fields": ["user","address^3","message"],
      "type": "best_fields"
    }
  }
}

#搜索对以朝为前缀开头的数据
GET twitter/_search
{
  "query": {
    "prefix": {
      "user": {
        "value": "朝"
      }
    }
  }
}

#精确匹配
GET twitter/_search
{
  "query": {
    "terms": {
      "user.keyword": [
        "双榆树-张三",
        "东城区-老刘"
        ]
    }
  }
}

GET twitter/_search
{
  "query": {
    "bool": {
      "must":
        {},
      "filter": 
        {},
      "must_not": 
        {},
      "should": 
        {},
      "minimum_should_match": 1,
      "boost": 1
    }
  }
}

#must:
#文档 必须匹配这些条件才能被包含进来。
#must_not:
#文档 必须不匹配这些条件才能被包含进来。 
#should:
#如果满足这些语句中的任意语句,将增加
#_score,否则,无任何影响。它们主要用于修正每个文
#档的相关性得分。 
#filter:
# 必须 匹配,但它以不评分、过滤模式来进行。这些语
#句对评分没有贡献,只是根据过滤标准来排除或包含文档
GET twitter/_search
{
  "query": {
    "bool": {
      "must":
        [
          {"match":{"city":"北京"}},
          {"match":{"age":"30"}}
        ],
        "must_not": [
          {"prefix": {
            "city": {
              "value": "上海"
            }
          }}
        ],
        "should": [
          {"match_phrase": {
            "message": "出发,下一站云南!"
          }}
        ],
        "filter": {
          "range": {
            "age": {
              "gte": 10,
              "lte": 50
            }
          }
        }
    }
  },
  "explain": false
}

#查询北京地址,在location位置为中心的3公里以内的所有文档
GET twitter/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "address": "北京"
        }}
      ]
    }
  },
  "post_filter": {
    "geo_distance": {
      "distance": "5km",
      "distance_type": "plane", 
      "location": {
        "lat":  39.920086,
        "lon": 116.454182
      }
    }
  },
  "sort": [
    {
      "_geo_distance": {
        "order": "asc",
        "location": "39.920086,116.454182",
        "unit": "km"
      }
    }
  ]
}

#另一种写法
GET twitter/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "address": "北京"
        }}
      ],
      "filter": {
         "geo_distance": {
         "distance": "5km",
         "distance_type": "plane", 
          "location": {
            "lat":  39.920086,
            "lon": 116.454182
          }   
        }
      }
    }
  },

  "sort": [
    {
      "_geo_distance": {
        "order": "asc",
        "location": "39.920086,116.454182",
        "unit": "km"
      }
    }
  ]
}

GET twitter/_search
{
  "query": {
  
  },
  "post_filter": {
    "geo_distance": {
      "distance": 100,
      "distance_unit": "km",
      "FIELD": {
        "lat": 40.73,
        "lon": -74.1
      }
    }
  },
  "sort": [
    {
      "_geo_distance": {
        "FIELD": {
          "lat": 40,
          "lon": -70
        },
        "order": "asc"
      }
    }
  ]
}

GET twitter/_search
{
  "query": {
  },
  "post_filter": {
    "geo_distance": {
      "distance": 100,
      "distance_unit": "km",
      "FIELD": {
        "lat": 40.73,
        "lon": -74.1
      }
    }
  },
  "sort": [
    {
      "_geo_distance": {
        "FIELD": {
          "lat": 40,
          "lon": -70
        },
        "order": "asc"
      }
    }
  ]
}

#查询三十到四十岁之间的document
GET twitter/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 30,
        "lte": 40
      }
    }
  },
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ]
}

PUT twitter/_doc/20
{
  "user" : "王二",
  "message" : "今儿天气不错啊,出去转转去",
  "uid" : 20,
  "age" : 40,
  "province" : "北京",
  "country" : "中国",
  "address" : "中国北京市海淀区",
  "location" : {
    "lat" : "39.970718",
    "lon" : "116.325747"
  }
}

#查询存在city这个字段的document
GET twitter/_search
{
  "query": {
    "bool": {
      "must": [
        {"exists": {"field": "city"}}
      ]
    }
  }
}

#取反
GET twitter/_search
{
  "query": {
    "bool": {
      "must_not": [
        {"exists": {"field": "city"}}
      ]
    }
  }
}

#查询包含happy的文档,and条件查询
GET twitter/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "message": {
            "query": "happy birthday",
            "operator": "and"
          }
        }}
      ]
    }
  }
}

#or条件查询
GET twitter/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "message": "happy birthday"
        }}
      ]
    }
  }
}

#and条件查询 使用minimum_should_match 包含两个单词
GET twitter/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "message": {
            "query": "happy birthday",
            "minimum_should_match": 2
          }
        }}
      ]
    }
  }
}

PUT twitter/_doc/8
{
  "user": "朝阳区-老王",
  "message": "Happy",
  "uid": 6,
  "age": 50,
  "city": "北京",
  "province": "北京",
  "country": "中国",
  "address": "中国北京市朝阳区国贸",
  "location": {
    "lat": "39.918256",
    "lon": "116.467910"
  }
}

PUT twitter/_doc/5
{
  "user": "朝阳区-老王",
  "message": "BirthDay My Friend Happy !",
  "uid": 6,
  "age": 50,
  "city": "北京",
  "province": "北京",
  "country": "中国",
  "address": "中国北京市朝阳区国贸",
  "location": {
    "lat": "39.918256",
    "lon": "116.467910"
  }
}

#包含的单词按要求排序获取结果,且不区分大小写
GET twitter/_search
{
  "query": {
    "match_phrase": {
      "message": "happy birthday"
    }
  }
}

PUT twitter/_doc/5
{
  "user": "朝阳区-老王",
  "message": "Happy Good BirthDay My Friend!",
  "uid": 6,
  "age": 50,
  "city": "北京",
  "province": "北京",
  "country": "中国",
  "address": "中国北京市朝阳区国贸",
  "location": {
    "lat": "39.918256",
    "lon": "116.467910"
  }
}

#允许happy与birthday拥有一个tokend的差别
GET twitter/_search
{
  "query": {
    "match_phrase": {
      "message": {
        "query": "Happy birthday",
        "slop": 1
      }
    }
  },
  "highlight": {
    "fields": {
      "message": {}
    }
  }
}

GET twitter/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ]
    }
  }
}


#_name 字段名 ,当匹配到了_name后会将字段存入matched_queries中统一返回
GET twitter/_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {
          "city": {
            "query": "北京",
            "_name":"城市"
          }
        }},
        {
          "match": {
            "country": {
              "query": "中国",
              "_name":"国家"
            }
          }
        }
      ],
      "should": [
        {"match": {
          "_id":{
            "query": "1",
            "_name":"ID"
          }
        }}
      ]
    }
  }
}

#sql查询
GET /_sql?
{
  "query":"""
  select * from twitter where age =30
  """
}

#一次请求多次查询
GET twitter/_msearch
{"index":"twitter"}
{"query":{"match_all":{}},"from":0,"size":1}
{"index":"twitter"}
{"query":{"bool":{"filter":{"term":{"city.keyword":"北京"}}}},"size":1}
{"index":"twitter"}
{"query":{"match_all":{}}}

#查询多个索引
GET /twitter,twitter1/_search

#查询并返回额外的 搜索北京
GET twitter/_search
{
  "profile": "true",
  "query": {
    "term": {
      "city.keyword": {
        "value": "北京"
      }
    }
  }
}

#搜索北和京二字
GET twitter/_search
{
  "profile": "true",
  "query": {
    "match": {
      "city": "北京"
    }
  }
}

DELETE twitter
 
PUT twitter
{
  "mappings": {
    "properties": {
      "DOB": {
        "type": "date"
      },
      "address": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "age": {
        "type": "long"
      },
      "city": {
        "type": "keyword"
      },
      "country": {
        "type": "keyword"
      },
      "location": {
        "type": "geo_point"
      },
      "message": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "province": {
        "type": "keyword"
      },
      "uid": {
        "type": "long"
      },
      "user": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}

POST _bulk
{"index":{"_index":"twitter","_id":1}}
{"user":"张三","message":"今儿天气不错啊,出去转转去","uid":2,"age":20,"city":"北京","province":"北京","country":"中国","address":"中国北京市海淀区","location":{"lat":"39.970718","lon":"116.325747"}, "DOB": "1999-04-01"}
{"index":{"_index":"twitter","_id":2}}
{"user":"老刘","message":"出发,下一站云南!","uid":3,"age":22,"city":"北京","province":"北京","country":"中国","address":"中国北京市东城区台基厂三条3号","location":{"lat":"39.904313","lon":"116.412754"}, "DOB": "1997-04-01"}
{"index":{"_index":"twitter","_id":3}}
{"user":"李四","message":"happy birthday!","uid":4,"age":25,"city":"北京","province":"北京","country":"中国","address":"中国北京市东城区","location":{"lat":"39.893801","lon":"116.408986"}, "DOB": "1994-04-01"}
{"index":{"_index":"twitter","_id":4}}
{"user":"老贾","message":"123,gogogo","uid":5,"age":30,"city":"北京","province":"北京","country":"中国","address":"中国北京市朝阳区建国门","location":{"lat":"39.718256","lon":"116.367910"}, "DOB": "1989-04-01"}
{"index":{"_index":"twitter","_id":5}}
{"user":"老王","message":"Happy BirthDay My Friend!","uid":6,"age":26,"city":"北京","province":"北京","country":"中国","address":"中国北京市朝阳区国贸","location":{"lat":"39.918256","lon":"116.467910"}, "DOB": "1993-04-01"}
{"index":{"_index":"twitter","_id":6}}
{"user":"老吴","message":"好友来了都今天我生日,好友来了,什么 birthday happy 就成!","uid":7,"age":28,"city":"上海","province":"上海","country":"中国","address":"中国上海市闵行区","location":{"lat":"31.175927","lon":"121.383328"}, "DOB": "1991-04-01"}


#统计查询格式

#"aggregations" : {
#    "<aggregation_name>" : {
#        "<aggregation_type>" : {
#            <aggregation_body>
#        }
#        [,"meta" : {  [<meta_data_body>] } ]?
#        [,"aggregations" : { [<sub_aggregation>]+
#} ]?
#    }
#    [,"<aggregation_name_2>" : { ... } ]*
#}

GET twitter/_search
{
  "size": 0,
  "aggs": {
    "age":{
      "range": {
        "field": "age",
        "ranges": [
          {
            "from": 20,
            "to": 30
          },{
            "from": 30,
            "to":40
          },{
            "from": 40,
            "to":50
          }
        ]
      }
    }
  }
}

#对日期进行统计
GET twitter/_search
{
  "size": 0,
  "aggs": {
    "birth_range": {
      "date_range": {
        "field": "DOB",
        "format": "yyyy-MM-dd", 
        "ranges": [
          {
            "from": "1989-01-01",
            "to": "1990-01-01"
          },
          {
            "from": "1991-01-01",
            "to": "1992-01-01"
          }
        ]
      }
    }
  }
}

#trems类似于group对结果进行分组统计
GET twitter/_search
{
  "query": {
    "match": {
      "message": "happy birthday"
    }
  },
  "size": 0,
  "aggs": {
    "city": {
      "terms": {
        "field": "city",
        "size": 10
      }
    }
  }
}

GET twitter/_search
{
  "size": 0,
  "aggs": {
    "birth_year": {
      "terms": {
        "script": {
          "source": "2020 - doc['age'].value"
        },
        "size": 10
      }
    }
  }
}

#根据值动态构建文档中中间隔为interval的桶
GET twitter/_search
{
  "size": 0,
  "aggs": {
    "age": {
      "histogram": {
        "field": "age",
        "interval": 1
      }
    }
  }
}

GET twitter/_search
{
  "size": 0,
  "aggs": {
    "year": {
      "date_histogram": {
        "field": "DOB",
        "interval": "year"
      }
    }
  }
}

#聚合统计city的数量,但是失效了
GET twitter/_search
{
  "size": 0,
  "aggs": {
    "city_count": {
      "cardinality": {
        "field": "city.keyword"
      }
    }
  }
}


GET twitter/_search
{
  "size": 0,
  "aggs": {
    "age_group": {
      "range": {
        "field": "age",
        "ranges": [
          {
            "from": 20,
            "to": 24
          },
          {
            "from": 24,
            "to": 26
          },
          {
            "from": 26
            , "to": 28
          }
        ]
      }
    }
  }
}

GET twitter/_search
{
  "size": 0,
  "aggs": {
    "city": {
      "terms": {
        "field": "city",
        "size": 10
      }
    }
  }
}

GET  twitter/_search
{
  "size": 0,
  "aggs": {
    "age": {
      "histogram": {
        "field": "age",
        "interval": 2
      }
    }
  }
}

#根据DOB的值动态根据year分割并存储进桶
GET twitter/_search
{
  "size": 0,
  "aggs": {
    "date": {
      "date_histogram": {
        "field": "DOB",
        "interval": "year"
      }
    }
  }
}

#获取平均年龄
GET twitter/_search
{
  "size": 0,
  "aggs": {
    "age_average": {
      "avg": {
        "field": "age"
      }
    }
  }
}

#查询city为上海的用户的平均年龄
GET twitter/_search
{
  "size": 0,
  "query": {
    "match": {
      "city": {
        "query": "北京",
        "operator": "and"
      }
    }
  }, 
  "aggs": {
    "average_age_beijin": {
      "avg": {
        "field": "age"
      }
    }
  }
}

#global针对所有文档进行计算平均值
GET twitter/_search
{
  "size": 0
  , 
  "query": {
    "match": {
      "city": "北京"
    }
  }, 
  "aggs": {
    "average_age": {
      "avg": {
        "field": "age"
      }
    },
    "average_age_all":{
      "global": {}
      ,"aggs": {
        "age_global_avg": {
          "avg": {
            "field": "age"
          }
        }
      }
    }
  }
}

#获取age的统计数据
GET twitter/_search
{
  "size": 0,
  "aggs": {
    "age_stats": {
      "stats": {
        "field": "age"
      }
    }
  }
}

#取age的最大值
GET twitter/_search
{
  "size": 0
  , "aggs": {
    "age_max": {
      "max": {
        "field": "age"
      }
    }
  }
}

#对统计的age平均值进行乘以1.5倍的处理
GET twitter/_search
{
  "size": 0
  , "aggs": {
    "avg_age_1.5": {
      "avg": {
        "field": "age"
        , "script": {
          "source": "_value * params.correction"
          , "params": {
            "correction":1.5
          }
        }
      }
    }
  }
}

#使用script的doc来形成聚合
GET twitter/_search
{
  
   "size": 0
   , "aggs": {
     "age_avg_time": {
       "avg": {"script": {
          "source": "doc['age'].value * params.times"
          , "params": {
            "times":2
          }
        }
         
       }
     }
   }
}


#百分比聚合
GET twitter/_search
{
  "size": 0
  , "aggs": {
    "age_quartiles": {
      "percentiles": {
        "field": "age",
        "percents": [
          25,
          50,
          75,
          100
        ]
      }
    }
  }
}

GET twitter/_search
{
  "size": 0
  , "aggs": {
    "age_quartiles": {
      "percentiles": {
        "field": "age",
        "percents": [
          1,
          5,
          25,
          50,
          75,
          95,
          99
        ]
      }
    }
  }
}

GET twitter/_search
{
  "size": 0
  , "aggs": {
    "age_avg": {
      "avg": {
        "field": "age"
      }
    }
  }
}

#首先生成每个城市的聚合,然后再对这个聚合进行计算平均年龄
GET twitter/_search
{
  "size": 0
  , "aggs": {
    "cities": {
      "terms": {
        "field": "city",
        "size": 10,
        "order": {
          "age_avg": "desc"
        }
      },
      "aggs": {
        "age_avg": {
          "avg": {
            "field": "age"
          }
        }
      }
    }
  }
}

#对年龄进行生成桶聚合,然后在计算平均年龄
GET twitter/_search
{
  "size": 0
  , "aggs": {
    "age_range": {
      "range": {
        "field": "age",
        "ranges": [
          {
            "from": 20,
            "to": 24
          },
          {
            "from": 24
            , "to": 28
          },
          {
            "from": 28,
            "to":30
          }
        ]
      },
      "aggs": {
        "avg_age": {
          "avg": {
            "field": "age"
          }
        }
      }
    }
  }
}

PUT sports
{
  "mappings": {
    "properties": {
      "birthdate": {
        "type": "date",
        "format": "dateOptionalTime"
      },
      "location": {
        "type": "geo_point"
      },
      "name": {
        "type": "keyword"
      },
      "rating": {
        "type": "integer"
      },
      "sport": {
        "type": "keyword"
      }
    }
  }
}

POST _bulk/
{"index":{"_index":"sports"}}
{"name":"Michael","birthdate":"1989-10-1","sport":"Baseball","rating":["5","4"],"location":"46.22,-68.45"}
{"index":{"_index":"sports"}}
{"name":"Bob","birthdate":"1989-11-2","sport":"Baseball","rating":["3","4"],"location":"45.21,-68.35"}
{"index":{"_index":"sports"}}
{"name":"Jim","birthdate":"1988-10-3","sport":"Baseball","rating":["3","2"],"location":"45.16,-63.58"}
{"index":{"_index":"sports"}}
{"name":"Joe","birthdate":"1992-5-20","sport":"Baseball","rating":["4","3"],"location":"45.22,-68.53"}
{"index":{"_index":"sports"}}
{"name":"Tim","birthdate":"1992-2-28","sport":"Baseball","rating":["3","3"],"location":"46.22,-68.85"}
{"index":{"_index":"sports"}}
{"name":"Alfred","birthdate":"1990-9-9","sport":"Baseball","rating":["2","2"],"location":"45.12,-68.35"}
{"index":{"_index":"sports"}}
{"name":"Jeff","birthdate":"1990-4-1","sport":"Baseball","rating":["2","3"],"location":"46.12,-68.55"}
{"index":{"_index":"sports"}}
{"name":"Will","birthdate":"1988-3-1","sport":"Baseball","rating":["4","4"],"location":"46.25,-68.55"}
{"index":{"_index":"sports"}}
{"name":"Mick","birthdate":"1989-10-1","sport":"Baseball","rating":["3","4"],"location":"46.22,-68.45"}
{"index":{"_index":"sports"}}
{"name":"Pong","birthdate":"1989-11-2","sport":"Baseball","rating":["1","3"],"location":"45.21,-68.35"}
{"index":{"_index":"sports"}}
{"name":"Ray","birthdate":"1988-10-3","sport":"Baseball","rating":["2","2"],"location":"45.16,-63.58"}
{"index":{"_index":"sports"}}
{"name":"Ping","birthdate":"1992-5-20","sport":"Baseball","rating":["4","3"],"location":"45.22,-68.53"}
{"index":{"_index":"sports"}}
{"name":"Duke","birthdate":"1992-2-28","sport":"Baseball","rating":["5","2"],"location":"46.22,-68.85"}
{"index":{"_index":"sports"}}
{"name":"Hal","birthdate":"1990-9-9","sport":"Baseball","rating":["4","2"],"location":"45.12,-68.35"}
{"index":{"_index":"sports"}}
{"name":"Charge","birthdate":"1990-4-1","sport":"Baseball","rating":["3","2"],"location":"46.12,-68.55"}
{"index":{"_index":"sports"}}
{"name":"Barry","birthdate":"1988-3-1","sport":"Baseball","rating":["5","2"],"location":"46.25,-68.55"}
{"index":{"_index":"sports"}}
{"name":"Bank","birthdate":"1988-3-1","sport":"Golf","rating":["6","4"],"location":"46.25,-68.55"}
{"index":{"_index":"sports"}}
{"name":"Bingo","birthdate":"1988-3-1","sport":"Golf","rating":["10","7"],"location":"46.25,-68.55"}
{"index":{"_index":"sports"}}
{"name":"James","birthdate":"1988-3-1","sport":"Basketball","rating":["10","8"],"location":"46.25,-68.55"}
{"index":{"_index":"sports"}}
{"name":"Wayne","birthdate":"1988-3-1","sport":"Hockey","rating":["10","10"],"location":"46.25,-68.55"}
{"index":{"_index":"sports"}}
{"name":"Brady","birthdate":"1988-3-1","sport":"Football","rating":["10","10"],"location":"46.25,-68.55"}
{"index":{"_index":"sports"}}
{"name":"Lewis","birthdate":"1988-3-1","sport":"Football","rating":["10","10"],"location":"46.25,-68.55"}

GET sports/_count

GET sports/_search
{
  "size":0,
  "aggs": {
    "NAME": {
      "terms": {
        "field": "name",
        "size": 10
        , "order": {
          "rating_avg": "desc"
        }
      },
      "aggs": {
        "rating_avg": {
          "avg": {
            "field": "rating"
          }
        }
      }
    }
  }
}

GET sports/_search
{
  "size": 0,
  "aggs": {
    "age_range": {
      "range": {
        "script": {
          "source": """
            ZonedDateTime dob = doc['birthdate'].value;
            return params.now - dob.getYear()
          """
          , "params": {
            "now":2020
          }
        },
        "ranges": [
          {
            "from": 30,
            "to": 31
          }
        ]
      }
    }
  }
}

#计数生成聚合桶
GET sports/_search
{
  "size": 0
  , "aggs": {
    "sports_count": {
      "value_count": {
        "field": "sport"
      }
    }
  }
}

#统计运动的类型生成聚合桶
GET sports/_search
{
  "size": 0
  , "aggs": {
    "sport": {
      "terms": {
        "field": "sport",
        "size": 10
      }
    }
  }
}

#计算范围内的运动员数量生成聚合桶
GET sports/_search
{
  "size": 0
  , "aggs": {
    "baseball_player_ring": {
      "geo_distance": {
        "field": "location",
        "origin": {
          "lat": 46.12,
          "lon": -68.55
        },
        "ranges": [
          {
            "from": 0,
            "to": 20
          }
        ]
      }
    }
  }
}

GET sports/_search
{
"_source": ["name","rating"]
}


GET sports/_search
{
  "size": 0
  , "aggs": {
    "sports_count": {
      "range": {
        "field": "",
        "ranges": [
          {
            "from": 50,
            "to": 100
          }
        ]
      }
    }
  }
}

GET sports/_search
{
  "size": 0
  , "aggs": {
    "baseball_player_ring": {
      "geo_distance": {
        "field": "location",
        "origin": {
          "lat": 46.12,
          "lon": -68.55
        },
        "ranges": [
          {
            "from": 0,
            "to": 20
          }
        ]
      }
    }
  }
}


PUT users
{
  "mappings": {
    "properties": {
      "age": {
        "type": "long"
      },
      "category": {
        "type": "keyword"
      },
      "country": {
        "type": "keyword"
      },
      "user": {
        "type": "keyword"
      }
    }
  }
}

POST _bulk
{ "index" : { "_index" : "users", "_id": 1} }
{"user":"bill", "age": 30, "country": "FR", "category": "A"}
{ "index" : { "_index" : "users", "_id": 2} }
{"user":"Marie", "age": 32, "country": "US", "category": "A"}
{ "index" : { "_index" : "users", "_id": 3} }
{"user":"Clarie", "age": 32, "country": "US", "category": "A"}
{ "index" : { "_index" : "users", "_id": 4} }
{"user":"Tom", "age": 44, "country": "DE", "category": "B"}
{ "index" : { "_index" : "users", "_id": 5} }
{"user":"John", "age": 40, "country": "US", "category": "B"}
{ "index" : { "_index" : "users", "_id": 6} }
{"user":"Emma", "age": 26, "country": "US", "category": "B"}

GET users/_search
{
  "size": 0,
  "aggs": {
    "category_terms": {
      "terms": {
        "field": "category",
        "size": 10
      },
      "aggs": {
        "country_terms": {
          "terms": {
            "field": "country",
            "size": 10
          },"aggs": {
            "avg_age": {
              "avg": {
                "field": "age"
              }
            }
          }
        }
      }
    }
  }
}

GET sports/_search
{
  "size": 0
  , "aggs": {
    "defender_filter": {
      "filter": {
        "term": {
          "sport": "Baseball"
        }
      },"aggs": {
        "avg_goals": {
          "avg": {
            "field": "rating"
          }
        }
      }
    }
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值