Elasticsearch7.5.2 常用rest api与elasticsearch库

目录

[一、rest api](#一、rest api)

[1. 新建索引](#1. 新建索引)

[2. 删除索引](#2. 删除索引)

[3. 插入单条数据](#3. 插入单条数据)

[4. 更新单条数据](#4. 更新单条数据)

[5. 删除单条数据](#5. 删除单条数据)

[6. 查询数据](#6. 查询数据)

[二、python elasticsearch库](#二、python elasticsearch库)

[1. 新建索引](#1. 新建索引)


一、rest api

1. 新建索引

请求方式:PUT

请求URL:http://ip/(your_index_name)

示例URL:http://ip/test_log_20240710-0

数据参数:

python 复制代码
params = {
    "settings": {
        "number_of_shards": 1,
        "number_of_replicas": 0
    },
    "aliases": {
        "test_log":{}
    },
    "mappings": {
        "properties": {
            "create_time": {
                "type": "date"
            },
            "status":{"type": "integer"},
            "dev_ip":{"type": "ip"},
            "dev_uuid":{"type": "keyword"},
            "user": {
                "properties": {
                    "name": {
                        "type": "text"
                    },
                    "age": {
                        "type": "integer"
                    },
                    "email": {
                        "type": "keyword"
                    }
                }
            },
            "infos": {
                "type": "nested",
                "dynamic": false,
                "properties": {
                    "v_id": {"type": "keyword"},
                    "v_name": {"type": "keyword"},
                    "v_desc": {"type": "text"}
                }
            },
            "remark": {"type": "text"},
            "show_num": {"type": "long", "doc_values": false, "index": false}
        }
    }
}


"""
字段类型:

text:用于全文本搜索,如文章内容。text字段会被分析器分词,支持模糊搜索和前缀搜索。
keyword:用于不进行分词的搜索,适合存储关键词、ID、标签等。keyword字段不会被分词,适用于精确匹配和聚合。
integer/long/short/byte/double/float/half_float/scaled_float:用于数值类型的数据,不同的类型对应不同的数值范围和精度。
boolean:布尔型字段,值只能是true或false。
binary:用于存储二进制数据,如图像或文件。
date:日期时间字段,可以使用ISO8601格式的字符串或毫秒级的时间戳。
date_nanos:纳秒精度的日期时间字段,存储为纳秒时间戳。
ip:用于存储IPv4或IPv6地址。
object:用于嵌套的JSON对象。可以定义内部字段的映射。
nested:用于存储复杂结构的嵌套文档,允许对嵌套文档进行独立索引和搜索。
geo_point:用于地理坐标,支持基于地理位置的查询。
geo_shape:用于地理形状,如多边形,支持更复杂的地理查询。
completion:用于自动补全功能,存储经过分析的词条列表。
constant_keyword:与keyword类似,但值在索引时会被复制到所有分片,加速聚合操作。
token_count:用于存储分词后的词数。
"""

2. 删除索引

请求方式:DELETE

请求URL:http://ip/(your_index_name)

示例URL:http://ip/test_log_20240710-0

3. 插入单条数据

请求方式:POST

请求URL:http://ip/(your_index_name)

示例URL:http://ip/test_log_20240710-0

数据参数:

python 复制代码
{
  "create_time": 1720601022255,
  "status": 201,
  "dev_ip": "192.168.1.101",
  "dev_uuid": "123e4567e89b12d3a456426614174000",
  "user": {
    "name": "战三",
    "age": 30,
    "email": "[email protected]"
  },
  "infos": [
    {
      "v_id": "123e4567e89b12d3a456426614174000",
      "v_name": "战三",
      "v_desc": "描述啦啦啦啦啦啦"
    }
  ],
  "remark": "描述!!!!!",
  "show_num": 6789
}

4. 更新单条数据

请求方式:PUT

请求URL:http://ip/(your_index_name)/_doc/(本条记录id)

示例URL:http://ip/test_log_20240710-0/_doc/KjjOm5ABJ5wlHmqgfJvm

数据参数:

python 复制代码
{
  "create_time": "2023-04-01T12:00:00Z",
  "status": 200,
  "dev_ip": "192.168.1.100",
  "dev_uuid": "123e4567-e89b-12d3-a456-426614174000",
  "user": {
    "name": "阿汉",
    "age": 30,
    "email": "[email protected]"
  },
  "infos": [
    {
      "v_id": "info1",
      "v_name": "Info One",
      "v_desc": "This is the description of info one."
    },
    {
      "v_id": "info2",
      "v_name": "Info Two",
      "v_desc": "This is the description of info two."
    }
  ],
  "remark": "Additional remarks about this log entry.",
  "show_num": 12345
}

5. 删除单条数据

请求方式:DELETE

请求URL:http://ip/(your_index_name)/_doc/(本条记录id)

示例URL:http://ip/test_log_20240710-0/_doc/KjjOm5ABJ5wlHmqgfJvm

数据参数:无

6. 查询数据

请求方式:POST

请求URL:http://ip/(your_index_name)/_search

示例URL:http://ip/test_log_20240710-0/_search

数据参数:

python 复制代码
{
    "size": 10,
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "status": 200
                    }
                }
            ]
        }
    }
}

二、python elasticsearch库

1. 新建索引

......

相关推荐
Lalolander1 分钟前
装备制造企业选型:什么样的项目管理系统最合适?
大数据·制造·工程项目管理·装备制造·epc项目管理·项目成本管控·业财一体化
Debug_TheWorld24 分钟前
Elasticsearch学习
elasticsearch
island13141 小时前
【git#4】分支管理 -- 知识补充
大数据·git·elasticsearch
LCHub低代码社区1 小时前
钧瓷产业原始创新的许昌共识:技术破壁·产业再造·生态重构(一)
大数据·人工智能·维格云·ai智能体·ai自动化·大禹智库·钧瓷码
晴天彩虹雨2 小时前
Flink 数据清洗与字段标准化最佳实践
大数据·数据仓库·flink
一个数据大开发2 小时前
解读《数据资产质量评估实施规则》:企业数据资产认证落地的关键指南
大数据·数据库·人工智能
TTBIGDATA2 小时前
如何将 Apache Hudi 接入 Ambari?完整部署与验证指南
大数据·hadoop·ambari·hudi·bigtop·湖仓·自定义组件集成
科技在线3 小时前
科技赋能建筑新未来:中建海龙模块化建筑产品入选中国建筑首批产业化推广产品
大数据·人工智能
24k小善4 小时前
Flink TaskManager详解
java·大数据·flink·云计算
时序数据说4 小时前
时序数据库IoTDB在航空航天领域的解决方案
大数据·数据库·时序数据库·iotdb