如何导入特定格式的Elasticsearch数据库定义文件?
How to Import Your Elasticsearch Definitions File
Your file contains three separate Elasticsearch API requests: creating the drafts index, defining its mapping, and setting up the variants index with custom analysis settings. Here are the most straightforward ways to import these into your cluster:
Method 1: Use Kibana Dev Tools (Recommended for Simplicity)
If you have Kibana connected to your Elasticsearch cluster, this is the easiest approach:
- Open Kibana and click the Dev Tools icon (wrench symbol) in the left sidebar.
- Clear any existing text in the console pane.
- Paste your entire file content directly into the console.
- Hit the Play button (▶️) or press
Ctrl+Enterto run all requests in sequence. - Check the responses below—look for
200 OKor201 Createdstatuses to confirm each step worked.
Method 2: Use Curl via a Shell Script
For command-line users without Kibana, wrap each request in a curl command and run them as a script:
- Save your original content to a file (e.g.,
es_setup.txtfor reference). - Create a new shell script (e.g.,
setup_es.sh) with the following code, replacinghttp://your-es-host:9200with your actual Elasticsearch cluster URL:#!/bin/bash ES_URL="http://your-es-host:9200" # Create the drafts index curl -X PUT "$ES_URL/drafts" -H "Content-Type: application/json" -d '{ "settings": { "max_result_window" : "100000" } }' echo -e "\n---\n" # Set the draft mapping for the drafts index curl -X PUT "$ES_URL/drafts/draft/_mapping" -H "Content-Type: application/json" -d '{ "draft":{ "properties":{ "id":{ "type":"keyword" }, "analysis_id":{ "type":"keyword" } } } }' echo -e "\n---\n" # Create the variants index with custom analysis settings curl -X PUT "$ES_URL/variants" -H "Content-Type: application/json" -d '{ "settings": { "analysis": { "normalizer": { "lowercase_normalizer": { "type": "custom", "char_filter": [], "filter": ["lowercase"] } } }, "max_result_window" : "100000" } }' - Make the script executable:
chmod +x setup_es.sh - Run the script:
./setup_es.sh - Verify each response confirms successful creation or update.
Method 3: Use the Elasticsearch Bulk API
For larger batches of requests, you can use the bulk endpoint by reformatting your file to match its required structure:
- Create a new file (e.g.,
bulk_setup.json) with this formatted content:{"put": {"_index": "drafts"}} {"settings": { "max_result_window" : "100000" }} {"put": {"_index": "drafts", "_type": "draft", "_mapping": true}} {"draft":{ "properties":{ "id":{ "type":"keyword" }, "analysis_id":{ "type":"keyword" } } }} {"put": {"_index": "variants"}} {"settings": { "analysis": { "normalizer": { "lowercase_normalizer": { "type": "custom", "char_filter": [], "filter": ["lowercase"] } } }, "max_result_window" : "100000" }} - Send it to Elasticsearch with curl:
curl -X POST "http://your-es-host:9200/_bulk" -H "Content-Type: application/json" --data-binary @bulk_setup.json - Review the bulk response to ensure all operations succeeded.
Key Notes:
- If your cluster uses authentication, add
-u username:passwordto your curl commands, or log into Kibana with valid credentials. - If you get "index already exists" errors, either delete the existing indices first (use
DELETE /draftsandDELETE /variantsin Dev Tools/curl) or append?ignore_already_exists=trueto your PUT index requests. - Ensure your Elasticsearch version supports the settings you’re using (e.g., custom normalizers are available in ES 5.0+).
内容的提问来源于stack exchange,提问作者thondeboer
相关产品推荐
相关产品推荐

