目录
? ?es的操作是基于?Restful?风格的?API?操作,请求体则是?Json?格式。
格式:? ?PUT? ?/索引名称
索引命名规范:
ES 索引创建成功之后,以下属性将不可修改:
# 简单创建索引
PUT /es_db
# 插入数据自动创建索引
PUT /user/_doc/1
{
"name":"fox",
"age":32,
"address":"长沙麓谷"
}
# 静态映射创建索引
PUT /user
{
"settings": {
"number_of_shards": "1",
"number_of_replicas": "1"
},
"mappings": {
"properties": {
"name": {
"type": "keyword"
},
"age" : {
"type" : "long"
},
"address" : {
"type" : "text"
}
}
}
}
备注:es的索引并不像关系型数据库一样需要提前定义。它在PUT数据的过程中会动态生成索引的映射,如果后续PUT过程中有新的字段需要保存,它同样会动态添加新字段的映射
格式:? ?GET? ?/索引名称
# 查询索引
GET /es_db
# es_db是否存在
HEAD /es_db
# match 匹配查询,会对查询文本分词后匹配
GET /es_db/_search
{
"query": {
"match": {
"address": "广州白云"
}
}
}
# term 词项查询,属于精确查询,不会对查询文本分词
GET /es_db/_search
{
"query": {
"term": {
"address": "广州白云"
}
}
}
#通过URI搜索,使用“q”指定查询字符串,“query string syntax” KV键值对
#条件查询, 如要查询age等于28岁的 _search?q=*:***
GET /es_db/_doc/_search?q=age:28
#范围查询, 如要查询age在25至26岁之间的 _search?q=***[** TO **] 注意: TO 必须为大写
GET /es_db/_doc/_search?q=age[25 TO 26]
#查询年龄小于等于28岁的 :<=
GET /es_db/_doc/_search?q=age:<=28
#查询年龄大于28前的 :>
GET /es_db/_doc/_search?q=age:>28
#分页查询 from=*&size=*
GET /es_db/_doc/_search?q=age[25 TO 26]&from=0&size=1
#对查询结果只输出某些字段 _source=字段,字段
GET /es_db/_doc/_search?_source=name,age
#对查询结果排序 sort=字段:desc/asc
GET /es_db/_doc/_search?sort=age:desc
格式:? ?DELETE? ?/索引名称
DELETE /es_db
格式:??PUT? /索引名称
# 创建索引es_db,指定其主分片数量为 3,每个主分片的副本数量为 2
PUT /es_db
{
"settings" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
}
# 创建索引时可以指定IK分词器作为默认分词器
PUT /es_db
{
"settings" : {
"index" : {
"analysis.analyzer.default.type": "ik_max_word"
}
}
}
# 填了id则是修改,不填即是新增
POST /es_db/_doc
{
"name": "张三",
"sex": 1,
"age": 25,
"address": "广州天河公园",
"remark": "java developer"
}
# 指定id新增数据,是全字段的更新,相当于删除旧文档,重新建新文档
PUT /es_db/_doc/1
{
"name": "张三",
"sex": 1,
"age": 25,
"address": "广州天河公园",
"remark": "java developer"
}
# 部分更新:在原有文档上更新
# Update -文档必须已经存在,更新只会对相应字段做增量修改
POST /es_db/_update/1
{
"doc": {
"age": 28
}
}
# 使用 _update_by_query 更新文档
POST /es_db/_update_by_query
{
"query": {
"match": {
"_id": 1
}
},
"script": {
"source": "ctx._source.age = 30"
}
}
# 并发场景下修改文档
# _seq_no和_primary_term是对_version的优化,7.X版本的ES默认使用这种方式控制版本,所以当在
# 高并发环境下使用乐观锁机制修改文档时,要带上当前文档的_seq_no和_primary_term进行更新:
POST /es_db/_doc/2?if_seq_no=21&if_primary_term=6
{
"name": "李四xxx"
}
# 删除文档
DELETE /es_db/_doc/1
?注意:??POST和PUT都能起到创建/更新的作用,PUT需要对一个具体的资源进行操作也就是要确定id才能进行更新/创建,而POST是可以针对整个资源集合进行操作的,如果不写id就由ES生成一个唯一id进行创建新文档,如果填了id那就针对这个id的文档进行创建/更新
{"actionName":{"_index":"indexName", "_type":"typeName","_id":"id"}}
{"field1":"value1", "field2":"value2"}
POST _bulk
{"create":{"_index":"article", "_type":"_doc", "_id":3}}
{"id":3,"title":"标题1","content":"内容1","tags":["java", "面向对象"],"create_time":1554015482530}
{"create":{"_index":"article", "_type":"_doc", "_id":4}}
{"id":4,"title":"标题2","content":"内容2","tags":["java", "面向对象"],"create_time":1554015482530}
POST _bulk
{"index":{"_index":"article", "_type":"_doc", "_id":3}}
{"id":3,"title":"标题1","content":"内容1","tags":["java", "面向对象"],"create_time":1554015482530}
{"index":{"_index":"article", "_type":"_doc", "_id":4}}
{"id":4,"title":"标题2","content":"内容2","tags":["java", "面向对象"],"create_time":1554015482530}
POST _bulk
{"delete":{"_index":"article", "_type":"_doc", "_id":3}}
{"delete":{"_index":"article", "_type":"_doc", "_id":4}}
POST _bulk
{"update":{"_index":"article", "_type":"_doc", "_id":3}}
{"doc":{"title":"ES大法必修内功"}}
{"update":{"_index":"article", "_type":"_doc", "_id":4}}
{"doc":{"create_time":1554018421008}}
POST _bulk
{"create":{"_index":"article", "_type":"_doc", "_id":3}}
{"id":3,"title":"标题1","content":"内容1","tags":["java", "面向对
象"],"create_time":1554015482530}
{"delete":{"_index":"article", "_type":"_doc", "_id":3}}
{"update":{"_index":"article", "_type":"_doc", "_id":4}}
{"doc":{"create_time":1554018421008}}
GET _mget
{
docs": [
{
_index": "es_db",
_id": 1,
"_index": "article",
"_id": 4
}
]
}
#可以通过ID批量获取es_db的数据
GET /es_db/_mget
{
"docs": [
{
"_id": 1
},
{
"_id": 4
}
]
}
#简化后
GET /es_db/_mget
{
"ids":["1","2"]
}
GET /es_db/_msearch
{}
{"query" : {"match_all" : {}}, "from" : 0, "size" : 2}
{"index" : "article"}
{"query" : {"match_all" : {}}}