Elasticsearch 版本 6.2.4
1. 當對某一type,關閉動態mapping(設為false,非strict)時,插入新的字段是否會存儲呢?能否搜索呢?能否排序呢?
創建索引:
PUT test/
{
"mappings": {
"_doc": {
"dynamic":false,
"properties": {
"addTime": {
"type": "integer"
}
}
}
}
}
查看mapping:
GET test/_mapping
得到結果:
{
"test": {
"mappings": {
"_doc": {
"dynamic": "false",
"properties": {
"addTime": {
"type": "integer"
}
}
}
}
}
}
插入數據:
POST test/_doc
{
"id":1,
"addTime":"1536476000"
}
查看是否存入:
GET test/_search
得到結果:
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "test",
"_type": "_doc",
"_id": "4zJ8vWUBHYQn1k6_tB3V",
"_score": 1,
"_source": {
"id": 1,
"addTime": "1536476000"
}
}
]
}
}
可以得知是會存儲的。
查看是否可以搜索:
GET test/_search
{
"query": {
"term": {
"id": {
"value": "1"
}
}
}
}
得到結果:
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
可以得知是無法搜索的。
查看是否可以排序:
GET test/_search
{
"sort": [
{
"id": {
"order": "desc"
}
}
]
}
得到結果:
{
"error": {
"root_cause": [
{
"type": "query_shard_exception",
"reason": "No mapping found for [id] in order to sort on",
"index_uuid": "4HMd3wH5SxWqfU-6rEfHyw",
"index": "test"
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "test",
"node": "y_oEUnmsTqyzHKrkH54d_w",
"reason": {
"type": "query_shard_exception",
"reason": "No mapping found for [id] in order to sort on",
"index_uuid": "4HMd3wH5SxWqfU-6rEfHyw",
"index": "test"
}
}
]
},
"status": 400
}
可以得知是無法排序的。
結論:當對某一type,關閉動態mapping(設為false,非strict)時,插入新的字段是會被存儲的,但不能搜索和排序。
2. 對於某一字段禁止建立索引之后,搜索結果是否還存在該字段?該字段能否被搜索到?該字段能否用來排序?
創建索引:
PUT test/
{
"mappings": {
"_doc": {
"properties": {
"addTime": {
"type": "integer",
"index":false
}
}
}
}
}
查看mapping:
GET test/_mapping
得到結果:
{
"test": {
"mappings": {
"_doc": {
"properties": {
"addTime": {
"type": "integer",
"index": false
}
}
}
}
}
}
插入數據:
POST test/_doc
{
"addTime":1536476000
}
查看是否存入:
GET test/_search
得到結果:
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "test",
"_type": "_doc",
"_id": "5jKPvWUBHYQn1k6_EB0i",
"_score": 1,
"_source": {
"addTime": 1536476000
}
}
]
}
}
可以看到能夠存入。
查看是否能夠搜索:
GET test/_search
{
"query": {
"term": {
"addTime": {
"value": 1536476000
}
}
}
}
得到結果:
{
"error": {
"root_cause": [
{
"type": "query_shard_exception",
"reason": "failed to create query: {\n \"term\" : {\n \"addTime\" : {\n \"value\" : 1536476000,\n \"boost\" : 1.0\n }\n }\n}",
"index_uuid": "QNQEwQONTpe5DL2KJal70g",
"index": "test"
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "test",
"node": "y_oEUnmsTqyzHKrkH54d_w",
"reason": {
"type": "query_shard_exception",
"reason": "failed to create query: {\n \"term\" : {\n \"addTime\" : {\n \"value\" : 1536476000,\n \"boost\" : 1.0\n }\n }\n}",
"index_uuid": "QNQEwQONTpe5DL2KJal70g",
"index": "test",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "Cannot search on field [addTime] since it is not indexed."
}
}
}
]
},
"status": 400
}
可以得知不能對未索引(index:false)的字段進行搜索。
查看是否能夠排序:
GET test/_search
{
"sort": [
{
"addTime": {
"order": "desc"
}
}
]
}
得到結果:
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": null,
"hits": [
{
"_index": "test",
"_type": "_doc",
"_id": "ryi1xmUBXnKGUBJZj3hG",
"_score": null,
"_source": {
"addTime": 1536476000
},
"sort": [
1536476000
]
}
]
}
}
得知是可以排序的。
結論:對於某一字段禁止建立索引之后,該字段的值是可以存入的(存在於搜索結果),但是不能搜索,可以排序。
