ElasticSearch官方为我们准备了一部分测试数据供调试使用,我们可以在安装完成Kinaba后进行数据导入处理
打开 https://github.com/elastic/elasticsearch/blob/master/docs/src/test/resources/accounts.json
复制全部数据(点击 Raw 按钮,新页面 Ctrl + A)
打开 Kinaba :xxxx:5601/app/dev_tools#/console,第一行输入下面第一行,第二行开始粘贴测试数据,点击 ▶️ 运行
POST /bank/account/_bulk
{"index":{"_id":"1"}}
{"account_number":1,"balance":39225,"f....(测试数据)
一个是通过使用 REST request URI发送搜索参数(uri+检索参数)
另一个是通过使用 REST request body来发送它们(uri请求体)
例子1:
GET bank/_search?q=*&sort=account_number:asc
返回结果:
结果并不会返回所有数据而是返回10条数据,类似于分页
{
"took" : 43,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1000,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "bank",
"_type" : "account",
"_id" : "0",
"_score" : null,
"_source" : {
"account_number" : 0,
"balance" : 16623,
"firstname" : "Bradshaw",
"lastname" : "Mckenzie",
"age" : 29,
"gender" : "F",
"address" : "244 Columbus Place",
"employer" : "Euron",
"email" : "[email protected]",
"city" : "Hobucken",
"state" : "CO"
},
"sort" : [
0
]
},
...
例子2:
先按照account_number进行降序,如果相同按照balance进行降序
GET bank/_search
{
"query": {"match_all": {}},
"sort": [
{
"account_number": "desc"
},
{
"balance": "desc"
}
]
}
返回结果:
{
"took" : 12,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1000,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "bank",
"_type" : "account",
"_id" : "999",
"_score" : null,
"_source" : {
"account_number" : 999,
"balance" : 6087,
"firstname" : "Dorothy",
...
ElasticSearch提供了一个可以执行查询的son风的DSL(domain-specific language领域特定语言)。这个被称为 Query DSL。该查询语言非常全面,并且刚开始的时候感觉有点复杂,真正学好它的方法是从一些基础的示例开始的。
一个查询语句的典型结构:
{
QUERY_NAME:{
ARGUMENT:VALUE,
ARGUMENT:VALUE...
}
}
例如查询所有:
GET bank/_search
{
"query": {
"match_all": {}
}
}
如果是针对某个字段查询,查询结构:
{
QUERY_NAME:{
FIELD NAME:{
ARGUMENI:VALUE,
ARGUMENT:VALUE...
}
}
}
例如按照 balance 降序查询:
GET bank/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"balance": {
"order": "desc"
}
}
]
}
其实还有一种简单的表达形式,效果相同
"balance": {
"order": "desc"
}
可以简写为:
"balance": "desc"
分页查询且只查询部分属性的例子:
GET bank/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"balance": {
"order": "desc"
}
}
],
"from": 0,
"size": 5,
"_source": ["balance", "account_number"]
}