source:https://www.elastic.co/guide/en/elasticsearch/reference/6.4/getting-started-explore-data.html
简单的数据集:
现在我们对基本概念有了一些了解,现在尝试处理一些更加真实的数据集,这里准备了一些简单虚拟的银行用户账号信息的json文档,每个文档都是一下的格式:
{
"account_number": 0,
"balance": 16623,
"firstname": "Bradshaw",
"lastname": "Mckenzie",
"age": 29,
"gender": "F",
"address": "244 Columbus Place",
"employer": "Euron",
"email": "bradshawmckenzie@euron.com",
"city": "Hobucken",
"state": "CO"
}
数据源来自于 www.json-generator.com
所以可以忽略数据的真实性,这都是随机生成的。
加载一些简单的数据集:
你可以从上面的网站下载一些简单的数据集,加载数据到我们的集群用以下方式:
curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_doc/_bulk?pretty&refresh" --data-binary "@accounts.json"
curl "localhost:9200/_cat/indices?v"
返回以下结果:
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open bank l7sSYV2cQXmu6_4rJWVIww 5 1 1000
这意味着我们成功的写入了1000条文档到我们的“银行”索引中。
搜索API
现在我们开始进行一些简单的搜索,有两种简单的搜索方式:
一个是通过REST request url发送一些搜索参数,
另一个是通过REST request body发送。
request body方法允许你更加实验性,通过可读的json来定义你的搜索模式。
我们尝试一个简单请求URL(第一种方法),但是在本书其他的部分中,我们用的是request body(第二种方法)
restAPI搜索通过_search结尾,这个栗子是返回bank索引中的所有文档:
curl -X GET "localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty&pretty"
让我首先来分析一下这个搜索语句
我们通过"_search"结尾来搜索银行索引,
"q=*"这个参数表示搜索所有的索引中的文档,
"sort=account_num:asc"这个参数表示对结果中的"account_num"进行升序排序。
"pretty"这个参数要求es尽可能返回一个json格式的结果。
结果如下:
{
"took" : 63,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1000,
"max_score" : null,
"hits" : [ {
"_index" : "bank",
"_type" : "_doc",
"_id" : "0",
"sort": [0],
"_score" : null,
"_source" : {"account_number":0,"balance":16623,"firstname":"Bradshaw","lastname":"Mckenzie","age":29,"gender":"F","address":"244 Columbus Place","employer":"Euron","email":"bradshawmckenzie@euron.com","city":"Hobucken","state":"CO"}
}, {
"_index" : "bank",
"_type" : "_doc",
"_id" : "1",
"sort": [1],
"_score" : null,
"_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"