企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
>[info] es对于restful的基本操作 一种软件架构风格,而不是标准,只是提供了一组设计原则和约束条件。它主要用于客户端和服务器交 互 类的软件。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。 ***** **基本Rest命令说明:** | method | url地址 | 描述 | | --- | --- | ---- | | PUT | localhost:9200/索引名称/类型名称/文档id | 创建文档(指定文档id) | | POST | localhost:9200/索引名称/类型名称 | 创建文档(随机文档id)| | DELETE | localhost:9200/索引名称/类型名称/文档id | 删除文档 | | GET | localhost:9200/索引名称/类型名称/文档id | 查询文档通过文档id | | POST | localhost:9200/索引名称/类型名称/_search | 查询所有数据 | | POST | localhost:9200/索引名称/类型名称/_update | 更新数据 | **字段类型说明:** * **字符串类型:** text 、 keyword * **数值类型:** long, integer, short, byte, double, float, half_float, scaled_float | 类型 | 长度(字节) | 范围 | 默认值 | 说明 | | --- | --- | ---- | ---- | ---- | | byte | 8 | -128 ~ 127 | | 有符号 | | short | 16 | -32768 ~ 32767 | | 有符号 | | integer | 32 | -21亿 ~ 21亿 | | 有符号 | | long | 64 | | | 有符号 | | float | 32 | | | 单精度浮点数 | | double | 64| | | 双精度浮点数 | | half_float | 16 | | | 半精度浮点数 | | scaled_float | | | | 缩放类型的浮点数(如:price字段只需要精确到分,57.34缩放因子为100,存储结果为:5734) | * **日期类型:** date PUT /索引名/~类型名~/文档id {请求体} * **布尔值类型:** boolean 二进制类型 binary * **其他:** **可以使用 restful 风格:** ``` curl -XPUT http://ip:9200/test2/type2/2 '{}' 等 ``` >[info] 代码高亮 highlight:代码高亮 fields:指定字段 pre_tags:自定义标签开始 post_tags:自定义标签结束 ``` GET /test1/type1/_search { "query": { "match": { "name": "初心" } }, "highlight": { "pre_tags": "<span style='color:red;'>", "post_tags": "</span>", "fields": { "name":{} } } } ``` >[info] 索引 * **查询所有索引** ``` GET /_cat/indices?v ``` * **创建索引** ``` PUT /shop ``` * **删除索引** 通过DELETE 命令实现删除、 根据你的请求来判断是删除索引还是删除文档记录! ``` DELETE /shop ``` * **获取当前/全部索引 mapping setting 信息** ``` GET _all/_mapping GET _all/_settings GET /shop/_mapping GET /shop/_settings ``` >[info] 基本使用 * **创建索引/类型/数据(自动识别类型)** ``` PUT /test1/type1/1 { "name":"初心", "age":18 } ``` **在 es-head 控制端查看创建的索引:** ![](https://img.kancloud.cn/52/2d/522dd2f27ec41b3fbbafd050b157d121_623x271.png) ![](https://img.kancloud.cn/74/c8/74c83cc2e6d916a89782ecdc225bb329_927x312.png) ***** * **指定字段的类型:** ``` PUT /test1 { "mappings": { "properties": { "name":{ "type":"text" }, "aget":{ "type":"long" }, "birthdy":{ "type":"date" } } } } # 添加数据集 POST /test1/_doc/1 { "id":"1", "aget":18, "birthdy":"2023-10-21", "name":"张三" } ``` ![](https://img.kancloud.cn/6b/4d/6b4d01ba0e13c4a80676ef92ada503d6_1258x473.png) ![](https://img.kancloud.cn/91/27/9127e3d9b0f93af38c18d24e80039aa4_881x280.png) * **创建索引对应的mapping(如数据库 schema 组织和结构) 和 setting(配置)** ``` # 字段说明: // id 字段自增id // good_sn 商品SKU // good_name 商品名称 // good_introduction 商品简介 // good_descript 商品详情 PUT /shop { "mappings": { "properties": { "id": { "type": "long", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "good_sn": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "good_name": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "good_introduction": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "good_descript": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } }, "settings": { "index": { "number_of_shards": 5, "number_of_replicas": 1 } } } ```