es - elasticsearch mapping - field data type - 1

145 阅读1分钟

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。

问 :field的common数据类型有哪些?
答 :

1. binary
2. boolean
3. keywords
4. numbers

问 :binary数据类型如何使用?
答 :

# 数据类型 : binary
# 存储内容 : binary 的 base64 编码
# 配置项   :
#   1. doc_values : 默认false
#   2. store      : 默认false
PUT /binary_test
{
  "mappings" : {
    "properties" : {
      "blob" : {
        "type"      : "binary", 
        "doc_values": true,
        "store"     : true
      }
    }
  }
}

GET /binary_test/_mapping

# 结果
{
  "binary_test" : {
    "mappings" : {
      "properties" : {
        "blob" : {
          "type" : "binary",
          "store" : true,
          "doc_values" : true
        }
      }
    }
  }
}

# 二进制的base64编码
POST /binary_test/_doc
{
  "blob" : "U29tZSBiaW5hcnkgYmxvYg=="
}

POST /binary_test/_search

# 结果
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "binary_test",
        "_type" : "_doc",
        "_id" : "gue-wncBb10PlmuY6QuN",
        "_score" : 1.0,
        "_source" : {
          "blob" : "U29tZSBiaW5hcnkgYmxvYg=="
        }
      }
    ]
  }
}

问 :boolean数据类型如何使用?
答 :

# 数据类型 : boolean
# 存储内容 :
#   1. false "false" ""
#   2. true  "true"
# 配置项   :
#   1. boost      : 默认1.0,不推荐使用
#   2. doc_values : 默认true
#   3. index      : 默认true
#   4. null_value : 默认null
#   5. store      : 默认false
#   6. meta       : metadata
PUT /boolean_test
{
  "mappings" : {
    "properties" : {
      "is_boolean" : {
        "type" : "boolean",
        "boost" : 2.0,
        "doc_values" : true,
        "index"      : true,
        "store"      : true,
        "null_value" : "false",
        "meta"       : {"is_type" : "boolean"}
      }
    }
  }
}

# 结果
#! Deprecation: Parameter [boost] on field [is_boolean] is deprecated and will be removed in 8.0
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "boolean_test"
}

# 索引了"", false, "false", true
POST /boolean_test/_doc
{
  "is_boolean" : ""
}

GET /boolean_test/_search
{
  "query": {
    "match": {
      "is_boolean": "false"
    }
  }
}

# 结果
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 0.7133499,
    "hits" : [
      {
        "_index" : "boolean_test",
        "_type" : "_doc",
        "_id" : "hOfYwncBb10PlmuYZQuc",
        "_score" : 0.7133499,
        "_source" : {
          "is_boolean" : "false"
        }
      },
      {
        "_index" : "boolean_test",
        "_type" : "_doc",
        "_id" : "hufZwncBb10PlmuYrQuV",
        "_score" : 0.7133499,
        "_source" : {
          "is_boolean" : null
        }
      },
      {
        "_index" : "boolean_test",
        "_type" : "_doc",
        "_id" : "i-fawncBb10PlmuYTwvV",
        "_score" : 0.7133499,
        "_source" : {
          "is_boolean" : ""
        }
      }
    ]
  }
}