Elasticsearch 版本 6.2.4
1. 当对某一type,关闭动态mapping(设为false,非strict)时,插入新的字段是否会存储呢,能否搜索呢?能否排序呢?
创建索引:
PUT test/ { "mappings": { "_doc": { "dynamic":false, "properties": { "addTime": { "type": "integer" } } } } }
插入数据:
POST test_xzy/_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": 1, "max_score": 1, "hits": [ { "_index": "test", "_type": "_doc", "_id": "4zJ8vWUBHYQn1k6_tB3V", "_score": 1, "_source": { "id": 1, "addTime": "1536476000" } } ] } }
可以得知是可以搜索的。
查看是否可以排序:
GET test/_search { "sort": [ { "id": { "order": "desc" } } ] }
得到结果:
{ "took": 1, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": null, "hits": [ { "_index": "test", "_type": "_doc", "_id": "4zJ8vWUBHYQn1k6_tB3V", "_score": null, "_source": { "id": 1, "addTime": "1536476000" }, "sort": [ 1 ] } ] } }
可以得知是可以排序的。
结论:当对某一type,关闭动态mapping(设为false,非strict)时,插入新的字段是会被存储的,也可以被搜索和排序的。
2. 对于某一字段禁止建立索引之后,搜索结果是否还存在该字段,该字段能否被搜索到?该字段能否用来排序?
创建索引:
PUT test_xzy/ { "mappings": { "_doc": { "properties": { "addTime": { "type": "integer", "index":false } } } } }
插入数据:
PUT test_xzy/ { "mappings": { "_doc": { "dynamic":false, "properties": { "addTime": { "type": "integer" }, "id":{ "type":"integer", "index":false } } } } }
查看是否存入:
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": { "id": 1, "addTime": "1536476000" } } ] } }
可以看到能够存入。
查看是否能够搜索:
GET test/_search { "query": { "term": { "id": { "value": "1" } } } }
得到结果:
{ "error": { "root_cause": [ { "type": "query_shard_exception", "reason": "failed to create query: {\n \"term\" : {\n \"id\" : {\n \"value\" : \"1\",\n \"boost\" : 1.0\n }\n }\n}", "index_uuid": "cNTINYvNTo6Gxbcq8gnJgA", "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 \"id\" : {\n \"value\" : \"1\",\n \"boost\" : 1.0\n }\n }\n}", "index_uuid": "cNTINYvNTo6Gxbcq8gnJgA", "index": "test", "caused_by": { "type": "illegal_argument_exception", "reason": "Cannot search on field [id] since it is not indexed." } } } ] }, "status": 400 }
可以得知不能对未索引(index:false)的字段进行搜索。
查看是否能够排序:
GET test/_search { "sort": [ { "id": { "order": "desc" } } ] }
得到结果:
{ "took": 1, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": null, "hits": [ { "_index": "test", "_type": "_doc", "_id": "5jKPvWUBHYQn1k6_EB0i", "_score": null, "_source": { "id": 1, "addTime": "1536476000" }, "sort": [ 1 ] } ] } }
得知是可以排序的。
结论:对于某一字段禁止建立索引之后,该字段的值是可以存入的(存在于搜索结果),但是不能搜索的,可以排序的。
原文:https://www.cnblogs.com/xzysaber/p/9614223.html