elasticsearch 7.x的api接口
下面命令以7.x版本为基础。通过crul命令可以模拟http请求,或者通过postman去发送操作请求。
一、操作
二、获取状态
GET "http://localhost:9200"三、新增⼀个⽂档
-H 表示增加header -d 即data,存放在body的json数据
PUT "localhost:9200/xdclass/_doc/1" -H 'Content-Type:
application/json' -d'
{
"user" : "lcy",
"message" : "lcy is good"
}
'四、删除⼀个⽂档
DELETE "localhost:9200/xdclass/_doc/1"五、索引
自动创建索引设置:
当添加文档的时候如果索引不存在。会根据这个配置决定是否创建索引。
注:分词器的设置,创建索引的时候必须指定好分词器,因为索引的分词器是一开始就固定了。
请求:
PUT "localhost:9200/_cluster/settings"{
"persistent": {
"action.auto_create_index": "true"
}
}响应:
{
"acknowledged": true,
"persistent": {
"action": {
"auto_create_index": "true"
}
},
"transient": {}
}六、新增
请求:
PUT "localhost:9200/es"响应:
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "es"
}七、获取指定索引
请求:
GET "localhost:9200/es"响应:
{
"es": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1563078001824",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "P-kmcRGlRECcBxAI_8mgaw",
"version": {
"created": "7020099"
},
"provided_name": "es"
}
}
}
}八、删除
请求:
DELETE "localhost:9200/es"响应:
{
"acknowledged": true
}九、批量获取
请求:根据逗号,分割索引查找
GET "localhost:9200/es,es1"响应:
{
"es": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1563078001824",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "P-kmcRGlRECcBxAI_8mgaw",
"version": {
"created": "7020099"
},
"provided_name": "es"
}
}
},
"es1": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1563078001824",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "P-kmcRGlRECcBxAI_8mgaw",
"version": {
"created": "7020099"
},
"provided_name": "es1"
}
}
}
}十、获取所有索引
请求1:
GET "localhost:9200/_all"响应:
{
"es": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1563078001824",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "P-kmcRGlRECcBxAI_8mgaw",
"version": {
"created": "7020099"
},
"provided_name": "es"
}
}
},
"es1": {
"aliases": {},
"mappings": {},
"settings": {
"index": {
"creation_date": "1563078001824",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "P-kmcRGlRECcBxAI_8mgaw",
"version": {
"created": "7020099"
},
"provided_name": "es1"
}
}
}
}请求2:
GET "localhost:9200/_cat/indices?v"响应:
health status index uuid pri rep docs.count
docs.deleted store.size pri.store.size
yellow open es bP6CZH5jSTGlhrTDzlq0bw 1 1 0
0 230b 230b
yellow open es1 3rGtXSv_QTK-GU_xHKRvmw 1 1 0
0 230b 230b十一、判断索引是否存在
请求:
curl -I "localhost:9200/es"响应:
状态码是200
十二、关闭索引
关闭索引,但是不删除。关闭以后,查询索引的时候多出verified_before_close:"true"字段
请求:
POST "localhost:9200/es/_close"响应:
{
"acknowledged": true,
"shards_acknowledged": true
}十三、开启索引
请求:
POST "localhost:9200/es/_open"响应:
{
"acknowledged": true,
"shards_acknowledged": true
}十四、获取索引下的文档数量
请求:
GET "localhost:9200/es/_count"响应:
{
"count": 835,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
}
}十五、映射mapping
对于已经存在的mapping,字段只能进行新增,不能进行修改操作
15.1 新增
请求:
PUT "localhost:9200/es/_mapping"{
"properties": {
"name": {
"type": "text"
},
"team_name": {
"type": "text"
},
"position": {
"type": "text"
},
"play_year": {
"type": "keyword"
},
"jerse_no": {
"type": "keyword"
}
}
}响应:
{
"acknowledged": true
}15.2 获取
单个获取请求:
GET "localhost:9200/es/_mapping"批量获取请求:
GET "localhost:9200/es,es1/mapping"全部获取请求:
GET "localhost:9200/all/_mapping"
#或者
GET "localhost:9200/_mapping"响应:
{
"es": {
"mappings": {
"properties": {
"jerse_no": {
"type": "keyword"
},
"name": {
"type": "text"
},
"play_year": {
"type": "keyword"
},
"position": {
"type": "keyword"
},
"team_name": {
"type": "text"
}
}
}
}
}15.3 修改
修改,新增字段
请求:
PUT "localhost:9200/nba/_mapping"{
"properties": {
"name": {
"type": "text"
},
"team_name": {
"type": "text"
},
"position": {
"type": "keyword"
},
"play_year": {
"type": "keyword"
},
"jerse_no": {
"type": "keyword"
},
"country": {
"type": "keyword"
}
}
}响应:
{
"acknowledged": true
}十六、文档
16.1 新增文档
es在7.x以前,mapping有type类型的概念,而在7.x以后,只有_doc这个默认的类型存在。所以索引后面的url跟着的document类型只能是默认值。
如果开启了自动创建索引,当索引不存在时自动创建
指定id的请求,使用PUT:
PUT localhost:9200/es/_doc/1{
"name": "张三",
"team_name": "部门",
"position": "岗位",
"play_year": "工龄",
"jerse_no": "工号"
}不指定id的请求,使用POST请求:
POST localhost:9200/es/_doc对于指定id的请求,如果说没有明确类型,id存在则修改旧的记录,不存在则新增。这时候可以指定类型为是创建,id存在的时候则报错
PUT localhost:9200/es/_doc/1?op_type=create{
"name": "张三",
"team_name": "部门",
"position": "岗位",
"play_year": "工龄",
"jerse_no": "工号"
}响应:
{
"_index": "es",
"_type": "_doc",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}16.2 获取文档
指定id查看请求:
POST localhost:9200/es/_doc/1查看多个文档请求:
POST localhost:9200/_mget{
"docs": [
{
#索引名称
"_index": "es",
#类型,取默认
"_type": "_doc",
#id值
"_id": "1"
},
{
"_index": "es1",
"_type": "_doc",
"_id": "2"
}
]
}查看某个索引下的多个文档请求:
POST localhost:9200/es/_mget{
"docs": [
{
#类型,取默认
"_type": "_doc",
#id值
"_id": "1"
},
{
"_type": "_doc",
"_id": "2"
}
]
}查看某个索引下的默认类型的多个文档请求:
POST localhost:9200/es/_doc/_mget{
"docs": [
{
#id值
"_id": "1"
},
{
"_id": "2"
}
]
}
#或者
{
"ids": ["1","2"]
}响应:
{
"_index": "es",
"_type": "_doc",
"_id": "1",
"_version": 1,
"_seq_no": "1",
"_primary_term": 2,
"found": true,
"_source": {
"name": "张三",
"team_name": "部门",
"position": "岗位",
"play_year": "工龄",
"jerse_no": "工号"
}
}16.3 修改文档
指定id修改请求:
POST localhost:9200/es/_update/1{
"doc": {
"name": "张三",
"team_name": "部门",
"position": "岗位",
"play_year": "工龄",
"jerse_no": "工号"
}
}向查询的_source新增一个字段请求:这里的字段会自动加到mapping里,类型根据参数自动识别。ctx对象为上下文对象,类似spring的上下文
POST localhost:9200/es/_update/1{
"script": "ctx._source.age = 18"
}向查询的_source删除一个字段请求:ctx对象为上下文对象,类似spring的上下文
POST localhost:9200/es/_update/1{
"script": "ctx._source.remove(\"age\")"
}根据参数值,更新指定⽂档的字段。如果指定文档不存在,,upsert参数包含的内容将会被插⼊到索引中,作为⼀个 新⽂档;如果指定的⽂档存在,ElasticSearch引擎将会执⾏指定的更新逻辑。 这里要求字段必须存在
请求:
POST localhost:9200/es/_update/1{
"script": {
"source": "ctx._source.age += params.score",
"params": {
"score": 4
}
},
"upsert": {
"score": 1
}
}16.4 删除文档
删除指定id请求:
DELETE localhost:9200/es/_doc/124、查询
词条(term)查询:词条查询不会分析查询条件,只有当词条和查询字符串完全匹配时,才匹配搜索。
全⽂(full text)查询:ElasticSearch引擎会先分析查询字符串,将其拆分成多个分词,只要已分析的字段中包含词条的任意⼀个,或全部包含,就匹配查询条件,返回该⽂档;如果不包含任意⼀个分词,表示没有任何⽂档匹配查询条件。注:全文搜索的字段,mapping一定要是text类型才能进行检索。
25、term查询
- 单条term查询
请求:
POST localhost:9200/es/_search{
"query": {
"term": {
"jerse_no": "工号"
}
}
}- 多条term查询
请求:
POST localhost:9200/es/_search{
"query": {
"terms": {
"jerse_no": ["12","22"]
}
}
}响应:
{
"took": 9,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "es",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"name": "张三",
"team_name": "部门1",
"position": "岗位",
"play_year": "工龄",
"jerse_no": "12",
"country": "中国",
"score": 1004
}
},
{
"_index": "es",
"_type": "_doc",
"_id": "2",
"_score": 1.0,
"_source": {
"name": "李四",
"team_name": "部门",
"position": "岗位",
"play_year": "工龄",
"jerse_no": "22",
"country": "中国"
}
}
]
}
}26、full text查询
注:全文搜索的字段,mapping一定要是text类型才能进行检索。
- 全部查询match_all:查询所有的记录,如果不分页默认为10条。分页信息为form和size
请求:
POST localhost:9200/es/_search{
"query": {
"match_all": {}
},
"from": 0,
"size": 10
}- 分词查询match:将条件分成一个个单词去匹配全文指定字段
请求:
POST localhost:9200/es/_search{
"query": {
"match": {
"team_name": "研"
}
}
}- 分词多字段查询multi_match:分词查询只是针对某一字段检索词,这里支持多字段
请求:
POST localhost:9200/es/_search{
"query": {
"multi_match": {
"query": "研发",
"fields": [
"team_name",
"position"
]
}
}
}- 不分词查询match_phrase:不分词查询的意思就是不将查询条件拆解,根据查询条件的单词进行匹配
请求:
POST localhost:9200/es/_search{
"query": {
"match_phrase": {
"team_name": "研"
}
}
}- 不分词前缀匹配match_phrase_prefix:即查询以查询条件为前缀的文档
请求:
POST localhost:9200/es/_search{
"query": {
"match_phrase_prefix": {
"position": "j"
}
}
}