ES分布式搜索-索引库操作

索引库操作

1、mapping映射属性

可以查看官方文档学习:ES官方手册

mapping是对索引库中文档的约束,常见的mapping属性包括:

  • type:字段数据类型,常见的简单类型有:

    • 字符串:text(可分词的文本)、keyword(精确值,例如:品牌、国家、ip地址)

    • 数值:long、integer、short、byte、double、float、

    • 布尔:boolean

    • 日期:date

    • 对象:object
  • index:是否创建索引,默认为true
  • analyzer:使用哪种分词器(绝大多数情况下分词text即可)
  • properties:该字段的子字段

2、创建索引库

ES中通过Restful请求操作索引库、文档。请求内容用DSL语句来表示。创建索引库和mapping的DSL语法如下:

JSON 复制代码
PUT /索引库名称
{
  "mappings": {
    "properties": {
      "字段名":{
        "type": "text",
        "analyzer": "ik_smart"
      },
      "字段名2":{
        "type": "keyword",
        "index": "false"
      },
      "字段名3":{
        "properties": {
          "子字段": {
            "type": "keyword"
          }
        }
      },
      // ...略
    }
  }
}

例如:

json 复制代码
# 创建名为rediaz的索引库
PUT /rediaz
{
  "mappings": {
    "properties": {
      "info": {
        "type": "text",
        "analyzer": "ik_smart"
      },
      "email": {
        "type": "keyword",
        "index": false  //不会创建索引,不会被搜索
      },
      "name": {
        "type": "object",
        "properties": {
          "firstname": {
            "type": "keyword"
          },
          "lastname": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

索引库创建结果:

json 复制代码
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "rediaz"
}

3、查询、删除和修改索引库

  1. 查询:

    json 复制代码
    GET /索引库名

    示例:

    josn 复制代码
    GET /rediaz
  2. 删除:

    json 复制代码
    DELETE /索引库名

    示例:

    json 复制代码
    DELETE /rediaz
  3. 修改:

    在ES中禁止修改索引库:在索引库创建成功之后,其数据结构(即mapping映射)已经创建好了,ES会基于mapping来创建倒排索引,如果修改索引库,则会将ES库中的所有倒排索引失效。

    但是可以添加新的字段:

    json 复制代码
    PUT /索引库名/_mapping
    {
    "properties" : {
      "新字段名":{
        "type" : "integer "}
      }
    }

    示例:

    json 复制代码
    PUT /rediaz/_mapping
    {
    "properties" : {
      "age":{
        "type" : "integer"}
      }
    }
相关推荐
SelectDB9 小时前
Apache Doris Python UDF:让 SQL 直接调用 Python 生态,支撑 Agent 时代复杂业务逻辑
大数据·数据库·python
ApacheSeaTunnel12 小时前
当多表数据涌入,Apache SeaTunnel 如何巧妙化解主键冲突?
大数据·开源·数据集成·seatunnel·技术分享·数据同步
Elasticsearch16 小时前
使用 Elastic Agent Builder 和 Sarvam AI 构建多语言语音 agent
elasticsearch
大大大大晴天3 天前
Hudi Metadata Table 与 Hive Sync (HMS)怎么选?
大数据
手可摘星辰7774 天前
一次线上FlinkCDC异常排查复盘
大数据·flink
大大大大晴天4 天前
Hudi技术内幕:Metadata Table原理与实践
大数据
武子康5 天前
调查研究-197 FAISS vs Elasticsearch 全面对比:从向量检索、全文搜索到 RAG 选型指南
人工智能·elasticsearch·agent
大大大大晴天5 天前
Hudi技术内幕:深入解析Index索引机制
大数据
阿里云大数据AI技术5 天前
Flink Forward Asia 2026 深圳启幕:Agentic Streaming for AI,开启实时智能新范式
大数据·flink
SelectDB5 天前
阶跃星辰基于 SelectDB 构建 PB 级 Agent 可观测平台
大数据·数据库·aigc