Elasticsearch使用mapping映射定义以及基本的数据类型

1、说明

Elasticsearch的映射相当于数据库的数据字典,它定义了每个字段的名称和能够保存的数据类型,并且内置了20多种字段类型用于支持多种多样的结构化数据,这里仅介绍几种常用的字段类型,如需要了解全部的类型,请参考官方文档的有关介绍。

2、Elasticsearch 映射类型

Elasticsearch 类型 说明
keyword 它用于保存不经过分析、处理的原始文本
text text 这表示用于处理的原始文本,可用于分词
integer 这是一个整型(32位),例如 1、2、3
long 这是一个长整型(64位)
float 这是一个浮点数(32位),例如 1.2 或 4.5
double 这是一个 double 类型浮点数(64位)
boolean 这是一个布尔值:true 或 false
date 表示时间
location 表示经纬度
list和json类型 用{}括起来的是json数据类型,用\[\]是数组类型
binary 用来存储二进制文件,用来保存图片和文件

3、案例介绍

3.1 文本类型text和keyword

text存储会把切分后的文本保存到索引中,如果没有分词一个个字的保存。

keyword 进行统计分析和精准搜索

bash 复制代码
  PUT keyword-test
  {
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      }
    }
  }
}
bash 复制代码
 PUT keyword-test
  {
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword"
      }
    }
  }
}

3.2 时间类型以及数值类型、布尔类型

时间类型,这个字段设置可以接收两种日期格式,其中epoch_millis代表时间戳的毫秒数。注意这里都是UTC时间格式,时间有时区的问题

bash 复制代码
PUT sougoulog-date
{
  "mappings": {
    "properties": {
      "visittime": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss ||epoch_millis"
      }
    }
  }
}

数值类型

bash 复制代码
PUT obj-test
{
  "mappings": {
    "properties": {
      "manager": {
        "properties": {
          "age":  { "type": "integer" }
        }
      }
    }
  }
}

布尔类型

bash 复制代码
PUT test-2
{
  "mappings": {
    "properties": {
      "sex": {
        "type": "boolean"
      }
    }
  }
}

3.3 json类型和数组类型

bash 复制代码
PUT obj-test
{
  "mappings": {
    "properties": {
      "region": {
        "type": "keyword"
      },
      "manager": {
        "properties": {
          "age":  { "type": "integer" },
          "name": {
            "properties": {
              "first": { "type": "text" },
              "last":  { "type": "text" }
            }
          }
        }
      }
    }
      }
    }
bash 复制代码
PUT shopping/_doc/1
{
  "tags":  [ "elastic", "search" ],
  "lists": [
    {
      "name": "mylist",
      "description": "language list"
    },
    {
      "name": "testlist",
      "description": "testlist"
    }
  ]
}

3.4经纬度类型和二进制类型

bash 复制代码
PUT geo-1
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_point"
      }
    }
  }
}
bash 复制代码
PUT binary-test
{
  "mappings": {
    "properties": {
      "pic": {
        "type": "binary"
      }
    }
  }
}

4、动态映射

看下面的例子就清楚了,如果我们添加明显有特征的数据类型,就会自动变成下面的数据类型

bash 复制代码
PUT number-test
{
  "mappings": {
    "numeric_detection": true
  }
}
PUT number-test/_doc/1
{
  "price": "2.5",
  "amount":"2"
}

GET number-test/_mapping

{
  "number-test" : {
    "mappings" : {
      "numeric_detection" : true,
      "properties" : {
        "amount" : {
          "type" : "long"
        },
        "price" : {
          "type" : "float"
        }
      }
    }
  }
}
相关推荐
大大大大晴天5 小时前
从 Prompt 到 Tool Calling:AI Agent 如何稳定连接程序世界
大数据·agent
key_3_feng6 小时前
反馈闭环:Agent 自我进化的真正引擎
大数据·agent
EnCi Zheng6 小时前
Agent 如何进行工具调用:从一次 Function Calling 到生产级工具编排的完整链路
大数据·人工智能·agent·ai编程·工具调用
GlobalInfo6 小时前
重磅发布 | 2026年船体清洁机器人行业研究报告:市场规模、发展前景与十五五战略地图
大数据·人工智能·机器人
海宇服务7 小时前
零信任架构实战:基于海宇全能小微企业报告构建自动化园区商户免押租赁资信核验网关
大数据·人工智能·架构·自动化
热文新讯8 小时前
迎进博·话疗愈:2026酒店“疗愈睡眠”闭门研讨会在沪举行
大数据·人工智能·物联网
NailiConveyor10 小时前
柔性物流方案如何适配工厂升级?自动化输送流水线定制要点
大数据·运维·自动化·制造·业界资讯
梦影_11 小时前
MySQL进阶教程(五)—— SQL调优(下)
android·大数据·数据库·后端·sql·mysql·database
没落之王11 小时前
三易五行看宋朝的三界确立
大数据·人工智能·算法
samLi062013 小时前
【数据集】水星逆行及相关日期窗口数据(1973-2025年)
大数据