ElasticSearch默认为每个被索引的文档都定义了一个特殊的域 - '_all',它自动包含被索引文档中一个或者多个域中的内容, 在进行搜索时,如果不指明要搜索的文档的域,ElasticSearch则会去搜索_all域。_all带来搜索方便,其代价是增加了系统在索引阶段对CPU和存储空间资源的开销。
默认情况,ElasticSarch自动使用_all所有的文档的域都会被加到_all中进行索引。可以使用"_all" : {"enabled":false} 开关禁用它。如果某个域不希望被加到_all中,可以使用 "include_in_all":false。例如:
- {
- "person": {
- "_all": { "enabled": true }
- "properties": {
- "name": {
- "type": "object",
- "dynamic": false,
- "properties": {
- "first": {
- "type": "string",
- "store": true,
- "include_in_all": false
- },
- "last": {
- "type": "string",
- "index": "not_analyzed"
- }
- }
- },
- "address": {
- "type": "object",
- "include_in_all": false,
- "properties": {
- "first": {
- "properties": {
- "location": {
- "type": "string",
- "store": true,
- "index_name": "firstLocation"
- }
- }
- },
- "last": {
- "properties": {
- "location": {
- "type": "string"
- }
- }
- }
- }
- },
- "simple1": {
- "type": "long",
- "include_in_all": true
- },
- "simple2": {
- "type": "long",
- "include_in_all": false
- }
- }
- }
- }
查询时,_all和其它域一样使用:
- GET /profiles/_search
- {
- "query": {
- "match": {
- "_all": "food"
- }
- }
- }
或者在不提供搜索域的情况下,默认会搜索_all,例如:
- GET /profiles/_search
- {
- "query": {
- "query_string": {
- "query": "food"
- }
- }
- }
参考资源
本文详细解释了ElasticSearch中的_all域的作用、默认行为及如何配置以优化索引性能。通过实例展示了如何在查询时使用_all域和其他自定义域,以及如何禁用_all域以减少资源消耗。
1358

被折叠的 条评论
为什么被折叠?



