1. 首页
  2. 课程学习
  3. 数据库
  4. es_script.txt

es_script.txt

上传者: 2025-05-25 11:55:34上传 TXT文件 7.48KB 热度 3次
根据提供的文件信息,我们可以归纳出以下相关知识点: ### 一、Elasticsearch索引映射配置 #### 1. 索引名称:`g3-cbgw-test` 该索引用于存储与交易相关的数据。 - **_doc**:文档类型。 - **TradeType**:交易类型,关键字类型(`keyword`),主要用于分类或过滤。 - **Logdate**:日志日期时间,日期类型(`date`),支持多种日期格式: - `yyyy-MM-dd HH:mm:ss Z`:如 `2019-11-15 12:00:00 +0800` - `epoch_millis`:毫秒时间戳 - `yyyy-MM-dd Z`:如 `2019-11-15 +0800` - **Status**:状态码,关键字类型(`keyword`),通常表示交易的状态。 - **Duration**:持续时间,整型(`integer`),用于记录交易执行的时间长度。 - **Province**:省份,关键字类型(`keyword`),用于表示交易发生的地理位置。 - **ErrCode**:错误代码,关键字类型(`keyword`),用于记录交易过程中出现的错误。 - **ErrMsg**:错误消息,关键字类型(`keyword`),用于记录具体的错误信息。 - **Bet**:投注信息,对象类型,包含以下子字段: - **SportsCode**:运动编码,关键字类型(`keyword`),用于标识投注所涉及的体育项目。 - **Leg**:投注组合,嵌套类型(`nested`),每个元素代表一个投注组合项,包含: - **Cond**:条件,关键字类型(`keyword`),表示投注条件。 - **Amount**:金额,双精度浮点类型(`double`),表示投注金额。 #### 2. 示例:创建索引 ```bash curl -XPUT "http://10.0.1.69:9200/g3-cbgw-test?pretty" -H "Content-Type: application/json" -d '{ "mappings": { "_doc": { "properties": { "TradeType": { "type": "keyword" }, "Logdate": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss Z||epoch_millis||yyyy-MM-dd Z" }, "Status": { "type": "keyword" }, "Duration": { "type": "integer" }, "Province": { "type": "keyword" }, "ErrCode": { "type": "keyword" }, "ErrMsg": { "type": "keyword" }, "Bet": { "properties": { "SportsCode": { "type": "keyword" }, "Leg": { "type": "nested", "properties": { "Cond": { "type": "keyword" }, "Amount": { "type": "double" } } } } } } } } }' ``` ### 二、操作文档 #### 1. 创建文档 ```bash curl -XPUT "http://10.0.1.69:9200/g3-cbgw-test/_doc?pretty" -H "Content-Type: application/json" -d '{ "TradeType": "SellReply", "Logdate": "2019-11-15 12:00:00 +0800", "Status": "0", "Duration": 1, "Province": "", "Bet": { "SportsCode": "FB", "Leg": [ { "Cond": "+1.00", "Amount": "32.00" }, { "Cond": "+2.00", "Amount": "2.00" }, { "Cond": "+2.00", "Amount": "100.00" } ] } }' ``` #### 2. 更新文档 使用Painless脚本进行更新,可以对现有文档进行更灵活的操作。 - **更新Duration** ```bash curl -XPOST "http://10.0.1.69:9200/g3-cbgw-test/_doc/1/_update?pretty" -H "Content-Type: application/json" -d '{ "script": { "source": "ctx._source.Duration += params.count", "lang": "painless", "params": { "count": 60 } } }' ``` - **更新ProvinceInfo** ```bash curl -XPOST "http://10.0.1.69:9200/g3-cbgw-test/_doc/1/_update?pretty" -H "Content-Type: application/json" -d '{ "script": { "source": "ctx._source.ProvinceInfo = ctx._source.Province + params.group", "lang": "painless", "params": { "group": "一" } } }' ``` - **向Bet.Leg添加新项** ```bash curl -XPOST "http://10.0.1.69:9200/g3-cbgw-test/_doc/1/_update?pretty" -H "Content-Type: application/json" -d '{ "script": { "source": "ctx._source.Bet.Leg.add(params.Leg)", "lang": "painless", "params": { "Leg": { "Cond": "+3.00", "Amount": "50.00" } } } }' ``` ### 三、删除索引 - 删除`book`索引 ```bash curl -XDELETE "http://10.0.1.69:9200/book?pretty" ``` - 删除`g3-cbgw-test`索引 ```bash curl -XDELETE "http://10.0.1.69:9200/g3-cbgw-test?pretty" ``` 以上命令展示了如何通过REST API使用curl命令行工具来管理Elasticsearch中的索引和文档。这些操作包括创建索引和文档、更新文档以及删除索引等基本操作,是日常管理和维护Elasticsearch实例时非常重要的技能之一。
下载地址
用户评论