GET、POST、PUT、DELETE、HEAD。
id | content |
---|---|
1001 | my name is zhang san |
1002 | i name is li si |
1003 | my name is wang wu |
keyword | id |
---|---|
name | 1001,1002,1003 |
zhang | 1001a |
my | 1001,1003 |
PUT请求:http://127.0.0.1:9200/索引名称
返回值:
{ "acknowledged"【响应结果】: true, # true 操作成功 //创建索引是否成功 "shards_acknowledged"【分片结果】: true, # 分片操作成功 "index"【索引名称】: "shopping" //创建的索引名称 } # 注意:创建索引库的分片数默认 1 片,在 7.0.0 之前的 Elasticsearch 版本中,默认 5 片
表头 | 含义 |
---|---|
health | 当前服务器健康状态: green(集群完整) yellow(单点正常、集群不完整) red(单点不正常) |
status | 索引打开、关闭状态 |
index | 索引名 |
uuid | 索引统一编号 |
pri | 主分片数量 |
rep | 副本数量 |
docs.count | 可用文档数量 |
docs.deleted | 文档删除状态(逻辑删除) |
store.size | 主分片和副分片整体占空间大小 |
pri.store.size | 主分片占空间大小 |
查看单个索引GET响应返回结果 { "shopping"【索引名】: { "aliases"【别名】: {}, "mappings"【映射】: {}, "settings"【设置】: { "index"【设置 - 索引】: { "creation_date"【设置 - 索引 - 创建时间】: "1614265373911", "number_of_shards"【设置 - 索引 - 主分片数量】: "1", "number_of_replicas"【设置 - 索引 - 副分片数量】: "1", "uuid"【设置 - 索引 - 唯一标识】: "eI5wemRERTumxGCc1bAk2A", "version"【设置 - 索引 - 版本】: { "created": "7080099" }, "provided_name"【设置 - 索引 - 名称】: "shopping" } } } }
全部查询:_search
单条数据查询:_doc
全量更新:PUT
单属性更新:POST
删除数据:DELETE
创建文档:PUT响应返回结果 { "_index"【索引】: "shopping", "_type"【类型-文档】: "_doc", "_id"【唯一标识】: "Xhsa2ncBlvF_7lxyCE9G", #可以类比为 MySQL 中的主键,随机生成 "_version"【版本】: 1, "result"【结果】: "created", #这里的 create 表示创建成功 "_shards"【分片】: { "total"【分片 - 总数】: 2, "successful"【分片 - 成功】: 1, "failed"【分片 - 失败】: 0 }, "_seq_no": 0, "_primary_term": 1 }
查看文档:GET响应返回结果 { "_index"【索引】: "shopping", "_type"【文档类型】: "_doc", "_id": "1", "_version": 2, "_seq_no": 2, "_primary_term": 2, "found"【查询结果】: true, # true 表示查找到,false 表示未查找到 "_source"【文档源信息】: { "title": "华为手机", "category": "华为", "images": "http://www.gulixueyuan.com/hw.jpg", "price": 4999.00 } }
多条件查询写法: { • "query":{//表示查询 • "match":{//部分匹配(match_all//全量查询,match_phrase//全匹配) • "条件key":"条件value" • } • }, • "from":0,//起始位置(页码-1*每页数据条数) • "size":2,//每页查询条数 • "_soource":{ • "title"//想要查询出来的字段 • }, • "sort":{ • "price":{//需要排序字段 • "order":"asc"//desc 排序方式 • } • } } { • "query":{ • "bool":{//条件参数 • "must":[//多个条件同时成立 should满足一个条件即成立 • { • "match":{ • "条件key":"条件value" • } • }, • { • "match":{ • "条件key":"条件value" • } • } • ], • "filter":{//过滤 • "range":{//范围 • "price":{//条件 • "gt":5000//大于 • } • } • } • } • }, • "highlight":{//高亮 • "pre_tags": "", • "post_tags": "", • "fields":{//字段 • "高亮字段" : {} • } • } } { • "aggs":{//聚合操作 • "price_group":{//名称,随意起名 (price_avg:平均值) • "terms":{//分组 (avg:平均值) • "分组字段" : "price" //分组字段 • } • } • }, • "size":0 //表示不需要原始数据 }
PUT添加映射关系 GET获取数据 { "properties":{//映射 "name":{ "type" : "text",//查询不需要完全匹配 "index" : true//是否能被索引,是否能被查询 }, "sex":{ "type" : "keyword",//查询需要完全匹配(不能被分词) "index" : true }, "tel":{ "type" : "keyword", "index" : false } } }
在Maven项目中的pom文件下引入依赖org.elasticsearch elasticsearch 7.8.0 org.elasticsearch.client elasticsearch-rest-high-level-client 7.8.0 org.apache.logging.log4j log4j-api 2.8.2 org.apache.logging.log4j log4j-core 2.8.2 com.fasterxml.jackson.core jackson-databind 2.9.9 junit junit 4.12
/* ES索引在java里简单操作: */ //创建ES客户端 RestHighLevelClient esClent = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost",9200,"http")) ); //创建索引 CreateIndexResponse createIndexResponse = esClient.indices().create(new