ElasticSearch Bulk导入失败:parse_exception错误排查求助
问题分析与解决方法
错误原因
- 请求方法错误:Elasticsearch的
_bulk接口需使用POST请求,你误用了PUT,导致Elasticsearch误将请求解析为创建索引操作,进而无法识别create关键字。 - 批量数据格式不合法:
- Elasticsearch bulk要求每行一个独立JSON对象,不能将操作元数据(如
create语句)和文档数据放在同一行。 - 你的JSON文件中包含多余的
和>符号,破坏了JSON结构。 - 部分JSON未正确换行,导致解析失败。
- Elasticsearch bulk要求每行一个独立JSON对象,不能将操作元数据(如
解决步骤
1. 修正请求命令
将PUT改为POST,同时推荐使用application/x-ndjson作为Content-Type(适配换行分隔的JSON格式):
curl --location --request POST '127.0.0.1:9200/_bulk' \ --header 'Content-Type: application/x-ndjson' \ --data '@/Users/themis/Downloads/ml-latest-small/movies.json'
2. 修正JSON文件格式
清理多余符号,确保每行一个JSON对象,修正后的movies.json内容如下:
{ "create" : { "_index" : "movies", "_id" : "135569" } } { "id": "135569", "title" : "Star Trek Beyond", "year":2016 , "genre":["Action", "Adventure", "Sci-Fi"] } { "create" : { "_index" : "movies", "_id" : "122886" } } { "id": "122886", "title" : "Star Wars: Episode VII - The Force Awakens", "year":2015 , "genre":["Action", "Adventure", "Fantasy", "Sci-Fi", "IMAX"] } { "create" : { "_index" : "movies", "_id" : "109487" } } { "id": "109487", "title" : "Interstellar", "year":2014 , "genre":["Sci-Fi", "IMAX"] } { "create" : { "_index" : "movies", "_id" : "58559" } } { "id": "58559", "title" : "Dark Knight, The", "year":2008 , "genre":["Action", "Crime", "Drama", "IMAX"] } { "create" : { "_index" : "movies", "_id" : "1924" } } { "id": "1924", "title" : "Plan 9 from Outer Space", "year":1959 , "genre":["Horror", "Sci-Fi"] }
3. Postman配置调整
如果你用Postman操作:
- 选择
POST请求方法 - URL填入
http://127.0.0.1:9200/_bulk - Headers添加
Content-Type: application/x-ndjson - Body选择
raw并粘贴修正后的JSON内容,或选择form-data上传修正后的文件
内容的提问来源于stack exchange,提问作者Jack Daniel
相关产品推荐
相关产品推荐

