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"
        }
      }
    }
  }
}
相关推荐
字节跳动数据平台3 小时前
代码量减少 70%、GPU 利用率达 95%:火山引擎多模态数据湖如何释放模思智能的算法生产力
大数据
得物技术5 小时前
深入剖析Spark UI界面:参数与界面详解|得物技术
大数据·后端·spark
武子康6 小时前
大数据-238 离线数仓 - 广告业务 Hive分析实战:ADS 点击率、购买率与 Top100 排名避坑
大数据·后端·apache hive
武子康1 天前
大数据-237 离线数仓 - Hive 广告业务实战:ODS→DWD 事件解析、广告明细与转化分析落地
大数据·后端·apache hive
大大大大晴天1 天前
Flink生产问题排障-Kryo serializer scala extensions are not available
大数据·flink
Elasticsearch2 天前
如何使用 Agent Builder 排查 Kubernetes Pod 重启和 OOMKilled 事件
elasticsearch
Elasticsearch3 天前
通用表达式语言 ( CEL ): CEL 输入如何改进 Elastic Agent 集成中的数据收集
elasticsearch
武子康3 天前
大数据-236 离线数仓 - 会员指标验证、DataX 导出与广告业务 ODS/DWD/ADS 全流程
大数据·后端·apache hive
武子康4 天前
大数据-235 离线数仓 - 实战:Flume+HDFS+Hive 搭建 ODS/DWD/DWS/ADS 会员分析链路
大数据·后端·apache hive
DianSan_ERP5 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet