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"
        }
      }
    }
  }
}
相关推荐
Urbano3 小时前
稳产提质、快返增效:慧拿智能模板机重塑服装厂精细化盈利模式
大数据·人工智能
YueLin1113 小时前
私域直播系统的下半场,是社区零售的分水岭
大数据·零售
哥本哈士奇3 小时前
医疗器械行业 Salesforce Territory 完整落地实例
大数据·人工智能
samLi06204 小时前
【无标题】
大数据
zandy10114 小时前
企业级BI平台选型指南:评估框架与核心能力矩阵
大数据·人工智能·矩阵
远铂4 小时前
BuildAdmin:GEO优化与AI内容营销一体化解决方案
大数据·人工智能·geo·buildadmin
董可伦4 小时前
Spark 源码 | Yarn Client 模式提交流程(五)
大数据·spark
abcy0712135 小时前
storm 实时性
大数据·storm
10岁的博客5 小时前
DevEco Code 的 Plan+Build 模式:审方案再执行的技术深度解析
大数据·数据库·人工智能
会助力智能会务5 小时前
会务系统供应商怎么选?会助力智能会务系统,一站式数字化会务服务商
大数据·运维·人工智能