目录
1、ES常用API接口
2、基本操作
根据不同的操作具有不同的提交方式restful风格
GET 查询
PUT 修改
POST 添加操作
DELETE 删除操作
2.1 创建索引
创建索引并往索引中添加一条文档
PUT /索引名称/类型名称/1
{
"name":"张三","age":15
}
POST /索引名称/类型名称/
{
"name":"张三","age":15
}
创建索引但是不添加数据
PUT /索引名/类型
{
"mappings":{"properties": {
"name":{
"type": "text"
},
"age":{
"type": "integer"
}
}
}
}
2.2 删除所有
DELETE /索引名
2.3 查询有哪些索引
GET /_cat/indices?v
2.4 查询索引的结构
GET /索引名
2.5 添加文档
必须指定id的值
# 必须指定id的值
PUT /qy151/student/2
{
"name":"李四",
"age":25
}
不指定id
# 不指定id
POST /qy151/student/
{
"name":"王五",
"age": 32
}
2.6 查询文档
查询的提交方式必须为GET
GET /索引名称/类型名称/id值
2.7 删除文档
提交方式DELETE提交方式
DELETE /索引名称/类型名称/id值
2.8 修改文档
修改---这种修改必须要求指定所有列,如果只指定部分列,则原来其他列消失
# 修改---这种修改必须要求指定所有列,如果只指定部分列,则原来其他列消失
PUT /qy151/student/2
{
"name":"王五",
"age":28
}PUT /qy151/student/2
{
"name":"刘德华"
}
只修改部分列
# 只修改部分列
POST /qy151/student/1/_update
{
"doc":{
"name":"刘德华"
}
}
3、根据其他条件查询
3.1 查询所有文档
GET /索引名称/类型名称/_search
3.2 根据条件搜索
GET /索引名称/类型名称/_search?q=字段名:值
3.3 查询的条件封装成json数据
根据年龄查询数据
GET /索引名称 /_search{"query":{"match":{ //匹配"age":18}}}
GET /索引名称 /_search{"query":{"range":{ //范围匹配"age":{"gt":2 //查询年龄大于2}}}}
3.4 查询部分列
GET /索引名称 /_search{"query":{"match":{"age":18}},"_source":["name","age","desc"] //指定查询哪些列}
3.5 分页查询
GET /索引名称 /_search{"query":{"match":{"age":18}},"_source":["name","age","desc"], //指定查询哪些列"from":0, //从第几条记录查询"size":5 //每页显示的记录数}
3.6 排序
GET /索引名称 /_search{"query":{"range":{ //范围匹配"age":{"gt":2 //查询年龄大于2}}},"_source":["name","age","desc"], //指定查询哪些列"from":0, //从第几条记录查询"size":5, //每页显示的记录数"sort":[ //排序{"age":{"order":"desc" //降序}}]}
3.7 多条件查询
must:and
GET /索引名称/_search
{
"query":{
"bool":{
"must":[ //姓刘并且年龄等于18
{
"match":{
"name":"刘"
}
},{
"match":{
"age":18
}
}
]
}
}
}
should:or
GET /索引名称/_search
{
"query":{
"bool":{
"should":[ //姓刘或年龄等于18
{
"match":{
"name":"刘"
}
},{
"match":{
"age":18
}
}
]
}
}
}
must_not:取反 不等于 ... and ...
GET /索引名称/_search
{
"query":{
"bool":{
"must_not":[ //取反 不姓刘
{
"match":{
"name":"刘"
}
}
]
}
}
}
4、高亮显示
GET /索引名称 /_search{"query":{"match":{ //匹配"name":刘}},"highlight":{// "pre_tags":"<font color='red'>", //前置标签// "post_tags":"</font>", //后置标签"fields":{"name":{}}}}
# 创建索引并往索引中添加一条文档
PUT /test/abc/1
{
"name":"张三",
"age":15
}
# 创建索引并往索引中添加一条文档
POST /test/abc/
{
"name":"张四",
"age":15
}
# 创建索引不添加数据
PUT /test2
{
"mappings": {
"properties": {
"name":{
"type": "text"
},
"age":{
"type": "integer"
}
}
}
}
# 查询有哪些索引
GET /_cat/indices?v
# 删除索引
DELETE /test
# 查询索引的结构
GET /test
#添加文档 必须指定id的值
PUT /test2/_doc/1
{
"name":"李四",
"age":25
}
#添加文档 不指定id的值
POST /test2/_doc/
{
"name":"李四2",
"age":25
}
# 查询文档---id查询
GET /test2/_doc/1
# 修改文档--必须要求指定所有列只指定部分列其他列会消失
PUT /test2/_doc/1
{
"name":"王五",
"age":28
}
# 只修改部分列
POST /test2/_doc/1/_update
{
"doc":{
"name":"刘德华"
}
}
# 查询所有文档
GET /test/abc/_search
# 根据条件搜索
GET /test2/_doc/_search?q=age:28
# 查询的条件封装成json数据
# 查询年龄等于28
GET /test2/_doc/_search
{
"query": {
"match": {
"age":28
}
}
}
# 范围查询 查询年龄大于15
GET /test2/_doc/_search
{
"query": {
"range": {
"age":{
"gt":15
}
}
}
}
# 查询部分列
GET /test2/_doc/_search
{
"query": {
"match": {
"age":28
}
},
"_source":["name"]
}
# 分页
GET /test2/_doc/_search
{
"query": {
"range": {
"age":{
"gt":15
}
}
},
"_source":["name"],
"from":0,
"size":5
}
# 排序--降序
GET /test2/_doc/_search
{
"query": {
"range": {
"age":{
"gt":15
}
}
},
"_source":["name"],
"from":0,
"size":5,
"sort":[
{
"age":{
"order":"desc"
}
}
]
}
# 多条件查询
# 姓刘并且年龄等于28
GET /test2/_doc/_search
{
"query": {
"bool":{
"must":[
{
"match":{
"name":"刘"
}
},{
"match":{
"age":"28"
}
}
]
}
}
}
# 姓刘或年龄等于28
GET /test2/_doc/_search
{
"query": {
"bool":{
"should":[
{
"match":{
"name":"刘"
}
},{
"match":{
"age":"25"
}
}
]
}
}
}
# 不姓刘
GET /test2/_doc/_search
{
"query": {
"bool":{
"must_not":[
{
"match":{
"name":"刘"
}
}
]
}
}
}
# 高亮显示
GET /test2/_doc/_search
{
"query": {
"match": {
"name":"刘"
}
},
"highlight":{
"fields":{
"name":{}
}
}
}
# 自定义前置标签
GET /test2/_doc/_search
{
"query": {
"match": {
"name":"刘"
}
},
"highlight":{
"pre_tags":"<font color='red'>",
"post_tags":"</font>",
"fields":{
"name":{}
}
}
}