1.安装
java升级到jdk8,下载es
https://www.elastic.co/downloads/elasticsearch
解压es压缩包,启动es
./bin/elasticsearch
访问ES:http://localhost:9200/
显示:
{
"name" : "pleYB0j",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "tRrh3XC_Rka1huf1zMOTSg",
"version" : {
"number" : "5.3.0",
"build_hash" : "3adb13b",
"build_date" : "2017-03-23T03:31:50.652Z",
"build_snapshot" : false,
"lucene_version" : "6.4.1"
},
"tagline" : "You Know, for Search"
}
2.ES命令
参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
ES命令格式:
curl -<REST Verb> <Node>:<Port>/<Index>/<Type><ID>
curl -<REST Verb> <Node>:<Port>/<Index>/<Type><ID>
2.1创建索引
curl -XPUT 'localhost:9200/customer?pretty'
第一个命令使用PUT创建了一个叫做“customer”的索引。我们简单地将pretty附加到调用的尾部,使其以美观的形式打印出JSON响应
访问:http://localhost:9200/_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open customer -D-eQGuNT-uG-g0MkuiBGg 5 1 0 0 650b 650b
第二个命令的结果告知我们,我们现在有一个叫做customer的索引,并且它有5个主分片和1份复制(都是默认值),其中包含0个文档。
2.2索引并查询文档
curl -XPUT 'localhost:9200/customer/external/1?pretty' -d ' {"name": "John Doe"}'
输出:
{
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : true
}
将一个简单的客户文档索引到customer索引、“external”类型中,这个文档的ID是1
取出文档:
curl -XGET 'localhost:9200/customer/external/1?pretty'
结果:
{
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"name" : "John Doe"
}
}
除了一个叫做found的字段来指明我们找到了一个ID为1的文档,和另外一个字段——_source——返回我们前一步中索引的完整JSON文档之外,其它的都没有什么特别之处。
2.3删除索引
curl -XDELETE 'localhost:9200/customer?pretty'
3.DEMO
导入测试数据
curl -XPOST 'localhost:9200/bank/account/_bulk?pretty' --data-binary @accounts.json
测试数据acounts.json,有两条数据,如下所示:
{"index":{"_id":"1"}}
{"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}
{"index":{"_id":"6"}}
{"account_number":6,"balance":5686,"firstname":"Hattie","lastname":"Bond","age":36,"gender":"M","address":"671 Bristol Street","employer":"Netagy","email":"hattiebond@netagy.com","city":"Dante","state":"TN"}
查询数据:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match": { "account_number": 1 } } }'
结果:
{
"took" : 11,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "bank",
"_type" : "account",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"account_number" : 1,
"balance" : 39225,
"firstname" : "Amber",
"lastname" : "Duke",
"age" : 32,
"gender" : "M",
"address" : "880 Holmes Lane",
"employer" : "Pyrami",
"email" : "amberduke@pyrami.com",
"city" : "Brogan",
"state" : "IL"
}
}
]
}
}