其他分享
首页 > 其他分享> > ElasticSearch(五):Mapping和常见字段类型

ElasticSearch(五):Mapping和常见字段类型

作者:互联网

ElasticSearch(五):Mapping和常见字段类型

学习课程链接《Elasticsearch核心技术与实战》


什么是Mapping


字段的数据类型


什么是Dynamic Mapping


类型的自动识别

JSON类型 Elasticsearch类型
字符串 匹配日期格式,设置成Date;匹配数字设置成Float或者Long,该选项默认关闭;设置为Text,并且增加keyword子字段
布尔值 Boolean
浮点数 Float
整数 Long
对象 Object
数组 由第一个非空数的类型所决定
空值 忽略


#写入文档,查看 Mapping
PUT mapping_test/_doc/1
{
  "firstName":"Chan",
  "loginDate":"2018-07-24T10:29:48.103Z",
  "uid" : "123",
  "isVip" : false,
  "isAdmin": "true",
  "age":19,
  "heigh":180
}

#Delete index
DELETE mapping_test

#查看 Dynamic Mapping文件
GET mapping_test/_mapping
#查看 Dynamic Mapping返回结果
{
  "mapping_test" : {
    "mappings" : {
      "properties" : {
        "age" : {
          "type" : "long"  # "age":19,设置为long
        },
        "firstName" : {
          "type" : "text",  # "firstName":"Chan",设置为Text,并且增加keyword子字段
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "heigh" : {
          "type" : "long" #"heigh":180设置为long
        },
        "isAdmin" : {
          "type" : "text", #"isAdmin": "true",设置为Text,并且增加keyword子字段
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "isVip" : {
          "type" : "boolean" #"isVip" : false,设置为boolean
        },
        "loginDate" : {
          "type" : "date" #"loginDate":"2018-07-24T10:29:48.103Z",设置为Date
        },
        "uid" : {
          "type" : "text", # "uid" : "123",设置为Text,并且增加keyword子字段,匹配数字设置成Float或者Long,该选项默认关闭;
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}


能否更改 Mapping 的字段类型

分两种情况:


控制Dynamic Mappings

dynamic true false strict
文档可索引 YES YES NO
字段可索引 YES NO NO
Mapping被更新 YES NO NO


#1.默认Mapping支持dynamic,写入的文档中加入新的字段
PUT dynamic_mapping_test/_doc/1
{
  "newField":"someValue"
}
#2.该字段可以被搜索,数据也在_source中出现
POST dynamic_mapping_test/_search
{
  "query":{
    "match":{
      "newField":"someValue"
    }
  }
}
#返回结果:
{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "dynamic_mapping_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "newField" : "someValue"
        }
      }
    ]
  }
}
#3.修改为dynamic false
PUT dynamic_mapping_test/_mapping
{
  "dynamic": false
}
#4.新增 anotherField
PUT dynamic_mapping_test/_doc/10
{
  "anotherField":"someValue"
}
#5.该字段不可以被搜索,因为dynamic已经被设置为false
POST dynamic_mapping_test/_search
{
  "query":{
    "match":{
      "anotherField":"someValue"
    }
  }
}
#返回结果:
{
  "took" : 657,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}
#6.修改为strict
PUT dynamic_mapping_test/_mapping
{
  "dynamic": "strict"
}

#7.写入数据出错,HTTP Code 400
PUT dynamic_mapping_test/_doc/12
{
  "lastField":"value"
}
#返回结果:
{
  "error": {
    "root_cause": [
      {
        "type": "strict_dynamic_mapping_exception",
        "reason": "mapping set to strict, dynamic introduction of [lastField] within [_doc] is not allowed"
      }
    ],
    "type": "strict_dynamic_mapping_exception",
    "reason": "mapping set to strict, dynamic introduction of [lastField] within [_doc] is not allowed"
  },
  "status": 400
}


如何定义一个 Mapping

PUT  index_name
{
    "mappings":{
        "properties":{
            //define your mappings here
        }
    }
}


Mapping的一些配置

#1.设置 index 为 false
DELETE users
PUT users
{
    "mappings" : {
      "properties" : {
        "firstName" : {
          "type" : "text"
        },
        "lastName" : {
          "type" : "text"
        },
        "mobile" : {
          "type" : "text",
          "index": false
        }
      }
    }
}
#插入数据
PUT users/_doc/1
{
  "firstName":"Ruan",
  "lastName": "Yiming",
  "mobile": "12345678"
}
#查询
POST /users/_search
{
  "query": {
    "match": {
      "mobile":"12345678" #该字段不可被搜索
    }
  }
}
#查询返回结果:
{
  "error": {
    "root_cause": [
      {
        "type": "query_shard_exception",
        "reason": "failed to create query: {\n  \"match\" : {\n    \"mobile\" : {\n      \"query\" : \"12345678\",\n      \"operator\" : \"OR\",\n      \"prefix_length\" : 0,\n      \"max_expansions\" : 50,\n      \"fuzzy_transpositions\" : true,\n      \"lenient\" : false,\n      \"zero_terms_query\" : \"NONE\",\n      \"auto_generate_synonyms_phrase_query\" : true,\n      \"boost\" : 1.0\n    }\n  }\n}",
        "index_uuid": "1oB9dwY2TPq-9QjiaMaU7g",
        "index": "users"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "users",
        "node": "u-4S1mfbQiuA1Bqe-wfPJQ",
        "reason": {
          "type": "query_shard_exception",
          "reason": "failed to create query: {\n  \"match\" : {\n    \"mobile\" : {\n      \"query\" : \"12345678\",\n      \"operator\" : \"OR\",\n      \"prefix_length\" : 0,\n      \"max_expansions\" : 50,\n      \"fuzzy_transpositions\" : true,\n      \"lenient\" : false,\n      \"zero_terms_query\" : \"NONE\",\n      \"auto_generate_synonyms_phrase_query\" : true,\n      \"boost\" : 1.0\n    }\n  }\n}",
          "index_uuid": "1oB9dwY2TPq-9QjiaMaU7g",
          "index": "users",
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "Cannot search on field [mobile] since it is not indexed." #错误原因
          }
        }
      }
    ]
  },
  "status": 400
}
#设定Null_value
DELETE users
PUT users
{
    "mappings" : {
      "properties" : {
        "firstName" : {
          "type" : "text"
        },
        "lastName" : {
          "type" : "text"
        },
        "mobile" : {
          "type" : "keyword",
          "null_value": "NULL"
        }

      }
    }
}
#插入数据
PUT users/_doc/1
{
  "firstName":"Ruan",
  "lastName": "Yiming",
  "mobile": null
}
#插入数据
PUT users/_doc/2
{
  "firstName":"Ruan2",
  "lastName": "Yiming2"

}
#查询
GET users/_search
{
  "query": {
    "match": {
      "mobile":"NULL"
    }
  }
}
#查询返回结果:
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "users",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "firstName" : "Ruan",
          "lastName" : "Yiming",
          "mobile" : null
        }
      }
    ]
  }
}
#设置 Copy to
DELETE users
PUT users
{
  "mappings": {
    "properties": {
      "firstName":{
        "type": "text",
        "copy_to": "fullName"
      },
      "lastName":{
        "type": "text",
        "copy_to": "fullName"
      }
    }
  }
}
#插入数据
PUT users/_doc/1
{
  "firstName":"Ruan",
  "lastName": "Yiming"
}
#查询方法1
GET users/_search?q=fullName:(Ruan Yiming)
#查询方法2
POST users/_search
{
  "query": {
    "match": {
       "fullName":{
        "query": "Ruan Yiming",
        "operator": "and"
      }
    }
  }
}
#查询返回结果:
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "users",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.5753642,
        "_source" : {
          "firstName" : "Ruan",
          "lastName" : "Yiming"
        }
      }
    ]
  }
}
#数组类型
PUT users/_doc/1
{
  "name":"twobirds",
  "interests":["reading","music"]
}
GET users/_mapping
#返回Mapping结果:
{
  "users" : {
    "mappings" : {
      "properties" : {
        "firstName" : {
          "type" : "text",
          "copy_to" : [
            "fullName"
          ]
        },
        "fullName" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "interests" : {
          "type" : "text", #数组类型,根据数组里数据类型配置
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "lastName" : {
          "type" : "text",
          "copy_to" : [
            "fullName"
          ]
        },
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

标签:users,doc,dynamic,Mapping,常见字,ElasticSearch,mapping,type
来源: https://www.cnblogs.com/czbxdd/p/11679230.html