Elasticsearch 笔记
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 ] } ] } }
得知是可以排序的。
结论:对于某一字段禁止建立索引之后,该字段的值是可以存入的(存在于搜索结果),但是不能搜索,可以排序。
Praise the sun.