一、RESTful API
IP:ElasticSearch 地址默认 localhost:9200
method | url地址 | 描述 |
---|---|---|
PUT | /索引名称/类型名称/文档id | 创建文档(指定文档id ) |
POST | /索引名称/类型名称 | 创建文档(随机文档id ) |
POST | /索引名称/类型名称/文档id/_update | 修改文档 |
DELETE | /索引名称/类型名称/文档id | 删除文档 |
GET | /索引名称/类型名称/文档id | 查询文档通过文档id |
POST | /索引名称/类型名称/_search | 查询所有数据 |
1.1 基本操作
创建文档(指定文档id )
# PUT /索引名称/~类型名称~/文档id
# {
请求体}
PUT /demo/test/1
{
"name":"Vincent Vic",
"tip":"Flowboot"
}
请求响应
#! Deprecation: [types removal] Specifying types in document index requests is deprecated, use the typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}).
{
"_index" : "demo",
"_type" : "test",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
创建索引
# PUT /索引名称
PUT /demo2
{
"mappings":{
"properties": {
"name":{
"type": "text"
},
"age":{
"type": "long"
}
}
}
}
查询创建的索引
GET demo2
得到的数据
{
"demo2" : {
"aliases" : {
},
"mappings" : {
"properties" : {
"age" : {
"type" : "long"
},
"name" : {
"type" : "text"
}
}
},
"settings" : {
"index" : {
"creation_date" : "1649492013747",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "NuUNYLzcQbCel-EPKgFOCg",
"version" : {
"created" : "7060299"
},
"provided_name" : "demo2"
}
}
}
}
添加数据
PUT /demo2/_doc/1
{
"name":"VVVV",
"age":22
}
查看索引
GET /demo2/_doc/1
修改索引数据
POST /demo2/_doc/1/_update
{
"doc":{
"name":"ccc"
}
}
删除文档/索引
DELETE /demo2
DELETE /demo2/_doc/1
1.2 复杂查询
准备工作
创建文档添加数据,本文如图所示
1.2.1 匹配查询
# 匹配查询
GET test4/user/_search
{
"query":{
"match":{
"name":"张三"
}
}
}
1.2.2 匹配查询 - 过滤字段
# 匹配查询 - 过滤字段
GET test4/user/_search
{
"query":{
"match":{
"name":"张三"
}
}
, "_source": ["name","desc"]
}
1.2.3 排序,分页
# 匹配查询 - 排序,分页 : from 第几个数据考试 ,size : 查询数量
GET test4/user/_search
{
"query":{
"match":{
"name":"张三"
}
}
,"sort": [
{
"age": {
"order": "desc"
}
}
],
"from": 0,
"size": 1
}
1.2.4 多条件匹配
# 多条件匹配
GET test4/user/_search
{
"query":{
"bool":{
"must": [
{
"match": {
"name":"张三"
}
},
{