ES 数据搜索(1)
1.准备数据:JSON生成器:www.json-generator.com/
下载官网原数据:https://raw.githubusercontent.com/elastic/elasticsearch/master/docs/src/test/resources/accounts.json
curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/account/_bulk?pretty&refresh" --data-binary "@accounts.json"
2.两种search请求方式:
REST Request URI
|
q=*匹配所有文档 sort 按account_number字段排序 升序 The |
curl -XGET 'localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty&pretty' |
|
{
"took" : 63,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1000,
"max_score" : null,
"hits" : [ {
"_index" : "bank",
"_type" : "account",
"_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" : "account",
"_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":"Brogan","state":"IL"}
}, ...
]
}
}
|
REST Request body
|
同上的rest请求体方式: query:请求全部 sort:按account_number升序排序 |
curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d' |
|
curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d' |
|
| 只写from |
curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d' |
| from+size |
curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d' |
| sort |
curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d' |
| 只返回关注的字段:account_number,balance |
curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d' |
| match query:匹配查询 返回account_num=20的结果 |
curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d' |
| 匹配查询 返回address包含mill or lane的结果 |
curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d' |
| 匹配:address包含:mill and lane |
curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d' |
| bool查询:must 都匹配 a and b |
curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d' {
"query": {
"bool": {
"must": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
|
| bool查询:should 可以匹配:a or b |
curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d' {
"query": {
"bool": {
"should": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
|
|
bool查询:must_not 都不匹配 |
curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d' {
"query": {
"bool": {
"must_not": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
|
| bool查询:混合查询 |
curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d' {
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}
|
| filter查询:落入filter范围的文档全部等价,不存在谁的score比谁的高 |
GET / bank / _search { “query”:{ “bool”:{ “must”:{“match_all”:{}}, “filter”:{ “range”:{ “balance”:{ “gte” “lte”:30000 } } } } } } |

浙公网安备 33010602011771号