记知识先记轮廓,关于DSL语法的轮廓,记住以下三句话即可:
使用Elasticsearch时,我们一般是调用RestClient API的方式读取和写入集群数据。有时也会使用工具查阅和操作数据,比如:使用Chrome插件Multi Elasticsearch Head或者Cerebro、Kibana。笔者建议使用Kibana的方式操作集群数据,使用Multi Elasticsearch Head或者Cerebro从整体上观察集群。
既然是操作集群数据,那就绕不开ES的DSL语法 — 一个让人又爱又恨的语法。
本文整理了一些常用DSL语法,方便记忆,分了如下几类:操作索引、操作文档、Match查询、Term查询、查看分词。如果碰到复杂查询还是建议查阅官网。
在Kibana上操作ES数据的方式如下:
(1) 创建索引:
PUT /goods{ "mappings": { "properties": { "brandName": { "type": "keyword" }, "categoryName": { "type": "keyword" }, "createTime": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss" }, "id": { "type": "keyword" }, "price": { "type": "double" }, "saleNum": { "type": "integer" }, "status": { "type": "integer" }, "stock": { "type": "integer" }, "title": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart" } } }, # 根据情况选择是否要修改 "settings": { "number_of_shards": 2, "number_of_replicas": 2 }}
(2) 删除索引:
DELETE goods
(3) 重建索引
有些场景下需要重建索引,比如修改了Mapping,重建步骤如下:
POST _reindex { "source": { "index": "goods" }, "dest": { "index": "goods1" }}DELETE goodsPOST _reindex { "source": { "index": "goods1" }, "dest": { "index": "goods" }}DELETE goods1
(1) 创建文档
# 这种方式,同样的id无法重新创建PUT goods/_create/1{ "id": 1, "brandName": "Apple", "categoryName": "手机", "createTime": "2023-10-22 19:12:56", "price": 8799, "saleNum": 599, "status": 0, "stock": 1000, "title": "Apple iPhone 15 Pro 512GB 远峰蓝色 支持移动联通电信5G 双卡双待手机"}# 这种方式,同样的id会覆盖原有的PUT goods/_doc/2{ "id": 2, "brandName": "Apple", "categoryName": "手机", "createTime": "2023-10-22 19:12:56", "price": 8799, "saleNum": 599, "status": 0, "stock": 1000, "title": "Apple iPhone 15 Pro 256GB 远峰蓝色 支持移动联通电信5G 双卡双待手机"}
(2) 更新文档
POST goods/_update/1{ "doc": { "title":"Apple iPhone 13 Pro (A2639) 256GB 远峰蓝色 支持移动联通电信5G 双卡双待手机111" }}
(3) 删除文档
DELETE goods/_doc/2
(4) 获取文档
# 获取单个文档GET goods/_doc/1# 批量获取GET books/_doc/_mget{ "ids": ["1","2"]}
Match查询会对查询内容做分词,然后根据倒排索引去匹配文档。Term查询对查询内容不做分词,直接去倒排索引里去匹配文档。
(1) 查询所有
POST goods/_search{ "query": { "match_all": { } }}
(2) match_phrase短语查询
POST goods/_search{ "query": { "match_phrase": { "title": "支持" } }}
(3) 匹配查询
POST goods/_search{ "query": { "match": { "title": "移动多余" } }}
(4) 模糊匹配查询
POST goods/_search{ "query": { "wildcard": { "title": { "value": "*鞋" } } }}
Term查询对查询内容不做分词,直接去倒排索引里去匹配文档。
POST goods/_search{ "query": { "term": { "title": { "value": "手机" } } }}# 匹配多个termPOST goods/_search{ "query": { "terms": { "title": [ "双卡", "待" ] } }}
复杂查询基本会用到bool关键字。
(1) bool + must
# 布尔查询,可以组合多个过滤语句来过滤文档POST goods/_search{ "query": { "bool": { "must": [ { "term": { "title": { "value": "Wolfgang Mauerer" } } }, { "term": { "date": { "value": "2010-06-01" } } } ] } }}# 匹配多个字段GET product/_search{ "query": { "bool": { "must": [ { "match_phrase": { "name": "连衣裙" } }, { "match_phrase": { "en_intro": "korean" } }, { "match_phrase": { "intro": "御姐" } } ] } }}
(2) bool + filter + range
POST books/_search{ "query": { "bool": { "must": [ { "term": { "author": { "value": "Wolfgang Mauerer" } } } ], "filter": [ { "term": { "date": { "value": "2010-06-01" } } } ] } }}POST goods/_search{ "query": { "bool": { "must": [ { "match": { "title": "华为" } } ], "filter": [ { "range": { "price": { "gte": 5000, "lte": 10000 } } } ] } }}
(1) Scroll分页
# 第一次使用 scroll APIPOST goods/_search?scroll=2m{ "query": { "match_all": {} }, "size": 2}# 进行翻页POST /_search/scroll { "scroll" : "2m", "scroll_id" : "FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFkxBWkYwOGw2U1dPSF94aHZTelFkaWcAAAAAAAADHhZoU05ERFl3WFIycXM3M3JKMmRQVkJB" }
(2) from + size分页
POST goods/_search{ "query": { "match_all": { } }, "from": 6, "size": 2, "sort": [ { "price": { "order": "asc" } } ]}
(1) 最大、最小、平均
POST goods/_search{ "aggs": { "avg_price": { "avg": { "field": "price" } } }}POST goods/_search{ "aggs": { "min_price": { "min": { "field": "price" } } }}POST goods/_search{ "aggs": { "max_price": { "max": { "field": "price" } } }}
(2) 范围查询
POST goods/_search{ "query": { "range": { "price": { "gte": 10, "lte": 20 } } }}
(3) 高亮查询
POST goods/_search{ "query": { "match": { "title": "跑鞋" } }, "highlight": { "fields": { "body": { "pre_tags": [ "<font color='red'>" ], "post_tags": [ "</font>" ] }, "title": {} } }}
(4) 分组查询
POST goods/_search{ "aggs": { "brandNameName": { "terms": { "field": "brandName" } } }}
(5) 子查询
POST goods/_search{ "aggs": { "brandNameName": { "terms": { "field": "brandName" }, "aggs": { "avgPrice": { "avg": { "field": "price" } } } } }}
相对一些分析进行分析时,看看ES怎么拆分的,可以用这个查看。
POST _analyze{ "analyzer": "standard", "text": "Linus 在90年代开发出了linux操作系统"}POST _analyze{ "analyzer": "ik_max_word", "text": "Linus 在90年代开发出了linux操作系统" }POST _analyze{ "analyzer": "ik_smart", "text": "Linus 在90年代开发出了linux操作系统" }POST _analyze{ "analyzer": "ik_smart", "text": "中华人民共和国国歌" }POST _analyze{ "analyzer": "ik_max_word", "text": "中华人民共和国国歌" }
本文主要介绍了常见DSL的用法,主要是帮助记忆,避免一些基本的操作还要去查询文档的尴尬。记住以下3句话,即可记住DSL的轮廓了:
本文链接:http://www.28at.com/showinfo-26-15616-0.html巧记Elasticsearch常用DSL语法
声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。邮件:2376512515@qq.com
上一篇: 用示例和应用程序了解必要的Golang库