多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# elasticsearch语法 ### 相关网站 - [官方文档](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html) - [mapping详解](https://zq99299.github.io/note-book/elasticsearch-core/search-engine/44-mapping-detailed.html) - [检索技巧](https://learnku.com/elasticsearch/t/47787) ### 命令列表 > GET /_cat ### 所有索引信息 > GET /_cat/indices?v > GET /_cat/indices/your_index_prefix*?v=true ### param参数 > [cat.asciidoc](https://github.com/elastic/elasticsearch/blob/main/docs/reference/cat.asciidoc) ```bash GET _cat/indices/so_alert*?format=json&s=creation.date.string:desc&bytes=b&h=health,status,index,uuid,pri,rep,docs.count,docs.deleted,store.size,pri.store.size,creation.date.string ``` ### 字段过滤&排序 ```bash GET /your_index_name/_search?_source=title,name,content&sort=id:desc { "query": { "match_all": {} }, "from": 0, "size": 10 } ``` ### sql查询 ```bash POST _plugins/_sql { "query": "SELECT class_id,alert_rules_name FROM so_alert LIMIT 10" } ``` ### 节点信息(版本) > GET /_nodes ### 查询全部 > GET /your_index_prefix*/_search ```bash GET /your_index_prefix*/_search { "query": { "match_all": {} } } ``` ### 分页查找 ```bash GET /your_index_name/_search { "query": { "match_all": {} }, "from": 0, "size": 10 } ``` ### 滚动分页 ```bash GET /your_index_name/_search?scroll=1m { "query": { "match_all": {} } } ``` ```bash GET /_search/scroll { "scroll": "1m", "scroll_id" : "FGluY2x1ZGVfY29udGV4dF91dWlkDXF1ZXJ5QW5kRmV0Y2gBFG93THVKSVVCZy1tZzVha2xlbEk0AAAAAAACHGQWaDhWTmdjblNRTDJjV1pFZFBBc2cyZw==" } ``` ### 布尔查询 - 嵌套must嵌入should查询:满足且A或B条件 ```bash GET /your_index_name/_search { "query": { "bool": { "must": [ { "match": { "field1": "公共机构" } }, { "bool": { "should": [ { "prefix": { "field2": "52" } }, { "match": { "field2": "" } } ] } } ], "must_not": [ { "match": { "field1": "事业部" } } ] } } } ``` ### 使用operator匹配所有分词 ```bash GET /your_index_name/_search { "query": { "match": { "title": { "query": "搜索引擎知识", "operator": "and" } } } } ``` ### 精确查找 ```bash GET /your_index_name/_search { "query": { "term": { "userName": "jack" } }, "size": 10 } ``` ### 按时间查找 ```bash GET /your_index_name/_search { "query": { "bool": { "filter": [ { "range": { "update_time": { "gte": "2023-05-07 21:18:25" } } } ] } } } ``` ### 聚合查询 ```bash GET /your_index_name/_search { "size": 0, "aggs": { "indexs": { "terms": { "field": "index_code.keyword", "order": { "maxValue": "asc" }, "size": 3 }, "aggs": { "maxValue": { "sum": { "field": "online_count" } } } } } } ``` ### 匹配结果解释分析 ```bash GET /your_index*/_search { "explain": true, "query": { "bool": { "must": [ { "match": { "title": "your keywords" } } ] } } } ``` ### 检索分析 > GET /_analyze ```bash GET /your_index_name/_analyze { "field": "your_field_name", "text": "2022年年度检查报告说明" } ``` ### 设置所有搜索此索引的最大值(默认10000) ```bash PUT /_all/_settings?preserve_existing=true { "index.max_result_window": "100000" } ``` ### index数据量 > GET /your_index_name/_count ### index是否存在 > HEAD /your_index_name ### 获取index的mapping > GET /your_index_name > GET /your_index_name/_mapping ### 复制index ```bash POST /_reindex { "source": { "index": "your_index_a" }, "dest": { "index": "your_index_b" } } ``` ### 通过mapping创建index > [设置settings](https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html) ```bash PUT /your_index_name { "settings": { "number_of_shards": 1, "number_of_replicas": 0, "index.max_result_window": "1000000" }, "mappings": { "properties": { "id": { "type": "long" }, "alert_id": { "type": "long" }, "created_by": { "type": "keyword" }, "created_at": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd'T'HH:mm:ssZ||yyyy-MM-dd'T'HH:mm:ss.SSSZ||yyyy-MM-dd||yyyy/MM/dd||epoch_second", "ignore_malformed": true }, "updated_by": { "type": "keyword" }, "updated_at": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd'T'HH:mm:ssZ||yyyy-MM-dd'T'HH:mm:ss.SSSZ||yyyy-MM-dd||yyyy/MM/dd||epoch_second", "ignore_malformed": true }, "time": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd'T'HH:mm:ssZ||yyyy-MM-dd'T'HH:mm:ss.SSSZ||yyyy-MM-dd||yyyy/MM/dd||epoch_second", "ignore_malformed": true } } } } ``` ### 删除index > DELETE /your_index_name ### 模板操作 - 查看所有模板 ```bash GET /_cat/templates ``` ```bash GET /_cat/templates/myhello* ``` - 查看指定模板 ```bash GET /_index_template/myhello_template ``` - 删除指定模板 ```bash DELETE _index_template/myhello_template ``` - 查看具体模板配置 ```bash GET /_index_template/myhello_template ``` - 新增/更新模板 ```bash PUT /_index_template/myhello_template { "index_patterns": [ "myhello_*" ], "template": { "settings": { "number_of_shards": 1, "number_of_replicas": 0, "index.max_result_window": "1000000" }, "mappings": { "properties": { "id": { "type": "long" }, "alert_id": { "type": "long" }, "created_by": { "type": "keyword" }, "created_at": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd'T'HH:mm:ssZ||yyyy-MM-dd'T'HH:mm:ss.SSSZ||yyyy-MM-dd||yyyy/MM/dd||epoch_second", "ignore_malformed": true }, "updated_by": { "type": "keyword" }, "updated_at": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd'T'HH:mm:ssZ||yyyy-MM-dd'T'HH:mm:ss.SSSZ||yyyy-MM-dd||yyyy/MM/dd||epoch_second", "ignore_malformed": true } } } } } ``` ### 解除索引只读状态问题 ```bash GET /your_index_name/_settings?pretty ``` 返回的数据 ```json { "your_index_name" : { "settings" : { "index" : { "number_of_shards" : "1", "blocks" : { "read_only_allow_delete" : "true" }, "provided_name" : "your_index_name", "creation_date" : "1700202922646", "number_of_replicas" : "1", "uuid" : "cJwxTNOaQDuwpYSXkWxqIA", "version" : { "created" : "135248327" } } } } } ``` > 如果出现 `"read_only_allow_delete" : "true"`,则表示该索引只读 解除所有索引只读状态 ```bash PUT /_all/_settings { "settings": { "index.blocks.read_only_allow_delete": null } } ```