码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Elasticsearch7.5.2 常用rest api与elasticsearch库


    目录

    一、rest api

    1. 新建索引

    2. 删除索引

    3. 插入单条数据

    4. 更新单条数据

    5. 删除单条数据

    6. 查询数据

    二、python elasticsearch库

    1. 新建索引


    一、rest api

    1. 新建索引

    请求方式:PUT

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

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

     数据参数:

    1. params = {
    2. "settings": {
    3. "number_of_shards": 1,
    4. "number_of_replicas": 0
    5. },
    6. "aliases": {
    7. "test_log":{}
    8. },
    9. "mappings": {
    10. "properties": {
    11. "create_time": {
    12. "type": "date"
    13. },
    14. "status":{"type": "integer"},
    15. "dev_ip":{"type": "ip"},
    16. "dev_uuid":{"type": "keyword"},
    17. "user": {
    18. "properties": {
    19. "name": {
    20. "type": "text"
    21. },
    22. "age": {
    23. "type": "integer"
    24. },
    25. "email": {
    26. "type": "keyword"
    27. }
    28. }
    29. },
    30. "infos": {
    31. "type": "nested",
    32. "dynamic": false,
    33. "properties": {
    34. "v_id": {"type": "keyword"},
    35. "v_name": {"type": "keyword"},
    36. "v_desc": {"type": "text"}
    37. }
    38. },
    39. "remark": {"type": "text"},
    40. "show_num": {"type": "long", "doc_values": false, "index": false}
    41. }
    42. }
    43. }
    44. """
    45. 字段类型:
    46. text:用于全文本搜索,如文章内容。text字段会被分析器分词,支持模糊搜索和前缀搜索。
    47. keyword:用于不进行分词的搜索,适合存储关键词、ID、标签等。keyword字段不会被分词,适用于精确匹配和聚合。
    48. integer/long/short/byte/double/float/half_float/scaled_float:用于数值类型的数据,不同的类型对应不同的数值范围和精度。
    49. boolean:布尔型字段,值只能是true或false。
    50. binary:用于存储二进制数据,如图像或文件。
    51. date:日期时间字段,可以使用ISO8601格式的字符串或毫秒级的时间戳。
    52. date_nanos:纳秒精度的日期时间字段,存储为纳秒时间戳。
    53. ip:用于存储IPv4或IPv6地址。
    54. object:用于嵌套的JSON对象。可以定义内部字段的映射。
    55. nested:用于存储复杂结构的嵌套文档,允许对嵌套文档进行独立索引和搜索。
    56. geo_point:用于地理坐标,支持基于地理位置的查询。
    57. geo_shape:用于地理形状,如多边形,支持更复杂的地理查询。
    58. completion:用于自动补全功能,存储经过分析的词条列表。
    59. constant_keyword:与keyword类似,但值在索引时会被复制到所有分片,加速聚合操作。
    60. token_count:用于存储分词后的词数。
    61. """

    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

    数据参数:

    1. {
    2. "create_time": 1720601022255,
    3. "status": 201,
    4. "dev_ip": "192.168.1.101",
    5. "dev_uuid": "123e4567e89b12d3a456426614174000",
    6. "user": {
    7. "name": "战三",
    8. "age": 30,
    9. "email": "zhansan@example.com"
    10. },
    11. "infos": [
    12. {
    13. "v_id": "123e4567e89b12d3a456426614174000",
    14. "v_name": "战三",
    15. "v_desc": "描述啦啦啦啦啦啦"
    16. }
    17. ],
    18. "remark": "描述!!!!!",
    19. "show_num": 6789
    20. }

    4. 更新单条数据

    请求方式:PUT

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

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

    数据参数:

    1. {
    2. "create_time": "2023-04-01T12:00:00Z",
    3. "status": 200,
    4. "dev_ip": "192.168.1.100",
    5. "dev_uuid": "123e4567-e89b-12d3-a456-426614174000",
    6. "user": {
    7. "name": "阿汉",
    8. "age": 30,
    9. "email": "john.doe@example.com"
    10. },
    11. "infos": [
    12. {
    13. "v_id": "info1",
    14. "v_name": "Info One",
    15. "v_desc": "This is the description of info one."
    16. },
    17. {
    18. "v_id": "info2",
    19. "v_name": "Info Two",
    20. "v_desc": "This is the description of info two."
    21. }
    22. ],
    23. "remark": "Additional remarks about this log entry.",
    24. "show_num": 12345
    25. }

    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

    数据参数:

    1. {
    2. "size": 10,
    3. "query": {
    4. "bool": {
    5. "must": [
    6. {
    7. "term": {
    8. "status": 200
    9. }
    10. }
    11. ]
    12. }
    13. }
    14. }

    二、python elasticsearch库

    1. 新建索引

    ......

  • 相关阅读:
    动态链接那些事
    如何修改运行中的docker容器的端口映射
    项目中用到的git命令(git tag标签全流程)
    一句话代码富集分析gost ghost
    2023年咸阳市《网络建设与运维》赛题解析------四、安全配置
    图像分割算法
    使用JavaScript实现复杂功能:动态数据可视化的构建
    Maven进阶
    【加密算法】RSA和AES在项目中的使用
    clock_property 时钟的常用属性
  • 原文地址:https://blog.csdn.net/a961634066/article/details/140330886
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号