index-是否可被搜索
示例如下:
PUT es-0001
{
"mappings": {
"properties": {
"name": {
"type": "keyword",
"index": false
}
}
}
}
POST es-0001/_doc
{
"name":"James"
}
GET es-0001/_search
{
"query": {
"term": {
"name": {
"value": "kevin"
}
}
}
}
{
"error" : {
"root_cause" : [
{
"type" : "query_shard_exception",
"reason" : "failed to create query: Cannot search on field [name] since it is not indexed.",
"index_uuid" : "ps0-ZgOfTuaGYxiAtvGFWQ",
"index" : "es-0001"
}
],
"type" : "search_phase_execution_exception",
"reason" : "all shards failed",
"phase" : "query",
"grouped" : true,
"failed_shards" : [
{
"shard" : 0,
"index" : "es-0001",
"node" : "88kvwMVmTnCb3vUjGWhc-A",
"reason" : {
"type" : "query_shard_exception",
"reason" : "failed to create query: Cannot search on field [name] since it is not indexed.",
"index_uuid" : "ps0-ZgOfTuaGYxiAtvGFWQ",
"index" : "es-0001",
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "Cannot search on field [name] since it is not indexed."
}
}
}
]
},
"status" : 400
}
store-是否被存储
概念:默认原始数据存储在_source里,也可以在Lucene里面存储一份,通过此关键词空置取值范围,true/false,默认false不存储。
PUT es-0001
{
"mappings": {
"_source": {
"enabled": false
},
"properties": {
"name": {
"type": "keyword",
"index": false,
"store": false
}
}
}
}
POST es-0001/_doc
{
"name":"James"
}
GET es-0001/_search
{
"stored_fields": ["name"]
}
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "es-0001",
"_type" : "_doc",
"_id" : "vwcnHX4BNbkwptbUE98B",
"_score" : 1.0,
"fields" : {
"name" : [
"James"
]
}
},
{
"_index" : "es-0001",
"_type" : "_doc",
"_id" : "wgcnHX4BNbkwptbUFN_3",
"_score" : 1.0,
"fields" : {
"name" : [
"James"
]
}
}
]
}
}
enabled-启用
概念:
设置字段是否需要被检索,类似index属性,用于对未知object类型的设置,取值范围true/false,不仅禁用了索引,还禁用了mapping结构。
应用领域:
部分应用领域,一些数据仅仅需要数据字段,不需要被检索与创建索引,一般用于对于object类型的设置,避免设置太多
PUT es-0001
{
"mappings": {
"_source": {
"enabled": false
},
"properties": {
"address": {
"type": "object",
"enabled": "false"
}
}
}
}
POST es-0001/_doc
{
"name":"James",
"address":{
"province":"Shaanxi",
"city":"xi'an"
}
}
POST es-0001/_doc
{
"name":"Kevin",
"address":{
"province":"Sichuan",
"city":"zhengzhou"
}
}
GET es-0001
GET es-0001的结果,可以看到address的type是object。并不会展示详细的信息
{
"es-0001" : {
"aliases" : { },
"mappings" : {
"_source" : {
"enabled" : false
},
"properties" : {
"address" : {
"type" : "object",
"enabled" : false
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "1",
"provided_name" : "es-0001",
"creation_date" : "1641167231118",
"number_of_replicas" : "1",
"uuid" : "GhfpWtBHR_GWFOsspLZ4Jw",
"version" : {
"created" : "7110199"
}
}
}
}
}
doc_value-聚合排序
概念解释:
列式数据存储,ES数据存储原始数据一份,列式数据一份,默认有2份数据。
应用:
基于该字段做聚合分析
基于该字段做排序
PUT es-0001
{
"mappings": {
"_source": {
"enabled": false
},
"properties": {
"name":{
"type": "keyword",
"doc_values": false
},
"address": {
"type": "object",
"enabled": "true"
}
}
}
}
POST es-0001/_doc
{
"name":"James1",
"address":{
"province":"Shaanxi",
"city":"xi'an"
}
}
POST es-0001/_doc
{
"name":"James2",
"address":{
"province":"Sichuan",
"city":"zhengzhou"
}
}
GET es-0001/_search
{
"sort": [
{
"name": {
"order": "desc"
}
}
]
}
{
"error" : {
"root_cause" : [
{
"type" : "illegal_argument_exception",
"reason" : "Can't load fielddata on [name] because fielddata is unsupported on fields of type [keyword]. Use doc values instead."
}
],
"type" : "search_phase_execution_exception",
"reason" : "all shards failed",
"phase" : "query",
"grouped" : true,
"failed_shards" : [
{
"shard" : 0,
"index" : "es-0001",
"node" : "kO50LN6pQVii_yQG2W94zQ",
"reason" : {
"type" : "illegal_argument_exception",
"reason" : "Can't load fielddata on [name] because fielddata is unsupported on fields of type [keyword]. Use doc values instead."
}
}
],
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "Can't load fielddata on [name] because fielddata is unsupported on fields of type [keyword]. Use doc values instead.",
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "Can't load fielddata on [name] because fielddata is unsupported on fields of type [keyword]. Use doc values instead."
}
}
},
"status" : 400
}
null-value-空置默认值
概念介绍:
ES允许可以不设置字段,没有数据库的必填选型;与数据库的默认值有区别,ES并不会存储这个值,也就是_source中不存储,仅仅用来索引检索
应用:
程序性能提示
避免程序数据异常错误
DELETE es-0001
PUT es-0001
{
"mappings": {
"_source": {
"enabled": true
},
"properties": {
"name":{
"type": "keyword",
"doc_values": true,
"null_value": "default"
},
"address": {
"type": "object",
"enabled": "true"
}
}
}
}
POST es-0001/_doc
{
"name":"kevin",
"address":{
"province":"Shaanxi",
"city":"xi'an"
}
}
POST es-0001/_doc
{
"name":null,
"address":{
"province":"Sichuan",
"city":"zhengzhou"
}
}
POST es-0001/_doc
{
"name":"James3",
"address":{
"province":"Sichuan",
"city":"zhengzhou"
}
}
GET es-0001
GET es-0001/_search
{
"sort": [
{
"name": {
"order": "desc"
}
}
]
}
GET es-0001/_search
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "es-0001",
"_type" : "_doc",
"_id" : "EQdbHX4BNbkwptbUXuKU",
"_score" : null,
"_source" : {
"name" : "kevin",
"address" : {
"province" : "Shaanxi",
"city" : "xi'an"
}
},
"sort" : [
"kevin"
]
},
{
"_index" : "es-0001",
"_type" : "_doc",
"_id" : "EwdbHX4BNbkwptbUaOLC",
"_score" : null,
"_source" : {
"name" : null,
"address" : {
"province" : "Sichuan",
"city" : "zhengzhou"
}
},
"sort" : [
"default"
]
},
{
"_index" : "es-0001",
"_type" : "_doc",
"_id" : "FAdbHX4BNbkwptbUcOLE",
"_score" : null,
"_source" : {
"name" : "James3",
"address" : {
"province" : "Sichuan",
"city" : "zhengzhou"
}
},
"sort" : [
"James3"
]
}
]
}
}
可以看到第二条数据的排序是default值,也就是null_value设置的值,感觉这个属性意义不大。
dynamic-自动扩充
概念解释:是否允许对象下面的属性自由扩展。可选值有true/false/strict/runtime,默认true。
应用:严格限制子对象下面的字段行为
PUT es-0001
{
"mappings": {
"_source": {
"enabled": true
},
"properties": {
"name": {
"type": "keyword",
"doc_values": true,
"null_value": "default"
},
"address": {
"dynamic": "strict",
"properties": {
"province": {
"type": "keyword"
}
}
}
}
}
}
POST es-0001/_doc
{
"name":"kevin",
"address":{
"province":"Shaanxi",
"city":"xi'an"
}
}
当设置为strict之后,不能进行任意扩展。结果如下。
{
"error" : {
"root_cause" : [
{
"type" : "strict_dynamic_mapping_exception",
"reason" : "mapping set to strict, dynamic introduction of [city] within [address] is not allowed"
}
],
"type" : "strict_dynamic_mapping_exception",
"reason" : "mapping set to strict, dynamic introduction of [city] within [address] is not allowed"
},
"status" : 400
}
ignore_above-超长忽略检索
概念解释:keyword类型下,字符过于长,检索意义不大,索引会被禁用,数据不可被检索
应用领域:基于keyword类型检索应用,超过限定长度就无需检索
PUT es-0001
{
"mappings": {
"_source": {
"enabled": true
},
"properties": {
"name": {
"type": "keyword",
"ignore_above": "5"
},
"address": {
"properties": {
"province": {
"type": "keyword"
}
}
}
}
}
}
POST es-0001/_doc
{
"name":"Kevinwu",
"address":{
"province":"Shaanxi",
"city":"xi'an"
}
}
GET es-0001/_search
{
"query": {
"term": {
"name": {
"value": "KevinWu"
}
}
}
}
超过5位的就无法被ES检索到了。
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
fields-多字段属性
概念解释:多字段属性
应用领域:同一个数据字段,需要同时满足多种业务需要.
PUT es-0001
{
"mappings": {
"_source": {
"enabled": true
},
"properties": {
"name": {
"type": "keyword",
"ignore_above": "500"
},
"age": {
"type": "keyword",
"fields": {
"age_alias": {
"type": "integer"
}
}
}
}
}
}
POST es-0001/_doc
{
"name":"Kevinwu",
"age":"20"
}
GET es-0001/_search
{
"query": {
"range": {
"age.age_alias": {
"gte": 5,
"lte": 90
}
}
}
}
在搜索的时候,可以根据age=“20” 字符串进行搜索,也可以用age.age_alias进行范围搜索。
coerce-限制类型
PUT es-0001
{
"mappings": {
"_source": {
"enabled": true
},
"properties": {
"name": {
"type": "keyword",
"ignore_above": "500"
},
"age": {
"type": "integer",
"coerce": false
}
}
}
}
POST es-0001/_doc
{
"name":"Kevinwu",
"age":"20"
}
限制了类型后,integer类型的插入字符类型20后,就提示无法插入。
{
"error" : {
"root_cause" : [
{
"type" : "mapper_parsing_exception",
"reason" : "failed to parse field [age] of type [integer] in document with id '2Qf8H34BNbkwptbUx_4l'. Preview of field's value: '20'"
}
],
"type" : "mapper_parsing_exception",
"reason" : "failed to parse field [age] of type [integer] in document with id '2Qf8H34BNbkwptbUx_4l'. Preview of field's value: '20'",
"caused_by" : {
"type" : "illegal_argument_exception",
"reason" : "Integer value passed as String"
}
},
"status" : 400
}