天天看點

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