Elasticsearch7.1索引中如何定义类型?1.6版本升级疑问
Hey there! Great question—this is a super common hurdle when moving from older Elasticsearch versions to 7.x. Let’s break down what changed and how to adapt your workflow:
Key Background: Types Are Gone in 7.x
Starting with Elasticsearch 7.0, the concept of document types (like your tweet type under the twitter index) was completely removed. This was a gradual deprecation that started in 6.x, where each index was limited to a single type, and 7.x finished the job by eliminating types entirely.
By default, the only allowed "type" identifier in 7.x is _doc—it’s essentially a placeholder for compatibility, but it no longer carries the same meaning as types did in 1.6.
How to Replace Your Old Type Structure
You have two main options to replicate your old type-based setup, depending on your use case:
Option 1: Create Separate Indexes for Each Old Type (Recommended)
This is the official preferred approach. If your old types (like tweet vs. a hypothetical user type under twitter) had distinct mappings or data shapes, splitting them into separate indexes (e.g., tweets and users) makes sense. It avoids field conflicts and improves performance since each index can be optimized for its specific data.
Example (Old 1.6 Syntax):
PUT twitter/tweet/1 { "user": "kimchy", "post_date": "2009-11-15T14:12:12", "message": "trying out Elasticsearch" }
7.1 Equivalent (Separate Index):
PUT tweets/_doc/1 { "user": "kimchy", "post_date": "2009-11-15T14:12:12", "message": "trying out Elasticsearch" }
Option 2: Add a Custom "Type" Field to Documents
If your old types shared similar structures and you want to keep them in a single index, add a custom field (e.g., doc_type) to each document to mimic the old type behavior. You can then filter queries by this field to get results equivalent to a specific old type.
7.1 Example with Custom Field:
PUT combined_twitter_data/_doc/1 { "doc_type": "tweet", "user": "kimchy", "post_date": "2009-11-15T14:12:12", "message": "trying out Elasticsearch" }
Query to Filter by "Type":
GET combined_twitter_data/_search { "query": { "term": { "doc_type": "tweet" } } }
Important Compatibility Note
While you can temporarily use custom type names (instead of _doc) by setting include_type_name: true when creating an index, this is a legacy compatibility flag. It’s not recommended for new setups, as it will be removed entirely in Elasticsearch 8.x.
内容的提问来源于stack exchange,提问作者sarah w

