Rest 风格说明
Rest 风格说明#
一种软件架构风格,而不是标准,只是提供了一组设计原则和约束条件。它主要用于客户端和服务器交互类的软件。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。
基本的 Rest 命令说明:
method | url 地址 | 描述 |
---|---|---|
PUT | localhost:9200 / 索引名称 / 类型名称 / 文档 id | 创建文档 (指定文档 id) |
POST | localhost:9200 / 索引名称 / 类型名称 | 创建文档(随机文档 id ) |
POST | localhost:9200 / 索引名称 / 类型名称 / 文档 id/_update | 修改文档 |
DELETE | localhost:9200 / 索引名称 / 类型名称 / 文档 id | 删除文档 |
GET | localhost:9200 / 索引名称 / 类型名称 / 文档 id | 查询文档通过文档 id |
POST | localhost:9200 / 索引名称 / 类型名称 /_search | 查询所有数据 |
基础测试#
- 关于索引的基本操作
1、创建一个索引
PUT /索引名/~类型名~/文档id
{请求体}
2、完成了自动增加索引!数据也成功的添加了,这就是我说大家在初期可以把它当作数据库学习的原因
3、name 这个字段用不用指定类型呢。毕竟我们关系型数据库是需要指定类型的
- 字符串类型
text、keyword - 数值类型
long,integer,short,byte,double,float,half float,scaled float - 日期类型
date - 布尔值类型
boolean - 二进制类型
binary - 等等。。。
4、指定字段的类型
创建规则
获得这个规则!通过 GET 请求获得具体的信息!
5、查看默认的信息
如果自己的文档字段没有指定,那么 es 就会给我们默认配置字段类型
拓展:通过 elasticsearch 索引情况,通过 get _cat/ 可以获得 es 的当前的很多信息
修改 提交还是使用 PUT 即可,然后覆盖#
之前的方法
新的方法
PUT /test1/type1/1
{
"name": "hudu",
"age": 3
}
PUT /test2/
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"age": {
"type": "long"
},
"birthday": {
"type": "date"
}
}
}
}
GET test2
PUT /test3/_doc/1
{
"name": "弧度孤鬼",
"age": 22,
"birth": "1998-06-01"
}
GET test3
GET _cat/health
GET _cat/indices?v
PUT /test3/_doc/1
{
"name": "弧度孤鬼123",
"age": 22,
"birth": "1998-06-01"
}
POST /test3/_doc/1/_update
{
"doc":{
"name": "张三"
}
}
DELETE test1
删除索引#
通过 DELETE
命令实现删除、根据你的请求来判断删除的是索引还是删除文档记录
使用 restful 风格是我们 es 推荐的
关于文档的基本操作(重点)#
基本操作#
1、添加数据
PUT /hudu/user/1
{
"name": "HUDU",
"age": 22,
"desc": "测试",
"tags": ["宅","夜猫子","开发"]
}
2、获取数据 GET
GET hudu/user/1
3、更新数据 PUT
put 如果不传值,会被覆盖,值会为空,推荐使用 POST 方式更新数据。
4、POST _update,推荐使用这种更新方式
5、简单的搜索
通过 id 查询
GET hudu/user/1
简单的条件查询可以根据默认的映射规则,产生基本的查询!
GET hudu/user/_search?q=name:张
复杂操作搜索 select(排序,分页,高亮,模糊查询,精准查询)#
GET /hudu/user/_search
{
"query":{
"match": {
"name": "李"
}
}
}
测试数据
输出结构,不想要那么多
我们之后使用 Java 操作 es,所有的方法和对象就是在这里面的 key
分页查询#
GET hudu/user/_search
{
"query": {
"match": {
"name": "李"
}
},
"sort": [
{
"age": {
"order": "desc"
}
}
],
"from": 0,
"size": 1
}
数据下标还是从 0 开始,和学的所有的数据结构是一样的
/search/{current}/{pagesize}
布尔值查询#
多条件查询
must(and),所有的条件都要符合 where id=1 and name=xxx
should(or),所有的条件都要符合 where id=1 or name=xxx
must_not
过滤器 filter
GET hudu/user/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "李"
}
}
],
"filter": {
"range": {
"age": {
"gte": 10,
"lte": 30
}
}
}
}
}
}
- gt 大于
- gte(greater than equal) 大于等于
- lt 小于
- lte(less than equal) 小于等于
匹配多个条件#
GET hudu/user/_search
{
"query": {
"match": {
"tags": "宅 开发"
}
}
}
精确查询#
term 查询是直接通过倒排索引指定的词条进行精确的查找的!
关于分词:
- term,直接查询精确的
- match,会使用分词器分析!(先分析文档,然后再通过分析的文档进行查询!)
两个类型 text keyword
PUT testdb
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"desc": {
"type": "keyword"
}
}
}
}
PUT testdb/_doc/1
{
"name": "弧度 name",
"desc": "弧度 desc"
}
PUT testdb/_doc/2
{
"name": "弧度 name",
"desc": "弧度 desc2"
}
GET _analyze
{
"analyzer": "keyword",
"text": "弧度 name"
}
GET _analyze
{
"analyzer": "standard",
"text": "弧度 name"
}
PUT testdb
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"desc": {
"type": "keyword"
}
}
}
}
PUT testdb/_doc/1
{
"name": "弧度 name",
"desc": "弧度 desc"
}
PUT testdb/_doc/2
{
"name": "弧度 name",
"desc": "弧度 desc2"
}
GET _analyze
{
"analyzer": "keyword",
"text": "弧度 name"
}
GET _analyze
{
"analyzer": "standard",
"text": "弧度 name"
}
GET testdb/_search
{
"query": {
"term": {
"name": "弧"
}
}
}
GET testdb/_search
{
"query": {
"term": {
"desc": "弧度 desc"
}
}
}
多个值匹配的精确查询#
PUT testdb/_doc/3
{
"t1": "22",
"t2": "2020-09-27"
}
PUT testdb/_doc/4
{
"t1": "33",
"t2": "2020-09-28"
}
GET testdb/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"t1": "22"
}
},
{
"term": {
"t1": "33"
}
}
]
}
}
}
高亮查询#
GET hudu/user/_search
{
"query": {
"match": {
"name": "李四"
}
},
"highlight": {
"fields": {
"name": {}
}
}
}
GET hudu/user/_search
{
"query": {
"match": {
"name": "李四"
}
},
"highlight": {
"pre_tags": "<p class='key' style='color:red'>",
"post_tags": "</p>",
"fields": {
"name": {}
}
}
}
这些其实 MySQL 也可以做,只是 MySQL 效率比较低
- 匹配
- 按照条件匹配
- 精确匹配
- 区间范围匹配
- 匹配字段过滤
- 多条件查询
- 高亮查询
本作品采用《CC 协议》,转载必须注明作者和本文链接