前言
初次学习ES认为api极其繁琐,因此多次到了使用ES的环境总感觉些许不适应,因此重新学习,重新学习之后感觉Boot的api很清晰,担心以后会再次遗忘,遂写下本文章用作复习和巩固
目录
URL路径请求规范
APIFOX地址
分词器操作
测试分词器
GET /_analyze
{
"analyzer":"ik_max_word",
"text":"今天晚饭很好吃"
}
{
"tokens": [
{
"token": "今天",
"start_offset": 0,
"end_offset": 2,
"type": "CN_WORD",
"position": 0
},
{
"token": "晚饭",
"start_offset": 2,
"end_offset": 4,
"type": "CN_WORD",
"position": 1
},
{
"token": "很好",
"start_offset": 4,
"end_offset": 6,
"type": "CN_WORD",
"position": 2
},
{
"token": "好吃",
"start_offset": 5,
"end_offset": 7,
"type": "CN_WORD",
"position": 3
}
]
}
索引操作
创建索引
PUT /user
{
"mappings": {
"properties": {
"id": {
"type": "long",
"index": "true"
},
"name": {
"properties": {
"firstName": {
"type": "keyword"
},
"lastName": {
"type": "keyword"
}
}
},
"birthDay": {
"type": "date"
},
"introduce": {
"type": "text",
"index": "true",
"analyzer": "ik_smart"
},
"email": {
"type": "keyword",
"index": "false"
},
"score": {
"type": "integer",
"index": "true"
},
"gender": {
"type": "integer",
"index": "true"
},
"is_health": {
"type": "boolean",
"index": false
},
"height": {
"type": "float",
"index": "false"
},
"addr":{
"type":"keyword",
"index":true
}
}
}
}
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "user"
}
查询索引
GET /user
{
"user": {
"aliases": {},
"mappings": {
"properties": {
"addr": {
"type": "keyword"
},
"birthDay": {
"type": "date"
},
"email": {
"type": "keyword",
"index": false
},
"gender": {
"type": "integer"
},
"height": {
"type": "float",
"index": false
},
"id": {
"type": "long"
},
"introduce": {
"type": "text",
"analyzer": "ik_smart"
},
"is_health": {
"type": "boolean",
"index": false
},
"name": {
"properties": {
"firstName": {
"type": "keyword"
},
"lastName": {
"type": "keyword"
}
}
},
"score": {
"type": "integer"
}
}
},
"settings": {
"index": {
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"number_of_shards": "1",
"provided_name": "user",
"creation_date": "1726092503460",
"number_of_replicas": "1",
"uuid": "0Aqr3R4gTCyvujNXms4Npg",
"version": {
"created": "7100199"
}
}
}
}
}
删除索引
DELETE /user
{
"acknowledged": true
}
修改索引
PUT /user/_mapping
{
"properties":{
"height":{
"type":"float",
"index":"false"
}
}
}
{
"acknowledged": true
}
文档操作
创建文档
POST /user/_doc/1
{
"birthDay":"2024-08-05",
"email":"2059839873@163.com",
"gender":"1",
"height":182.5,
"id":1,
"introduce":"一个宅男",
"is_health":true,
"name":{
"firstName":"孙",
"lastName":"健栋"
},
"score":89.2
}
{
"_index": "user",
"_type": "_doc",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
查询文档
GET /user/_doc/1
{
"_index": "user",
"_type": "_doc",
"_id": "1",
"_version": 1,
"_seq_no": 0,
"_primary_term": 1,
"found": true,
"_source": {
"birthDay": "2024-08-05",
"email": "2059839873@163.com",
"gender": "1",
"height": 182.5,
"id": 1,
"introduce": "一个宅男",
"is_health": true,
"name": {
"firstName": "孙",
"lastName": "健栋"
},
"score": 89.2
}
}
删除文档
DELETE /user/_doc/1
{
"_index": "user",
"_type": "_doc",
"_id": "1",
"_version": 2,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
增量修改
POST /user/_update/1
{
"doc": {
"name": {
"firstName": "孙",
"lastName": "健栋22222"
}
}
}
{
"_index": "user",
"_type": "_doc",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 3,
"_primary_term": 1
}
常用查询操作
查询全部
GET /user/_search
{
"query":{
"match_all":{}
}
}
单字段匹配
GET /user/_search
{
"query":{
"match":{
"introduce":"happens"
}
}
}
多字段匹配
GET /user/_search
{
"query":{
"multi_match":{
"query":"if",
"fields":["introduce"]
}
}
}
精确匹配
GET /user/_search
{
"query": {
"term": {
"addr": {
"value": "北京市"
}
}
}
}
数值范围匹配
GET /user/_search
{
"query":{
"range":{
"score":{
"gte":"90",
"lte":"100"
}
}
}
}
得分控制
GET /user/_search
{
"query":{
"function_score":{
"query":{
"match":{
"introduce":"happens"
}
},
"functions":[
{
"filter":{
"range":{
"score":{
"gte":90
}
}
},
"weight":2
}
],
"boost_mode":"sum"
}
}
}
组合匹配
GET /user/_search
{
"query": {
"bool": {
"must": [
{
"range": {
"score": {
"gte": 90
}
}
},
{
"bool": {
"should": [
{
"term": {
"gender": "0"
}
},
{
"match": {
"addr": "北京市"
}
}
]
}
}
],
"should": [],
"must_not": [
{
"range":{
"score":{
"gte":91
}
}
}
],
"filter": [
{
"term":{
"gender":"1"
}
}
]
}
}
}
常见参数设置
分页
GET /user/_search
{
"query": {
"match_all": {}
},
"from": 0,
"size": 10
}
排序
GET /user/_search
{
"query":{
"match_all":{}
},
"sort":[
{
"score":"desc"
}
]
}
高亮
GET /user/_search
{
"query":{
"match":{
"introduce":"if"
}
},
"highlight":{
"require_field_match":false,
"fields":{
"introduce":{
"pre_tags":"<em>",
"post_tags":"</em>"
},
"addr":{
"pre_tags":"<em>",
"post_tags":"</em>"
}
}
}
}
常用聚合类型
等值聚合
GET /user/_search
{
"query": {
"match_all": {}
},
"aggs": {
"score_agg": {
"terms": {
"field": "score",
"size": 102
},
"aggs": {
"gender_agg": {
"terms": {
"field": "gender",
"size": 2
}
}
}
}
}
}
平均值聚合
GET /user/_search
{
"query":{
"match_all":{}
},
"aggs":{
"gender_agg":{
"terms":{
"field":"gender",
"size":2
},
"aggs":{
"score_avg":{
"avg":{
"field":"score"
}
}
}
}
}
}
和值聚合
GET /user/_search
{
"query": {
"match_all": {}
},
"aggs": {
"gender_agg": {
"terms": {
"field": "gender",
"size": 2
},
"aggs": {
"score_agg": {
"sum": {
"field": "score"
}
}
}
}
}
}
最小值聚合
GET /user/_search
{
"query": {
"match_all": {}
},
"aggs": {
"gender_agg": {
"terms": {
"field": "gender",
"size":2
},
"aggs":{
"birth_gender":{
"min":{
"field":"birth"
}
}
}
}
}
}
最大值聚合
GET /user/_search
{
"query": {
"match_all": {}
},
"aggs": {
"gender_agg": {
"terms": {
"field": "gender",
"size": 2
},
"aggs": {
"score_agg": {
"max": {
"field": "score"
}
}
}
}
}
}