ES 8.0 默认把type给去掉了
新增/编辑
PUT /index/id 幂等操作 必须指定id 同一个id为修改
POST /index/id 非幂等操作 指定id时和put操作一样 不指定id 每次都会新增 id为系统随机分配
删除
DELETE /index 删除整个索引
DELETE /index/_doc/id 删除指定document
查询
GET /index/_search 不带条件查询所有
GET /index/_doc/id 查询指定document
复杂查询练习
批量导入一批数据
POST /atguigu/_bulk
{"index":{"_id":1}}
{ "title":"小米手机", "images":"http://image.jd.com/12479122.jpg", "price":1999, "stock": 200, "attr": { "category": "手机", "brand": "小米" } }
{"index":{"_id":2}}
{"title":"超米手机", "images":"http://image.jd.com/12479122.jpg", "price":2999, "stock": 300, "attr": { "category": "手机", "brand": "小米" } }
{"index":{"_id":3}}
{ "title":"小米电视", "images":"http://image.jd.com/12479122.jpg", "price":3999, "stock": 400, "attr": { "category": "电视", "brand": "小米" } }
{"index":{"_id":4}}
{ "title":"小米笔记本", "images":"http://image.jd.com/12479122.jpg", "price":4999, "stock": 200, "attr": { "category": "笔记本", "brand": "小米" } }
{"index":{"_id":5}}
{ "title":"华为手机", "images":"http://image.jd.com/12479122.jpg", "price":3999, "stock": 400, "attr": { "category": "手机", "brand": "华为" } }
{"index":{"_id":6}}
{ "title":"华为笔记本", "images":"http://image.jd.com/12479122.jpg", "price":5999, "stock": 200, "attr": { "category": "笔记本", "brand": "华为" } }
{"index":{"_id":7}}
{ "title":"荣耀手机", "images":"http://image.jd.com/12479122.jpg", "price":2999, "stock": 300, "attr": { "category": "手机", "brand": "华为" } }
{"index":{"_id":8}}
{ "title":"oppo手机", "images":"http://image.jd.com/12479122.jpg", "price":2799, "stock": 400, "attr": { "category": "手机", "brand": "oppo" } }
{"index":{"_id":9}}
{ "title":"vivo手机", "images":"http://image.jd.com/12479122.jpg", "price":2699, "stock": 300, "attr": { "category": "手机", "brand": "vivo" } }
{"index":{"_id":10}}
{ "title":"华为nova手机", "images":"http://image.jd.com/12479122.jpg", "price":2999, "stock": 300, "attr": { "category": "手机", "brand": "华为" } }
# 匹配所有
GET /atguigu/_search
{
"query": {
"match_all": {}
}
}
# 条件匹配 如果是字符串会进行分词拆分 查询出所有的包含分词的数据
GET /atguigu/_search
{
"query": {
"match": {
"title": "小米手机"
}
}
}
# 条件匹配 分词拆分走and查询 查询出包含所有分词的数据
GET /atguigu/_search
{
"query": {
"match": {
"title": {
"query": "小米手机",
"operator": "and"
}
}
}
}
# 子属性匹配
GET /atguigu/_search
{
"query": {
"match": {
"attr.brand": "小米"
}
}
}
# 多字段匹配
GET /atguigu/_search
{
"query": {
"multi_match": {
"query": "小米",
"operator": "and",
"fields": ["title","attr.brand.keyword"]
}
}
}
# 词条查询 精确查询
GET /atguigu/_search
{
"query": {
"term": {
"price": {
"value": "4999"
}
}
}
}
# 范围查询
GET /atguigu/_search
{
"query": {
"range": {
"price": {
"gte": 3000,
"lte": 3999
}
}
}
}
# 组合查询 `bool`把各种其它查询通过`must`(与)、`must_not`(非)、`should`(或)的方式进行组合 一个组合查询里面只能出现一种组合,不能混用
GET /atguigu/_search
{
"query": {
"bool": {
"should": [
{
"range": {
"price": {
"gte": 3000,
"lte": 3999
}
}
},
{
"range": {
"price": {
"gte": 4000,
"lte": 4999
}
}
}
]
}
}
}
组合嵌套查询 should 和must 并列存在 should不生效 所以使用嵌套bool query的方式
GET /atguigu/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "小米手机"
}
},
{
"bool": {
"should": [
{
"range": {
"price": {
"gte": 4000,
"lte": 5000
}
}
},
{
"range": {
"price": {
"gte": 3999,
"lte": 3999
}
}
}
]
}
}
]
}
}
}
# 过滤 过滤条件不想对结果评分产生影响 可以使用过滤对查询结果进行过滤 `filter`中还可以再次进行`bool`组合条件过滤。
GET /atguigu/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "小米"
}
},
{
"match": {
"price": "4999"
}
}
],
"filter": [
{
"range": {
"price": {
"gte": 3000,
"lte": 4999
}
}
}
]
}
}
}
# 排序
GET /atguigu/_search
{
"query": {
"match": {
"title": "小米手机"
}
},
"sort": [
{
"price": {
"order": "desc"
}
},
{
"stock": {
"order": "desc"
}
}
]
}
# 分页
GET /atguigu/_search
{
"query": {
"match": {
"attr.brand": "小米手机"
}
},
"from": 0,
"size": 2
}
# 高亮
GET /atguigu/_search
{
"query": {
"match": {
"attr.category": {
"query": "笔记本",
"operator": "and"
}
}
},
"highlight": {
"fields": {
"attr.category": {}
},
"pre_tags": "<em>",
"post_tags": "</em>"
}
}
# 结果字段过滤
GET /atguigu/_search
{
"_source": ["title","price","attr"],
"query": {"match": {
"price": "3999"
}}
}
GET /atguigu/_search
# 聚合成桶 "size": 0, ## 不关心查询多少条数据 只关心聚合结果
GET /atguigu/_search
{
"size": 0,
"aggs": {
"brands": {
"terms": {
"field": "attr.brand.keyword"
}
}
}
}
# 桶内度量
GET /atguigu/_search
{
"size": 0,
"aggs": {
"brand": {
"terms": {
"field": "attr.brand.keyword"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}文章来源:https://www.toymoban.com/news/detail-762662.html
# 桶内嵌套桶
GET /atguigu/_search
{
"size": 0,
"aggs": {
"brand": {
"terms": {
"field": "attr.brand.keyword"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
},
"categorys": {
"terms": {
"field": "attr.category.keyword"
}
}
}
}
}
}文章来源地址https://www.toymoban.com/news/detail-762662.html
到了这里,关于ElasticSearch 8.11 基本操作练习的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!