天天看点

Elasticsearch实战(六)-mapping映射(中)4 数据类型

4 数据类型

  • 字符串

    text、keyword

  • 数值型

    long、integer、short, byte, double, float half_float, scaled_float

  • 布尔

    boolean

  • 日期

    date

  • 二进制

    binary

  • 范围

    integer_range, float_range, long_range, double_range, date_ range

  • 复杂数据类型

数组类型array、对象类型object、嵌套类型nested object

  • 地理位置

    geo_point、geo_shape

  • 专用类型

    记录ip地址 ip

    实现自动补全 completion

    记录分词数 token_count

    记录字符串hash值 murmur3

    percolator

    join

  • 多字段特性multi-fields

允许对同一个字段采用不同的配置,比如分词,常见例子如对人名实现拼音搜索,

只需要在人名中新增一个子字段为pinyin即可

Elasticsearch实战(六)-mapping映射(中)4 数据类型
Elasticsearch实战(六)-mapping映射(中)4 数据类型

ES可以自动识别文档字段类型,从而降低用户使用成本,如下所示

PUT /test_index/doc/1
{
  "username": "java",
  "age": "18"
}

GET /test_index/_mapping      
{
  "test_index" : {
    "mappings" : {
      "properties" : {
        "age" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "username" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}
      

ES是依靠JSON文档的字段类型来实现自动识别字段类型,支持的类型如下:

Elasticsearch实战(六)-mapping映射(中)4 数据类型
PUT /test_index/doc/1
{"username":" alfred",
"age":14,
"birth":"1988-10-10",
"married":false,
"year":18,
"tags":["boy","fashion"],
"money":100.1
}

GET /test_index/_mapping      
{
  "test_index" : {
    "mappings" : {
      "properties" : {
        "age" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "birth" : {
          "type" : "date"
        },
        "married" : {
          "type" : "boolean"
        },
        "money" : {
          "type" : "float"
        },
        "tags" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "username" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "year" : {
          "type" : "long"
        }
      }
    }
  }
}