1、索引相關(guān)
查看集群狀態(tài)
http://127.0.0.1:9200/_cluster/health
創(chuàng)建索引
curl -XPUT 10.9.39.37:9200/test_cycle-order_20227_1
查看所有索引
http://127.0.0.1:9200/_cat/indices
查看索引信息
http://127.0.0.1:9200/index_name
{
"user-info": {
"aliases": {},
"mappings": {
"_doc": {
"properties": {
"address": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"age": {
"type": "long"
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"sex": {
"type": "long"
}
}
}
},
"settings": {
"index": {
"creation_date": "1670915768780",
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "An2CYpuxSq-6sh0vWRjS2g",
"version": {
"created": "6040299"
},
"provided_name": "user-info"
}
}
}
}
刪除索引
curl -XDELETE 127.0.0.1:9200/index_name
2、高級(jí)查詢 DSL
2.1 查詢所有 match_all
使用match_all,默認(rèn)只會(huì)返回10條數(shù)據(jù)
curl -X GET 'http://10.9.39.37:9200/user-info/_doc/_search' \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"query":{
"match_all":{
}
}
}'
{
//查詢花費(fèi)的總時(shí)間
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
//符合條件的總文檔數(shù)
"total": 1,
"max_score": 1.0,
//結(jié)果集,默認(rèn)10
"hits": [
{
"_index": "user-info",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"name": "張三",
"age": 18,
"address": "北京市朝陽區(qū)",
"sex": 1
}
}
]
}
}
返回指定條數(shù)
{
"query": {
"match_all": {}
},
"size":50
}
size 不能無限大,如果過大會(huì)出現(xiàn)異常
{
"error": {
"root_cause": [
{
"type": "query_phase_execution_exception",
"reason": "Result window is too large, from + size must be less than or equal to: [10000] but was [99999]. See the scroll api for a more efficient way to request large data sets. This limit can be set by changing the [index.max_result_window] index level setting."
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "user-info",
"node": "uyvZR82uTVGEdPxthCCYHg",
"reason": {
"type": "query_phase_execution_exception",
"reason": "Result window is too large, from + size must be less than or equal to: [10000] but was [99999]. See the scroll api for a more efficient way to request large data sets. This limit can be set by changing the [index.max_result_window] index level setting."
}
}
]
},
"status": 500
}
1、查詢結(jié)果的窗口太大,from + size的結(jié)果必須小于或等于10000,而當(dāng)前查詢結(jié)果的窗
口為20000。
2、可以采用scroll api更高效的請(qǐng)求大量數(shù)據(jù)集。
3、查詢結(jié)果的窗口的限制可以通過參數(shù)index.max_result_window進(jìn)行設(shè)置。
注意:參數(shù)index.max_result_window主要用來限制單次查詢滿足查詢條件的結(jié)果窗口的 大小,窗口大小由from + size共同決定。不能簡單理解成查詢返回給調(diào)用方的數(shù)據(jù)量。這 樣做主要是為了限制內(nèi)存的消耗。 比如:from為1000000,size為10,邏輯意義是從滿足條件的數(shù)據(jù)中取1000000到 (1000000 + 10)的記錄。這時(shí)ES一定要先將(1000000 + 10)的記錄(即 result_window)加載到內(nèi)存中,再進(jìn)行分頁取值的操作。盡管最后我們只取了10條數(shù)據(jù)返 回給客戶端,但ES進(jìn)程執(zhí)行查詢操作的過程中確需要將(1000000 + 10)的記錄都加載到 內(nèi)存中,可想而知對(duì)內(nèi)存的消耗有多大。這也是ES中不推薦采用(from + size)方式進(jìn)行 深度分頁的原因。
同理,from為0,size為1000000時(shí),ES進(jìn)程執(zhí)行查詢操作的過程中確需要將1000000 條 記錄都加載到內(nèi)存中再返回給調(diào)用方,也會(huì)對(duì)ES內(nèi)存造成很大壓力。
2.2 分頁查詢 from
{
"query": {
"match_all": {}
},
"from": 0,
"size": 9999
}
2.3 深分頁查詢 scroll
最佳實(shí)踐還是根據(jù)異常提示中的采用scroll api更高效的請(qǐng)求大量數(shù)據(jù)集。
- 查詢命令中新增scroll=3000ms,說明采用游標(biāo)查詢,保持游標(biāo)查詢窗口3000毫秒
- 這里由于測(cè)試數(shù)據(jù)量不夠,所以size值設(shè)置為
- 實(shí)際使用中為了減少游標(biāo)查詢的次數(shù),可以將size值適當(dāng)增大,比如設(shè)置為1000
curl -X GET 'http://10.9.39.37:9200/user-info/_doc/_search?scroll=3000ms' \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"query": {
"match_all": {}
},
"size": 2
}'
{
"_scroll_id": "DnF1ZXJ5VGhlbkZldGNoBQAAAAACCBzjFnV5dlpSODJ1VFZHRWRQeHRoQ0NZSGcAAAAAB9aKqBZzVkIwYk5kMlNVbWwybnJDaXZwb1VnAAAAAAqiPgUWaUxYa0tPcHZUaHE0bm16TUpZVkpOZwAAAAACCBziFnV5dlpSODJ1VFZHRWRQeHRoQ0NZSGcAAAAAB9aKpxZzVkIwYk5kMlNVbWwybnJDaXZwb1Vn",
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 7,
"max_score": 1.0,
"hits": [
{
"_index": "user-info",
"_type": "_doc",
"_id": "5",
"_score": 1.0,
"_source": {
"name": "1211111",
"age": 18,
"address": "北京市朝陽區(qū)",
"sex": 1
}
},
{
"_index": "user-info",
"_type": "_doc",
"_id": "8",
"_score": 1.0,
"_source": {
"name": "娃娃",
"age": 18,
"address": "北京市朝陽區(qū)",
"sex": 1
}
}
]
}
}
除了返回前2條記錄,還返回了一個(gè)游標(biāo)ID值_scroll_id。
采用游標(biāo)ID查詢
curl -X GET 'http://10.9.39.37:9200/_search/scroll' \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"scroll": "1m",
"scroll_id": "DnF1ZXJ5VGhlbkZldGNoBQAAAAACCDZHFnV5dlpSODJ1VFZHRWRQeHRoQ0NZSGcAAAAAB9aaaxZzVkIwYk5kMlNVbWwybnJDaXZwb1VnAAAAAAqiVaMWaUxYa0tPcHZUaHE0bm16TUpZVkpOZwAAAAACCDZIFnV5dlpSODJ1VFZHRWRQeHRoQ0NZSGcAAAAAB9aabBZzVkIwYk5kMlNVbWwybnJDaXZwb1Vn"
}'
{
"_scroll_id": "DnF1ZXJ5VGhlbkZldGNoBQAAAAACCDZHFnV5dlpSODJ1VFZHRWRQeHRoQ0NZSGcAAAAAB9aaaxZzVkIwYk5kMlNVbWwybnJDaXZwb1VnAAAAAAqiVaMWaUxYa0tPcHZUaHE0bm16TUpZVkpOZwAAAAACCDZIFnV5dlpSODJ1VFZHRWRQeHRoQ0NZSGcAAAAAB9aabBZzVkIwYk5kMlNVbWwybnJDaXZwb1Vn",
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 8,
"max_score": 1.0,
"hits": []
}
}
多次根據(jù)scroll_id游標(biāo)查詢,直到?jīng)]有數(shù)據(jù)返回則結(jié)束查詢。采用游標(biāo)查詢索引全量數(shù)據(jù), 更安全高效,限制了單次對(duì)內(nèi)存的消耗。
2.3 指定字段排序 sort
注意:會(huì)讓得分失效
curl -X GET 'http://10.9.39.37:9200/user-info/_doc/_search' \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"query": {
"match_all": {}
},
"size": 5,
"sort":[
{
"age":"desc"
}
]
}'
排序分頁
{
"query": {
"match_all": {}
},
"size": 5,
"from": 5,
"sort": [
{
"age": "desc"
}
]
}
2.4 返回指定字段_source
{
"query": {
"match_all": {}
},
"size": 5,
"from": 5,
"sort": [
{
"age": "desc"
}
],
"_source":["age","name"]
}
2.5 關(guān)鍵詞 match
match在匹配時(shí)會(huì)對(duì)所查找的關(guān)鍵詞進(jìn)行分詞,然后按分詞匹配查找
{
"query": {
"match": {
"age":18
}
},
"size": 5,
"from": 5,
"sort": [
{
"age": "desc"
}
],
"_source":["age","name"]
}
2.5 多字段查詢 multi_match
{
"query": {
"multi_match": {
"query": "張三",
"fields": [
"name",
"address"
]
}
},
"size": 5,
"from": 5,
"sort": [
{
"age": "desc"
}
],
"_source": [
"age",
"name"
]
}
2.6 query_string
允許我們?cè)趩蝹€(gè)查詢字符串中指定AND | OR | NOT條件,同時(shí)也和 multi_match query 一樣,支持多字段搜索。和match類似,但是match需要指定字段名,query_string是在所 有字段中搜索,范圍更廣泛
{
"query": {
"query_string": {
"query": "張三 or 娃娃22211"
}
},
"size": 100,
"from": 0,
"sort": [
{
"age": "desc"
}
],
"_source": [
"age",
"name"
]
}
指定單個(gè)字段查詢
{
"query": {
"query_string": {
"default_field": "address",
"query": "張三 or 娃娃22211"
}
},
"size": 100,
"from": 0,
"sort": [
{
"age": "desc"
}
],
"_source": [
"age",
"name"
]
}
指定多個(gè)字段查詢
{
"query": {
"query_string": {
"fields": [
"name",
"address"
],
"query": "張三 or 娃娃22211"
}
},
"size": 100,
"from": 0,
"sort": [
{
"age": "desc"
}
],
"_source": [
"age",
"name"
]
}
2.6 關(guān)鍵詞查詢 term
Term用來使用關(guān)鍵詞查詢(精確匹配),還可以用來查詢沒有被進(jìn)行分詞的數(shù)據(jù)類型。Term是 表達(dá)語意的最小單位,搜索和利用統(tǒng)計(jì)語言模型進(jìn)行自然語言處理都需要處理Term。 match在匹配時(shí)會(huì)對(duì)所查找的關(guān)鍵詞進(jìn)行分詞,然后按分詞匹配查找,而term會(huì)直接對(duì)關(guān) 鍵詞進(jìn)行查找。一般模糊查找的時(shí)候,多用match,而精確查找時(shí)可以使用term文章來源:http://www.zghlxwxcb.cn/news/detail-459122.html
{
"query": {
"term": {
"name": {
"value": "張三"
}
}
},
"size": 100,
"from": 0,
"sort": [
{
"age": "desc"
}
]
}
采用term精確查詢, 查詢字段映射類型為keyword文章來源地址http://www.zghlxwxcb.cn/news/detail-459122.html
{
"query": {
"term": {
"address.keyword": {
"value": "北京市朝陽區(qū)"
}
}
},
"size": 100,
"from": 0,
"sort": [
{
"age": "desc"
}
]
}
到了這里,關(guān)于ES常用命令與常用查詢(1)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!