如何在Elasticsearch 1.7中默认开启doc_values?是否有对应配置参数?
Hey there, let's tackle your question about enabling doc_values by default in Elasticsearch 1.7. First off, the short answer: there's no global setting in elasticsearch.yml that lets you turn on doc_values for all fields across the board. But don't worry—we've got two solid workarounds to make this happen.
Option 1: Use an Index Template for Global Defaults
Index templates are the way to go if you want every new index you create to automatically enable doc_values for specific field types. Here's how to set it up:
Step 1: Create the Template
Send this PUT request to your ES cluster:
PUT _template/default_doc_values { "template": "*", "settings": { "index.number_of_shards": 1 }, "mappings": { "_default_": { "dynamic_templates": [ { "string_fields_with_doc_values": { "match": "*", "match_mapping_type": "string", "mapping": { "type": "string", "doc_values": true } } }, { "numeric_fields_with_doc_values": { "match": "*", "match_mapping_type": "long,integer,short,byte,double,float", "mapping": { "type": "{dynamic_type}", "doc_values": true } } } ] } } }
This template applies to every new index (thanks to "template": "*"). It targets string and numeric fields, automatically enabling doc_values for them. You can extend this to other types like date by adding another entry to dynamic_templates with the appropriate match_mapping_type.
Step 2: Verify It Works
Create a test index and add a document, then check the mapping:
PUT test_index/_doc/1 { "username": "johndoe", "age": 30 } GET test_index/_mapping
You should see that both username and age have "doc_values": true in their mapping definitions.
Option 2: Explicitly Configure doc_values for Existing Indices
If you're dealing with already-created indices, things are a bit trickier: you can't modify the doc_values setting for existing fields—it changes the underlying storage structure, which ES doesn't allow on the fly. Instead, you'll need to:
- Create a new index with the desired mapping (explicitly setting
doc_values: truefor your fields) - Migrate data from the old index to the new one
Here's an example of the new index mapping:
PUT my_new_index { "mappings": { "_doc": { "properties": { "product_name": { "type": "string", "doc_values": true }, "price": { "type": "float", "doc_values": true } } } } }
Since Elasticsearch 1.7 doesn't have the native _reindex API (that came later in 5.x), you'll need to use the scroll API to batch export data from the old index, then the bulk API to import it into the new one. Simple scripts can automate this process if you have large datasets.
Quick Notes
- In ES 1.7, doc_values are disabled by default for most field types—so manual configuration is a must.
- Enabling doc_values does increase disk usage, but it drastically improves performance for aggregations, sorting, and filtering operations. It's well worth it for fields you frequently use in those contexts.
内容的提问来源于stack exchange,提问作者ram

