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 进行统计分析和精准搜索

  PUT keyword-test
  {
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      }
    }
  }
}
 PUT keyword-test
  {
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword"
      }
    }
  }
}

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

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

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

数值类型

PUT obj-test
{
  "mappings": {
    "properties": {
      "manager": {
        "properties": {
          "age":  { "type": "integer" }
        }
      }
    }
  }
}

布尔类型

PUT test-2
{
  "mappings": {
    "properties": {
      "sex": {
        "type": "boolean"
      }
    }
  }
}

3.3 json类型和数组类型

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

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

PUT geo-1
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_point"
      }
    }
  }
}
PUT binary-test
{
  "mappings": {
    "properties": {
      "pic": {
        "type": "binary"
      }
    }
  }
}

4、动态映射

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

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"
        }
      }
    }
  }
}

你可能感兴趣的:(Elasticsearch,elasticsearch,jenkins,大数据)