到elasticsearch网站下载最新版本的elasticsearch 6.2.1
https://www.elastic.co/downloads/elasticsearch
下载tar包,然后解压到/usr/local目录下,修改一下用户和组之后可以使用非root用户启动,启动命令
./bin/elasticsearch
然后访问http://127.0.0.1:9200/
接下来导入json格式的数据,数据内容如下
{"index":{"_id":"1"}} {"title":"许宝江","url":"7254863","chineseName":"许宝江","sex":"男","occupation":" 滦县农业局局长","nationality":"中国"} {"index":{"_id":"2"}} {"title":"鲍志成","url":"2074015","chineseName":"鲍志成","occupation":"医师","nationality":"中国","birthDate":"1901年","deathDate":"1973年","graduatedFrom":"香港大学"}
需要注意的是{"index":{"_id":"1"}}和文件末尾另起一行换行是不可少的
否则会出现400状态,错误提示分别为
Malformed action/metadata line [1], expected START_OBJECT or END_OBJECT but found [VALUE_STRING]
The bulk request must be terminated by a newline [\n]"
使用下面命令来导入json文件
其中的people.json为文件的路径,可以是/home/common/下载/xxx.json
其中的es是index,people是type,在elasticsearch中的index和type可以理解成关系数据库中的database和table,两者都是必不可少的
curl -H "Content-Type: application/json" -XPOST ‘localhost:9200/es/people/_bulk?pretty&refresh‘ --data-binary "@people.json"
成功后的返回值是200,比如
{ "took" : 233, "errors" : false, "items" : [ { "index" : { "_index" : "es", "_type" : "people", "_id" : "1", "_version" : 1, "result" : "created", "forced_refresh" : true, "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1, "status" : 201 } }, { "index" : { "_index" : "es", "_type" : "people", "_id" : "2", "_version" : 1, "result" : "created", "forced_refresh" : true, "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1, "status" : 201 } } ] }
接下来可以使用对应的查询语句对数据进行查询
原文地址:https://www.cnblogs.com/tonglin0325/p/8446975.html
时间: 2024-10-06 14:58:31