Elasticearch 查询详解(二)
知止自律打卡-微信小程序APP
一款微信自律打卡小程序,支持自定义打卡项目、习惯记录、项目日记、专注计时与数据统计。自律打卡、学习打卡、读书打卡、健身打卡、运动打卡、减肥打卡、戒色打卡、养生打卡、戒烟、戒酒、戒熬夜等多种场景。万能打卡模板自由创建。可自定义每日打卡目标,记录习惯连续天数,可视化追踪成长进度。
了解产品详情 →
微信搜索「知止自律打卡」
官网:
https://www.elastic.co/guide/en/enterprise-search-clients/index.html
1 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.1 创建一个索引
localhost:9200/索引名称/类型名称/文档id
PUT /test1/type1/1
{
"name": "有勇气的牛排",
"age": 18
}

1.1.1 字段类型:
字符串类型:text、keyword
数值类型:long、integer、short、byte、double、float、half_float、scaled_float
日期类型:date
布尔类型:boolean
二进制类型:binary
# 创建规则 索引+字段
PUT /test2
{
"mappings": {
"properties": {
"name":{
"type": "text"
},
"age":{
"type": "long"
},
"birthday":{
"type": "date"
}
}
}
}
1.1.2 获取索引信息
# 获取当前索引信息
GET test2

1.1.3 查看索引默认信息
PUT /test3/_doc/1
{
"name":"有勇气的牛排",
"age": 18,
"birthday": "2010-05-20"
}

GET test3

1.2 其他 _cat
# _cat 查看默认配置
# 获取数据库健康值
GET _cat/health
1.2 修改
1.2.1 PUT覆盖
重复执行
PUT /test3/_doc/1
{
"name":"有勇气的牛排",
"age": 18,
"birthday": "2010-05-20"
}
1.2.2 POST更新
# 创建/更新
PUT /test3/_doc/1
{
"name":"有勇气的牛排",
"age": 18,
"birthday": "2010-05-20"
}
# 更新
POST /test3/_doc/1/_update
{
"doc":{
"name": "大哥"
}
}
1.3 删除索引
更具路径判断删除索引还是文档
# 删除指定索引
DELETE test1
1.3 删除指定文档
DELETE users/_doc/170
2 文档基本操作
2.1 基本操作
2.1.1 添加数据
PUT /vitcloud/user/2
{
"name":"大漂亮",
"age": 17,
"desc": "女主",
"tags":["旅游","轻音乐","弹琴"]
}
2.1.2 查数据
GET /vitcloud/user/2
# 精确搜索
GET /vitcloud/user/_search?q=name:黑客
2.1.3 更新数据 PUT
如果不传值,会被覆盖
PUT /vitcloud/user/2
{
"name":"大漂亮2号",
"age": 17,
"desc": "女主",
"tags":["旅游","轻音乐","弹琴"]
}
2.1.4 更新数据 POST (推荐)
POST /vitcloud/user/1/_update
{
"doc":{
"name":"大漂亮2号"
}
}
2.2 复杂查询
2.2.1 模糊查询
GET /vitcloud/user/_search
{
"query":{
"match": {
"name": "有勇气"
}
}
}
2.2.2 _source: 结果过滤
GET /vitcloud/user/_search
{
"query":{
"match": {
"name": "有勇气"
}
},
"_source":["name","desc"]
}
2.2.3 分组查询
GET /blog_rate/_search
{
"size": 0,
"aggs": {
"group_by_tags": {
"terms": {
"field": "uid"
}
}
}
}
2.2.4 sort: 排序
GET /vitcloud/user/_search
{
"query":{
"match": {
"name": "有勇气"
}
},
"sort": [
{
"age":{
"order": "desc"
}
}
]
}
2.2.5 分页
/search/{current}/{pagesize}
from: 从第几条开始
size: 显示多少条
GET /vitcloud/user/_search
{
"query":{
"match": {
"name": "有勇气"
}
},
"_source":["name","desc"],
"from": 0,
"size": 2
}
2.2.6 bool 多条件精确查询
must:都满足
must_not:不满足的
should:满足一个即可
GET /vitcloud/user/_search
{
"query":{
"bool": {
"must": [
{
"match": {
"name": "有勇气"
}
},{
"match": {
"tags": "旅游"
}
}
]
}
}
}
2.2.7 范围过滤
gt:大于lt:小于gte:大于等于lte:小于等于
GET /vitcloud/user/_search
{
"query":{
"bool": {
"must": [
{
"match": {
"name": "有勇气"
}
},
"filter":{
"range":{
"age":{
"gt":10,
"lt":30
}
}
}
]
}
}
}
2.2.8 匹配多个 条件多条件 空格隔开
GET /vitcloud/user/_search
{
"query":{
"match": {
"tags": "游 音乐"
}
}
}
2.2.9 精确查询
term查询是直接通过倒排索引指定的词条进程精确的查找
关于分词
- term :直接查询精确的
- mathc:会使用分词器解释(先分析文档,然后通过分析的文档进行查询)
keyword不会被分词
2.2.10 精确查询 多个值
# 添加数据
PUT /vitcloud/_doc/1
{
"t1": "22",
"t2": "2022-02-22"
}
PUT /vitcloud/_doc/2
{
"t1": "33",
"t2": "2022-02-23"
}
GET vitcloud/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"t1": "22"
}
},
{
"term": {
"t1": "33"
}
}
]
}
}
}
2.2.11 高亮查询
默认为em标签
GET vitcloud/user/_search
{
"query": {
"match":{
"name": "有勇气"
}
},
"highlight": {
"fields": {
"name":{}
}
}
}
自定义标签
GET vitcloud/user/_search
{
"query": {
"match":{
"name": "有勇气"
}
},
"highlight": {
"pre_tags": "<p class='key' style='color:red'>",
"post_tags": "</p>",
"fields": {
"name":{}
}
}
}
来源
狂神说:https://www.bilibili.com/video/BV17a4y1x7zq
标签: