当前位置: 首页 > 工具软件 > Qing restfull > 使用案例 >

ES的RESTFUL API 一些常用命令案例

和魁
2023-12-01
#查看集群健康情况
GET /_cat/health?v

###查看有哪些索引
GET /_cat/indices?v
GET /_cat/indices


#使用post全部覆盖方式,修改内容,需要全部指定字段
PUT /index/user/1
{
  "name" : "zs",
  "age" : 13,
  "sex":"男"
}
#使用_update,修改指定字段内容,部分替换(partial update)
POST /index/user/1/_update
{
  "doc":{
    "age":15
  }
}



##使用脚本的方式修改数据,数字
GET /index/user/1/_update
{
  "script":"ctx._source.age+=1"
  
}
#字符串
GET /index/user/1/_update
{
  "script":"ctx._source.name+='hehe'"
}
#数组类型,添加
GET /index/user/1/_update
{
  "script":{
    "source":"ctx._source.arrname.add(params.tag)",
    "params": {
      "tag":"football"
    }
  }
}
#解释:arrname:表示数组字段名称,tag:表示自定义参数,tag和params里面的tag保持一致
#数组类型,删除
GET /index/user/1/_update
{
  "script":{
    "source":"ctx._source.arrname.remove(ctx._source.arrname.indexOf(parmas.tag))",
    "params": {
      "tag":"football"
    }
  }
}
#删除文档
GET /index/user/1/_update
{
  "script":{
    "source":"ctx.op=ctx._source.age==params.count?'delete':'none'",
    "params": {
      "count":22
    }
  }
}
#upsert操作,如果文档不存在,会进行初始化(创键文档),如果存在就执行"script":"ctx._source.age+=1"这个操作
GET /index/user/1/_update
{
  "script":"ctx._source.age+=1",
  "upsert":{
    "name" : "zs",
    "age" : 13,
    "sex":"男"
  }
}



PUT /index1

GET /index1

PUT /index1/user/1
{
  "name":"zs",
  "age":12,
  "sex":"男"
}


#从6.0以后一个索引下只能存在一种类型
PUT /index1/user/2
{
  "bookname":"python",
  "pagesize":1000,
  "price":30.2
}
PUT /index1/book/2
{
  "bookname":"python",
  "pagesize":1000,
  "price":30.3
}



#one(primary shard)只要有一个primary shard 是活跃的就可以执行
#all (all shard)所有的primary shard和replica shard都是活跃的才能执行
#quorum(default)默认值,大部分shard是活跃的才能执行(例如:共有6个shard,至少有3个shard是活跃的才能执行写操作)
#2.quorum机制:多数shard都是可用的
#int((primary+number_of_replica)/2)+1
#例如:3个primary shard,1个replica
#int((3+1)/2)+1=3
#至少3个shard是活跃的
#注意:可能出现shard不能分配齐全的情况
#比如:1个primary shard,1个replica int((1+1)/2)+1=2 但是如果只有一个节点,因为primary shard和replica shard 不能在同一个节点上,所以仍然不能执行
#写操作
#再举例:1个primary shard ,3个replica ,2个节点
#int((1+3)/2)+1=3

PUT /index/user/11?consistency=one
{
  "name" : "zs",
    "age" : 13,
    "sex":"男"
  
}

##别名,查看官网
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html

#查询所有的文档,不管哪个索引下的文档
GET _search
#hits默认只显示10条数据
#查询index索引下的文档
GET /index/_search
#多索引
GET /index,index1/_search
#通配符索引,可以直接省去类型,因为在es6以后规定一个索引下的索引只能存放一个类型,es7直接就废掉type了
GET /*x,*1/_search  
#带type查找
GET /index/user/_search

#多类型
GET /index,index1/type1,type2/_search

#集群中的索引
GET /_all/_search

###使用coyp_to提高查询的性能
copy_to字段是es6提供的,query string中如果不指定查询字段查询文档内容,效率会底,使用copy_to查询这个字段,coyp_to就是将每个字段的内容复制到copy_to字段中,使用copy_to需要自定义mapping,在每个字段中指定copy_to字段,coyp_to只能应用于text类型,日期数据不行
demo:
PUT /index/type/_mapping
{
    "properties":{
        "post_date":{
          "type":"date"
        },
        "title":{
          "type":"text",
          "copy_to": "自定义字段名"
        },
        "content":{
          "type":"text",
          "copy_to": "自定义字段名"
        },
        "author_id":{
          "type":"integer"
        }
    }
}

#不使用copy_to的query string
GET /index/type/_search?q=content:html
#不指定字段查询内容
GET /index/type/_search?q=html,document
#使用copy_to查询
GET /index/type/_search?q=自定义字段名:html,document


##################
#字符串排序问题,因为text分词,不能排序
PUT  /index_idea
{
  "settings": {
    "number_of_shards": 3, 
    "number_of_replicas": 2
  },
  "mappings": {
    "blog":{
      "properties":{
        "id":{
          "type":"long"
        },
        "title":{
          "type":"text",
          "analyzer":"ik_max_word",
          "fields":{
            "raw":{
              "type":"keyword"
            },
            "fielddata":true
          }
        },
        "postdate":{
          "type":"date"
        },
        "url":{
          "type":"text"
        }
      }
    }
  }
}
#添加这个就能排序
"fields":{
            "raw":{
              "type":"keyword"
            },
            "fielddata":true
          }
#排序,不能直接使用interests,因为它还是分词的
GET /index/type/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "interests.raw": {
        "order": "desc"
      }
    }
  ]
}
##################







###对elasticsearch的基本增删改查#####
#添加索引
PUT /lib/
{
  "settings": {
    "index":{
      "number_of_shards":3,
      "number_of_replicas":0
    }
  }
}

#查看索引的配置
GET /lib/_settings
#查看所有索引的配置
GET /_all/_settings

#添加文档
PUT /lib/user/1
{
  "name":"zs",
  "age":12,
  "hobby":"打飞机,跳舞,唱歌"
}
#不指定id使用post方式,id为RLChE2kB-j6TUjGAbl7M
POST /lib/user
{
  "name":"zs1",
  "age":13,
  "hobby":"跳舞,唱歌"
}

#查询文档,指定显示字段,GET
GET /lib/user/RLChE2kB-j6TUjGAbl7M?_source=name

#更新文档,使用put方式覆盖
PUT /lib/user/1
{
  "name":"zs",
  "age":14,
  "hobby":"打飞机,跳舞,唱歌"
}
#查询id为1的文档
GET /lib/user/1
#直接修改
POST /lib/user/1/_update
{
  "doc": {
    "age":15
  }
}
#删除某一个文档
DELETE /lib/user/1
#删除索引
DELETE /lib
######################################
###使用MultiGet实现批量获取文档,他们建立与index,type,id
#使用Multi Get API可用通过索引名,类型名,文档id一次得到一个文档集合,文档可以来自同一个索引库,也可来自不通过的索引库
GET /_mget
{
  "docs":[
    {
      "_index":"lib",
      "_type":"user",
      "_id":"1"
    },
    {
      "_index":"lib",
      "_type":"user",
      "_id":"RLChE2kB-j6TUjGAbl7M"
    }
    ]
}
#获取指定字段
GET /_mget
{
  "docs":[
    {
      "_index":"lib",
      "_type":"user",
      "_id":"1",
      "_source":"name"
      
    },
    {
      "_index":"lib",
      "_type":"user",
      "_id":"RLChE2kB-j6TUjGAbl7M",
      "_source":["name","age"]
    }
    ]
}
#获取同索引同类型的文档
GET /lib/user/_mget
{
  "docs":[
    {
      "_id":"1",
      "_source":"name"
      
    },
    {
      "_id":"RLChE2kB-j6TUjGAbl7M",
      "_source":["name","age"]
    }
    ]
}
##进一步简化
GET /lib/user/_mget
{
  "ids":["1","RLChE2kB-j6TUjGAbl7M"]
}


#####使用Bulk实现批量操作,增删改没有查
#bluk的格式
#{action:{metadata}}\n
#{requestbody}\n

#action:(行为)
#create:文档不存在时候创键
#update:更新文档
#index:创键新文档或替换已有的文档
#delete:删除一个文档,删除不需要{rquestbody}
#metadata:_index,_type,_id
#create和index的区别
#如果数据存在,使用create操作失败,会提示文档已经存在,使用index则可以执行成功
POST /lib2/books/_bulk
{"index":{"_id":1}}
{"name":"java设计模式","price":100}
{"index":{"_id":2}}
{"name":"rabbitmq指南","price":50}
#批量查询
GET /lib2/books/_mget
{
  "ids":["1","2"]
}

POST /lib2/books/_bulk
{"delete":{"_index":"lib2","_type":"books","_id":"2"}}
{"create":{"_index":"tt","_type":"ttt","_id":"100"}}
{"name":"lisi"}
{"index":{"_index":"tt","_type":"ttt"}}
{"name":"zhaosi"}
{"update":{"_index":"lib2","_type":"books","_id":"1"}}
{"doc":{"price":58}}
#查看tt下面所有的文档
GET /tt/ttt/_search


#版本控制
GET /tt/ttt/100
#内部版本控制,需要和文档version版本号相同
PUT /tt/ttt/100?version=2
{
  "name":"lisi1"
}
#外部版本号控制,需要大于文档version才可以
PUT /tt/ttt/100?version=4&version_type=external
{
  "name":"lisi2"
}
##什么是mapping
#自动映射,dynamic mapping
GET /myindex/article/_mapping

PUT /myindex/article/1
{
  "post_date":"2018-05-10",
  "title":"java",
  "content":"java is the best language",
  "author_id":119
}
PUT /myindex/article/2
{
  "post_date":"2018-05-12",
  "title":"html",
  "content":"i like html",
  "author_id":120
}
PUT /myindex/article/3
{
  "post_date":"2018-05-16",
  "title":"es",
  "content":"ES is distributed document store",
  "author_id":110
}


#查不出来,因为,查询post_data含有2018的
GET /myindex/article/_search?q=post_date:2018
#查出来,因为日期,数据,keyword不会分词
GET /myindex/article/_search?q=post_date:2018-05-10
#查出来,text会分词
GET /myindex/article/_search?q=content:html


#Object雷西兴
PUT /lib5/person/1
{
  "name":"Tom",
  "age":25,
  "birthday":"1985-12-12",
  "address":{
    "country":"china",
    "province":"guangdong",
    "city":"shenzhen"
  }
}


GET /lib5/person/_mapping

#底层存储结构
{
  "name":["Tom"],
  "age":[25],
  "birthday":["1985-12-12"],
  "address.country":["china"],
  "address.provice":["guangdong"],
  "address.city":["shenzhen"]
}

{
  "persons":[
    {"name":"lisi","age":27},
    {"name":"wangwu","age":20}
    {"name":"zhangsan","age":23}
    ]
}

#底层存储结构
{
  "persons.name":["lisi","wangwu","zhaoliu"],
  "persons.age":[27,20,23]
}

#手动创键mapping,tip:es会为每个字段创键倒排索引,如果index设置为false,则该字段将不会被索引
PUT /lib6
{
  "settings": {
    "index":{
      "number_of_shards":4,
      "number_of_replicas":0
    }
  },
  "mappings":{
    "books":{
      "properties":{
        "title":{"type":"text"},
        "name":{"type":"text","analyzer":"standard"},
        "publish_date":{"type":"date","index":"false"},
        "price":{"type":"double"},
        "number":{"type":"integer"}
      }
    }
  }
}
GET /lib6/books/_search

DELETE /lib6

POST /lib6/books/_bulk
{"index":{"_id":1}}
{"title":"es1","name":"java编程思想","publish_date":"2019-10-10","price":100,"number":50}
{"index":{"_id":2}}
{"title":"es1","name":"rabbitmq指南","publish_date":"2019-10-10","price":101,"number":51}


GET /lib6/_mapping
#日期查不出来,因为index为false,将不被索引了
GET /lib6/books/_search?q=publish_date:2019-10-10

#####基本查询(英文)1,分词器默认为standard分词器
PUT /lib7
{
  "settings": {
    "index":{
      "number_of_shards":4,
      "number_of_replicas":0
    }
  },
  "mappings":{
    "user":{
      "properties":{
        "name":{"type":"text"},
        "address":{"type":"text","analyzer":"standard"},
        "age":{"type":"integer"},
        "interests":{"type":"text"},
        "birthday":{"type":"date"}
      }
    }
  }
}
DELETE /lib7

PUT /lib7/user/_bulk
{"index":{"_id":1}}
{"name":"zhaoliu","address":"hei long jiang sheng tie lingshi","age":50,"birthday":"1970-12-12","interests":"xi huan hejiu,duanlian,lvyou"}
{"index":{"_id":2}}
{"name":"zhaoming","address":"bei jing hai dian qu qing he zhen","age":20,"birthday":"1998-10-12","interests":"xi huan hejiu,duanlian,changge"}
{"index":{"_id":3}}
{"name":"lisi","address":"bei jing hai dian qu qing he zhen","age":23,"birthday":"1998-10-12","interests":"xi huan hejiu,duanlian,changge"}
{"index":{"_id":4}}
{"name":"wamgwu","address":"bei jing hai dian qu qing he zhen","age":26,"birthday":"1995-10-12","interests":"xi huan biancheng,tingyinyue,lvyou"}
{"index":{"_id":5}}
{"name":"zhangsan","address":"bei jing chao yang qu","age":29,"birthday":"1988-10-12","interests":"xi huan tingyinyue,changge,tiaowu"}

#max_score  和当前搜索相关的最大匹配度分数
GET /lib7/user/_search?q=name:lisi

GET /lib7/user/_search?q=interests:changge&sort=age:desc
##term查询和terms查询
#term query会去倒排索引中寻找确切的term,他并不知道分词器的存在,这种查询适合keyword,numeric,date
#term:查询某个字段里含有多个关键词的文档
GET /lib7/user/_search
{
  "query": {
    "term": {
      "interests":"hejiu"
    }
  }
}
#term:查询某个字段里含有多少个关键词的文档
GET /lib7/user/_search
{
  "query": {
    "terms": {
      "interests": [
        "hejiu",
        "changge"
      ]
    }
  }
}
#从0开始取2个文档
GET /lib7/user/_search
{
  "from": 0,
  "size": 2, 
  "query": {
    "terms": {
      "interests": [
        "hejiu",
        "changge"
      ]
    }
  }
}
#显示version
GET /lib7/user/_search
{
  "version": true, 
  "query": {
    "terms": {
      "interests": [
        "hejiu",
        "changge"
      ]
    }
  }
}

#基本查询(英文)2,match查询,他知道分词器的存在
#他会将name拆开为zhaoliu,zhaoming两个去倒排索引中查询
#如果是trem,他不会将name拆开
GET /lib7/user/_search
{
  "query": {
    "match": {
      "name": "zhaoliu zhaoming"
    }
  }
}
GET /lib7/user/_search
{
  "query": {
    "match": {
      "age": 20
    }
  }
}
#查询所有文档
GET /lib7/user/_search
{
  "query": {
    "match_all": {}
  }
}

###multi_match可以指定多个字段
GET /lib7/user/_search
{
  "query": {
    "multi_match": {
      "query": "changge",
      "fields": ["interests","name"]
    }
  }
}

#match_phrase,短语匹配查询,es引擎首先分析(analyze)查询字符串,从分析后的文本中构建短语查询,这意味着必须匹配短语中的所有分词,并且保证各个分词的相对位置不变
GET /lib7/user/_search
{
  "query": {
    "match_phrase": {
      "interests": "duanlian,changge"
    }
  }
}
#指定显示的字段
GET /lib7/user/_search
{
  "_source": ["address","name"], 
  "query": {
    "match": {
      "interests": "changge"
    }
  }
}
#指定包含哪些字段,排除哪些字段
GET /lib7/user/_search
{
  "query": {
    "match_all": {}
  },
  "_source": {
    "includes": ["name","address"],
    "excludes": ["age","birthday"]
  }
}
#指定包含哪些字段,排除哪些字段
GET /lib7/user/_search
{
  "query": {
    "match_all": {}
  },
  "_source": {
    "includes": "addr*",
    "excludes": ["age","bir*"]
  }
}

#排序
GET /lib7/user/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ]
}

#前缀查询
GET /lib7/user/_search
{
  "query": {
    "match_phrase_prefix": {
      "name": {
        "query": "zhao"
      }
    }
  }
}
#范围查询,from,to默认包含前后范围
GET /lib7/user/_search
{
  "query": {
    "range": {
      "birthday": {
        "from": "1990-10-10",
        "to": "2018-05-01"
      }
    }
  }
}

GET /lib7/user/_search
{
  "query": {
    "range": {
      "age": {
        "from": 20,
        "to": 26,
        "include_lower": true,
        "include_upper": false
      }
    }
  }
}
#wildcard查询
#允许使用通配符*和?来进行查询
#*代表0个或多个字符
#?代表任意一个字符
GET /lib7/user/_search
{
  "query": {
    "wildcard": {
      "name": "zhao*"
    }
  }
}

GET /lib7/user/_search
{
  "query":{
    "wildcard":{
      "name":"li?i"
    }
  }
}

#fuzzy模糊查询,查询zhaoliu
GET /lib7/user/_search
{
  "query":{
    "fuzzy":{
      "name":"zholiu"
    }
  }
}

GET /lib7/user/_search
{
  "query":{
    "fuzzy":{
      "interests": {
        "value": "chagge"
      }
    }
  }
}

#高亮显示
GET /lib7/user/_search
{
  "query":{
    "match":{
      "interests":"changge"
    }
  },
  "highlight": {
    "fields": {
      "interests": {}
    }
  }
}

#基本查询(中文)
PUT /lib8
{
  "settings": {
    "index":{
      "number_of_shards":4,
      "number_of_replicas":0
    }
  },
  "mappings":{
    "user":{
      "properties":{
        "name":{"type":"text","analyzer":"ik_max_word"},
        "address":{"type":"text","analyzer":"ik_max_word"},
        "age":{"type":"integer"},
        "interests":{"type":"text","analyzer":"ik_max_word"},
        "birthday":{"type":"date"}
      }
    }
  }
}

PUT /lib8/user/_bulk
{"index":{"_id":1}}
{"name":"赵六","address":"黑龙江省铁岭","age":50,"birthday":"1970-12-12","interests":"喜欢喝酒,锻炼,说相声"}
{"index":{"_id":2}}
{"name":"赵明","address":"北京海淀区清河","age":20,"birthday":"1998-10-12","interests":"喜欢喝酒,锻炼,唱歌"}
{"index":{"_id":3}}
{"name":"lisi","address":"北京海淀区清河","age":23,"birthday":"1998-10-12","interests":"喜欢喝酒,锻炼,唱歌"}
{"index":{"_id":4}}
{"name":"王五","address":"北京海淀区清河","age":26,"birthday":"1995-10-12","interests":"喜欢编程,听音乐,旅游"}
{"index":{"_id":5}}
{"name":"张三","address":"北京海淀区清河","age":29,"birthday":"1988-10-12","interests":"喜欢摄影,听音乐,舞蹈"}

GET /lib8/user/_search
{
  "query": {
    "term": {
      "name": {
        "value": "赵"
      }
    }
  }
}
GET /lib8/user/_search
{
  "query": {
    "terms": {
      "interests": [
        "喝酒",
        "唱歌"
      ]
    }
  }
}
GET /lib8/user/_search
{
  "version": true, 
  "query": {
    "terms": {
      "interests": [
        "喝酒",
        "唱歌"
      ]
    }
  }
}
#控制返回的数量
GET /lib8/user/_search
{
  "version": true, 
  "from": 0,
  "size": 2, 
  "query": {
    "terms": {
      "interests": [
        "喝酒",
        "唱歌"
      ]
    }
  }
}

GET /lib8/user/_search
{
  "query": {
    "match": {
      "name": "赵六"
    }
  }
}

GET /lib8/user/_search
{
  "query": {
    "match": {
      "age": 20
    }
  }
}

GET /lib8/user/_search
{
  "query": {
    "match_all": {}
  }
}

GET /lib8/user/_search
{
  "query": {
    "multi_match": {
      "query": "旅游",
      "fields": ["interests","name"]
    }
  }
}


GET /lib8/user/_search
{
  
  "query": {
    "match_phrase": {
      "interests": "锻炼,说相声"
    }
  },
  "_source": ["name","address"]
}
#控制加载的字段,并且使用通配符
GET /lib8/user/_search
{
  
  "query": {
    "match_phrase": {
      "interests": "锻炼,说相声"
    }
  },
  "_source": {
    "includes": ["addr*"],
    "excludes": ["na*"]
  }
}

#排序

GET /lib8/user/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ]
}

#前缀匹配查询
GET /lib8/user/_search
{
  "query": {
    "match_phrase_prefix": {
      "name": "赵"
    }
  }
}
#范围查询,默认都是包含边界值的
GET /lib8/user/_search
{
  "query": {
    "range": {
      "age": {
        "gte": 0,
        "lte": 30,
        "include_lower": true,
        "include_upper": false
      }
    }
  }
}

#wildcard,通配符查询(?表示一个字符,*表示任意多个字符)
GET /lib8/user/_search
{
  "query": {
    "wildcard": {
      "name": "赵*"
    }
  }
}
#fuzzy模糊查询
GET /lib8/user/_search
{
  "query": {
    "fuzzy": {
      "interests": {
        "value":"喝酒"
      }
    }
  }
}
####过滤查询filter
POST /lib9/items/_bulk
{"index":{"_id":1}}
{"price":40,"itemID":"ID100123"}
{"index":{"_id":2}}
{"price":50,"itemID":"ID100124"}
{"index":{"_id":3}}
{"price":25,"itemID":"ID100125"}
{"index":{"_id":4}}
{"price":30,"itemID":"ID100126"}
{"index":{"_id":5}}
{"price":null,"itemID":"ID100127"}
#term过滤查询price为40的
GET /lib9/items/_search
{
  "query": {
    "bool":{
      "filter":[
        {"term":{"price":40}}
        ]
    }
  }
}
#terms过滤查询
GET /lib9/items/_search
{
  "query": {
    "bool":{
      "filter":[
        {"terms":{"price":[25,40]}}
        ]
    }
  }
}
#itemID为ID100123的文档存在但是查询不出来,因为默认为text分词了。。。解决办法有两种
GET /lib9/items/_search
{
  "query": {
    "bool":{
      "filter":[
        {"term":{"itemID":"ID100123"}}
        ]
    }
  }
}
#一、分词,默认将id转换为小写的
GET /lib9/items/_search
{
  "query": {
    "bool":{
      "filter":[
        {"term":{"itemID":"id100123"}}
        ]
    }
  }
}

GET /lib9/_mapping
DELETE /lib9
#二、自己创建mapping,不让分词就行了,不能使用index为false,index为false将不被索引,查询报错,该字段不存在,使用keyword
PUT /lib9
{
  "mappings" : {
      "items" : {
        "properties" : {
          "itemID" : {
            "type" : "keyword"
          },
          "price" : {
            "type" : "long"
          }
        }
      }
    }
}
#bool过滤查询
#可以实现组合过滤查询
#格式:
#{"bool":{"must":[],"should":[],"must_not":[]}}
#must必须满足的条件---and
#should:可以满足也可以不满足的条件 --or
#must_not不需要满足的条件--not
GET /lib9/items/_search
{
  "query": {
    "bool": {
      "should": [
        {"term": {"price": {"value": 25}}},
        {"term": {"itemID": {"value": "ID100123"}}}
      ],
      "must_not": [
        {"term": {"price": {"value": "30"}}}
      ]
    }
  }
}

GET /lib9/items/_search
{
  "query": {
    "bool": {
      "should": [
        {"term": {"itemID": {"value": "ID100123"}}},
        {
          "bool": {
            "must": [
              {"term": {"itemID": {"value": "ID100124"}}},
              {"term": {"price": {"value": 40}}}
            ]
          }
        }
      ]
    }
  }
}

#范围过滤
GET /lib9/items/_search
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "price": {
            "gte": 25,
            "lte": 50
          }
        }
      }
    }
  }
}

#查询price字段不为空的
GET /lib9/items/_search
{
  "query": {
    "bool": {
      "filter": {"exists": {
        "field": "price"
      }}
    }
  }
}

#聚合查询
#sum,price_of_sum自定义的名字,如果其他文档的结果我们不关心,则使永size为0 就行了
GET /lib9/items/_search
{
  "size":0,
  "aggs":{
    "price_of_sum":{
      "sum": {
        "field": "price"
      }
    }
  }
}
#min,max,avg
GET /lib9/items/_search
{
  "size":0,
  "aggs":{
    "price_of_min":{
      "min": {
        "field": "price"
      }
    }
  }
}
#cardinality求基数,互不相同的个数,忽略null
GET /lib9/items/_search
{
  "size":0,
  "aggs":{
    "price_of_cardinality":{
      "cardinality": {
        "field": "price"
      }
    }
  }
}
#分组用terms
GET /lib9/items/_search
{
  "size":0,
  "aggs":{
    "price_of_min":{
      "terms": {
        "field": "price"
      }
    }
  }
}
#对用唱歌兴趣的用户按年龄分组,再对每一组求平均年龄,按每一组中的平均年龄降序排序
GET /lib7/user/_search
{
  "size": 0, 
  "query": {
    "match": {
      "interests": "changge"
    }
  },
  "aggs": {
    "age_of_group": {
      "terms": {
        "field": "age",
        "order": {
          "age_of_avg": "desc"
        }
      },
      "aggs": {
        "age_of_avg": {
          "avg": {
            "field": "age"
          }
        }
      }
    }
  }
}
#复合查询
GET /lib7/user/_search
{
  "query":{
    "bool":{
      "must":{"match":{"interests":"changge"}},
      "must_not": {"match":{"interests":"lvyou"}},
      "should": [
        {"match": {"address": "bei jing"}},
        {"range":{"birthday":{"gte":"1996-01-01"}}}
      ]
    }
  }
}
GET /lib7/user/_search
{
  "query":{
    "bool":{
      "must":{"match":{"interests":"changge"}},
      "must_not": {"match":{"interests":"lvyou"}},
      "should": [
        {"match": {"address": "bei jing"}}
      ],
      "filter": {
        "range": {
          "birthday": {
            "gte": "1996-01-01"
          }
        }
      }
    }
  }
}
GET /lib7/user/_search
{
  "query":{
    "bool":{
      "must":{"match":{"interests":"changge"}},
      "must_not": {"match":{"interests":"lvyou"}},
      "should": [
        {"match": {"address": "bei jing"}}
      ],
      "filter": {
        "bool": {
          "must":[
              {"range":{"birthday":{"gte":"1990-01-01"}}},
              {"range":{"age":{"lte":30}}}
            ],
            "must_not":[
              {"term":{"age":"29"}}
              ]
        }
      }
    }
  }
}

#constant_score查询
#他将是一个不变的常量评分应用于所有的匹配文档
#他经常被用于你只需要执行一个filter,而没有其他查询(例如:评分查询)的情况下
#term查询放置在constant_score中,转成不评分的filter,这种方式可以用来取代filter语句的bool
GET /lib7/user/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "interests": "changge"
        }
      }
    }
  }
}

 

 类似资料: