索引和文档操作

索引操作

1.创建索引

PUT /test1/_doc/2
{
  "name": "桃子",
  "age": 18
}

GET test1 #可以看到有默认类型

2.指定类型,就像sql创建表

PUT /test2
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "age": {
        "type": "long"
      },
      "birthday": {
        "type": "date"
      }
    }
  }
}

3.其他命令

_cat es当前很多信息

GET _cat/health #查询健康值
GET _cat/indices?v #获取所有版本信息

4.put修该,覆盖

# 覆盖了原来id=1的值
PUT /test3/_doc/1
{
  "name": "桃桃123",
  "age": 28
}

5.post修改

POST test3/_doc/2/_update
{
  "doc": {//doc 固定写法
    "name": "靳朋永123"
  }
}

6.删除

  • 删除索引:DELETE 索引名
  • 删除文档:DELTETE 索引名/类型名/文档id

文档操作

添加数据
PUT test1/_doc/1
{
  "name": "靳朋永",
  "age": 30,
  "desc": "一顿操作猛如虎,一看工资2500",
  "tags": ["游戏","指南","程序员"]
}
查询数据
GET test1/_doc/1
更新数据,put或update
#put更新
PUT test1/_doc/1
{
  "name": "靳朋永123",
  "age": 30,
  "desc": "一顿操作猛如虎,一看工资2500",
  "tags": ["游戏","指南","程序员"]
}

#update更新,不加后面的update,就更直接put更新一样,别的字段会置为空
POST test1/_doc/1/_update
{
  "doc": {
    "name": "靳朋永123"
  }
}

简单搜索
GET test1/_doc/1
GET test1/_search?q=name:靳朋永
复杂查询
  • select *

    hits里有:

    • 索引和文档信息
    • 结果条数
    • score 权重
    • 文档具体内容
    GET test1/_search
    {
      "query": {
        "match": {
          "desc": "狂徒"
        }
      }
    }
    
    #结果
        "hits" : [
          {
            "_index" : "test1",
            "_type" : "jpy",
            "_id" : "3",
            "_score" : 0.6077959,
            "_source" : {
              "name" : "张三",
              "age" : 41,
              "desc" : "法外狂徒",
              "tags" : [
                "律师",
                "渣男"
              ]
            }
          },
    
  • 结果过滤,查询出的结果只要某些字段

    GET test1/_search
    {
      "query": {
        "match": {
          "desc": "狂徒"
        }
      },
      "_source": ["name","desc"]//要查询的字段
    }
    
    #结果:
        "hits" : [
          {
            "_index" : "test1",
            "_type" : "jpy",
            "_id" : "3",
            "_score" : 0.6077959,
            "_source" : {
              "name" : "张三",
              "desc" : "法外狂徒"
            }
          },
          
    
  • 排序

    GET test1/_search
    {
      "query": {
        "match": {
          "desc": "狂徒"
        }
      },
      "sort": [
        {
          "age": {//字段名
            "order": "desc"//升序or降序
          }
        }
      ]
    }
    
  • 分页

    GET test1/_search
    {
      "query": {
        "match": {
          "desc": "狂徒"
        }
      },
      "sort": [
        {
          "age": {
            "order": "desc"
          }
        }
      ],
      "from": 0,//从第几条开始
      "size": 2//每页几条
    }
    
  • 布尔查询

    must相当于and,所有条件都要符合

    must_not相当于not 不是什么什么

    should相当于or

    GET test1/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "name": "胡歌"
              }
            },
            {
              "match": {
                "desc": "法外狂徒"
              }
            }
          ]
        }
      }
    }
    
  • filter过滤

    #过滤年龄>10的胡歌
    GET test1/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "name": "胡歌"
              }
            }
          ],
          "filter": [
            {
              "range": {
                "age": {
                  "lt": 10
                }
              }
            }
          ]
        }
      }
    }
    
  • 匹配多个

    #查询desc字段有狂徒或者女的,多个空格隔开
    GET test1/_search
    {
      "query": {
        "match": {
          "desc": "狂徒 女"
        }
      }
    }
    
  • 精确查询

    term查询是直接通过倒排索引指定的词条进程精确查找的

    trem 直接查询精确的

    match 会使用分词器解析,先分析文档,然后再通过分析的文档进行查询

    两个类型:text keyword

    text:可以被分词器解析

    keyword:会作为一个整体,不会分开

    #多个精确查询
    GET testdb/_search
    {
      "query": {
        "bool": {
          "should": [
            {
              "term": {
                "t1": 12
              }
            },
            {
              "term": {
                "t1": 22
              }
            }
          ]
        }
      }
    }
    
  • 高亮查询

    GET test1/_search
    {
      "query": {
        "match": {
          "name": "胡歌"
        }
      },
      "highlight": {
        "fields": {
          "name": {}
        }
      }
    }
    
    #结果
    .....
    "highlight" : {
              "name" : [
                "<em>胡</em><em>歌</em>"
              ]
            }
    .....
    
    
    #自定义高亮标签
    GET test1/_search
    {
      "query": {
        "match": {
          "name": "胡歌"
        }
      },
      "highlight": {
        "pre_tags": "<p style='color:red'>", 
        "post_tags": "</p>", 
        "fields": {
          "name": {}
        }
      }
    }
    
posted @   jpy  阅读(23)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
点击右上角即可分享
微信分享提示