<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
為了方便學習, 本節中所有範例沿用上節的索引
GET class_1/_doc/1
查詢結果:
{ "_index" : "class_1", "_type" : "_doc", "_id" : "1", "_version" : 4, "_seq_no" : 4, "_primary_term" : 3, "found" : true, "_source" : { "name" : "l", "num" : 6 } }
GET class_1/_mget { "ids":[1,2,3] }
返回:
{ "docs" : [ { "_index" : "class_1", "_type" : "_doc", "_id" : "1", "_version" : 4, "_seq_no" : 4, "_primary_term" : 3, "found" : true, "_source" : { "name" : "l", "num" : 6 } }, { "_index" : "class_1", "_type" : "_doc", "_id" : "2", "found" : false }, { "_index" : "class_1", "_type" : "_doc", "_id" : "3", "_version" : 3, "_seq_no" : 10, "_primary_term" : 4, "found" : true, "_source" : { "num" : 9, "name" : "e", "age" : 9, "desc" : [ "hhhh" ] } } ] }
HEAD class_1/_doc/1
返回:
200 - OK
HEAD class_1/_doc/1000
返回:
404 - Not Found
GET class_1/_doc/1?_source_includes=name
返回:
{ "_index" : "class_1", "_type" : "_doc", "_id" : "1", "_version" : 4, "_seq_no" : 4, "_primary_term" : 3, "found" : true, "_source" : { "name" : "l" } }
可以看到只返回了name
欄位, 以上是一個基本的操作,下面給大家講下條件查詢~
查詢的複雜度取決於它附加的條件約束,跟我們寫sql
一樣。下面就帶大家一步一步看一下ES
中如何進行條件查詢~
GET class_1/_search
返回:
{ "took" : 15, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 8, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "class_1", "_type" : "_doc", "_id" : "h2Fg-4UBECmbBdQA6VLg", "_score" : 1.0, "_source" : { "name" : "b", "num" : 6 } }, { "_index" : "class_1", "_type" : "_doc", "_id" : "iGFt-4UBECmbBdQAnVJe", "_score" : 1.0, "_source" : { "name" : "g", "age" : 8 } }, { "_index" : "class_1", "_type" : "_doc", "_id" : "iWFt-4UBECmbBdQAnVJg", "_score" : 1.0, "_source" : { "name" : "h", "age" : 9 } }, { "_index" : "class_1", "_type" : "_doc", "_id" : "imFt-4UBECmbBdQAnVJg", "_score" : 1.0, "_source" : { "name" : "i", "age" : 10 } }, { "_index" : "class_1", "_type" : "_doc", "_id" : "3", "_score" : 1.0, "_source" : { "num" : 9, "name" : "e", "age" : 9, "desc" : [ "hhhh" ] } }, { "_index" : "class_1", "_type" : "_doc", "_id" : "4", "_score" : 1.0, "_source" : { "name" : "f", "age" : 10, "num" : 10 } }, { "_index" : "class_1", "_type" : "_doc", "_id" : "RWlfBIUBDuA8yW5cu9wu", "_score" : 1.0, "_source" : { "name" : "一年級", "num" : 20 } }, { "_index" : "class_1", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "name" : "l", "num" : 6 } } ] } }
可以看到索引class_1
中的所有資料都是上節新增的。這裡提一下,我們也可以新增多個索引一起查,然後返回,用,逗號
隔開就可以了
GET class_1,class_2,class_3/_search
{ "took" : 7, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 9, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "class_1", "_type" : "_doc", "_id" : "h2Fg-4UBECmbBdQA6VLg", "_score" : 1.0, "_source" : { "name" : "b", "num" : 6 } }, { "_index" : "class_2", "_type" : "_doc", "_id" : "RWlfBIUBDuA8yW5cu9wu", "_score" : 1.0, "_source" : { "name" : "一年級", "num" : 20 } }, .... ] } }
可以看到返回了索引class_2
中的資料,並且合併到了一起。
有的小夥伴可能對返回的欄位有點陌生,這裡給大家統一解釋一下:
{ "took":"查詢操作耗時,單位毫秒", "timed_out":"是否超時", "_shards":{ "total":"分片總數", "successful":"執行成功分片數", "skipped":"執行忽略分片數", "failed":"執行失敗分片數" }, "hits":{ "total":{ "value":"條件查詢命中數", "relation":"計數規則(eq計數準確/gte計數不準確)" }, "max_score":"最大匹配度分值", "hits":[ { "_index":"命中結果索引", "_id":"命中結果ID", "_score":"命中結果分數", "_source":"命中結果原檔案資訊" } ] } }
下面我們看下帶條件的查詢~
基本語法: es
中通過引數size
和from
來進行基礎分頁
的控制
from
:指定跳過
多少條資料size
:指定返回
多少條資料下面看下範例:
GET class_1/_search?from=2&size=2
返回:
{ "took" : 5, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 8, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "class_1", "_type" : "_doc", "_id" : "iWFt-4UBECmbBdQAnVJg", "_score" : 1.0, "_source" : { "name" : "h", "age" : 9 } }, { "_index" : "class_1", "_type" : "_doc", "_id" : "imFt-4UBECmbBdQAnVJg", "_score" : 1.0, "_source" : { "name" : "i", "age" : 10 } } ] } }
GET class_1/_search { "from" : 2, "size" : 2 }
返回結果和上面是一樣的~
這個大家應該不陌生,前面幾節都見過。使用query.match
進行查詢,match
適用與對單個欄位基於全文索引進行資料檢索。對於全文欄位,match
使用特定的分詞
進行全文檢索。而對於那些精確值,match
同樣可以進行精確匹配,match
查詢短語時,會對短語進行分詞,再針對每個詞條進行全文檢索。
GET class_1/_search { "query": { "match": { "name":"i" } } }
返回:
{ "took" : 4, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.3862942, "hits" : [ { "_index" : "class_1", "_type" : "_doc", "_id" : "imFt-4UBECmbBdQAnVJg", "_score" : 1.3862942, "_source" : { "name" : "i", "age" : 10 } } ] } }
使用query.match_phrase
進行查詢, 它與match
的區別就是不進行分詞,幹說,可能有點抽象,下面我們通過一個例子給大家分清楚:
先造點資料進去:
PUT class_1/_bulk { "create":{ } } {"name":"I eat apple so haochi1~","num": 1} { "create":{ } } { "name":"I eat apple so zhen haochi2~","num": 1} { "create":{ } } {"name":"I eat apple so haochi3~","num": 1}
假設有這麼幾個句子,現在我有一個需求我要把I eat apple so zhen haochi2~
這句話匹配出來
GET class_1/_search { "query": { "match": { "name": "apple so zhen" } } }
返回:
{ "took" : 15, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 3, "relation" : "eq" }, "max_score" : 2.2169428, "hits" : [ { "_index" : "class_1", "_type" : "_doc", "_id" : "cMfcCoYB090miyjed7YE", "_score" : 2.2169428, "_source" : { "name" : "I eat apple so zhen haochi2~", "num" : 1 } }, { "_index" : "class_1", "_type" : "_doc", "_id" : "b8fcCoYB090miyjed7YE", "_score" : 1.505254, "_source" : { "name" : "I eat apple so haochi1~", "num" : 1 } }, { "_index" : "class_1", "_type" : "_doc", "_id" : "ccfcCoYB090miyjed7YE", "_score" : 1.505254, "_source" : { "name" : "I eat apple so haochi3~", "num" : 1 } } ] } }
從結果來看,剛剛的幾句話都被查出來了,但是結果並大符合預期。從score
來看,"_score" : 2.2169428
得分最高,排在了第一,語句是I eat apple so zhen haochi2~
,說明匹配度最高,這個句子正是我們想要的結果~
GET class_1/_search { "query": { "match_phrase": { "name": "apple so zhen" } } }
結果:
{ "took" : 6, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 2.2169428, "hits" : [ { "_index" : "class_1", "_type" : "_doc", "_id" : "cMfcCoYB090miyjed7YE", "_score" : 2.2169428, "_source" : { "name" : "I eat apple so zhen haochi2~", "num" : 1 } } ] } }
結果符合預期,只返回了我們想要的那句。那麼match
為什麼都返回了,這就是前面講到的分詞
,首先會對name: apple so zhen
進行分詞,也就是說存在apple
的都會被返回。
當然,真正業務中的需求比這個複雜多了,這裡只是為了給大家做區分~ 下面接著看~
相當於對多個欄位執行了match
查詢, 這裡需要注意的是query
的型別要和欄位型別一致,不然會報型別異常
GET class_1/_search { "query": { "multi_match": { "query": "apple", "fields": ["name","desc"] } } }
{ "took" : 5, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 3, "relation" : "eq" }, "max_score" : 0.752627, "hits" : [ { "_index" : "class_1", "_type" : "_doc", "_id" : "b8fcCoYB090miyjed7YE", "_score" : 0.752627, "_source" : { "name" : "I eat apple so haochi1~", "num" : 1 } }, { "_index" : "class_1", "_type" : "_doc", "_id" : "ccfcCoYB090miyjed7YE", "_score" : 0.752627, "_source" : { "name" : "I eat apple so haochi3~", "num" : 1 } }, { "_index" : "class_1", "_type" : "_doc", "_id" : "cMfcCoYB090miyjed7YE", "_score" : 0.7389809, "_source" : { "name" : "I eat apple so zhen haochi2~", "num" : 1 } } ] } }
使用range
來進行範圍查詢,適用於陣列
,時間
等欄位
GET class_1/_search { "query": { "range": { "num": { "gt": 5, "lt": 10 } } } }
返回:
{ "took" : 6, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 3, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "class_1", "_type" : "_doc", "_id" : "h2Fg-4UBECmbBdQA6VLg", "_score" : 1.0, "_source" : { "name" : "b", "num" : 6 } }, { "_index" : "class_1", "_type" : "_doc", "_id" : "3", "_score" : 1.0, "_source" : { "num" : 9, "name" : "e", "age" : 9, "desc" : [ "hhhh" ] } }, { "_index" : "class_1", "_type" : "_doc", "_id" : "1", "_score" : 1.0, "_source" : { "name" : "l", "num" : 6 } } ] } }
使用term
進行非分詞欄位
的精確查詢。需要注意的是,對於那些分詞的欄位,即使查詢的value
是一個完全匹配的短語,也無法完成查詢
GET class_1/_search { "query": { "term": { "num": { "value": "9" } } } }
返回:
{ "took" : 4, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "class_1", "_type" : "_doc", "_id" : "3", "_score" : 1.0, "_source" : { "num" : 9, "name" : "e", "age" : 9, "desc" : [ "hhhh" ] } } ] } }
與term一樣, 區別在於可以匹配一個欄位的多個值,滿足一個即檢索成功
GET class_1/_search { "query": { "terms": { "num": [ 9, 1 ] } } }
返回:
{ "took" : 8, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "class_1", "_type" : "_doc", "_id" : "3", "_score" : 1.0, "_source" : { "num" : 9, "name" : "e", "age" : 9, "desc" : [ "hhhh" ] } }, { "_index" : "class_1", "_type" : "_doc", "_id" : "b8fcCoYB090miyjed7YE", "_score" : 1.0, "_source" : { "name" : "I eat apple so haochi1~", "num" : 1 } }, { "_index" : "class_1", "_type" : "_doc", "_id" : "ccfcCoYB090miyjed7YE", "_score" : 1.0, "_source" : { "name" : "I eat apple so haochi3~", "num" : 1 } }, { "_index" : "class_1", "_type" : "_doc", "_id" : "cMfcCoYB090miyjed7YE", "_score" : 1.0, "_source" : { "name" : "I eat apple so zhen haochi2~", "num" : 1 } } ] } }
為了確定當前索引有哪些檔案包含了對應的欄位,es
中使用exists
來實現
GET class_1/_search { "query": { "exists": { "field": "desc" } } }
返回:
{ "took" : 8, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "class_1", "_type" : "_doc", "_id" : "3", "_score" : 1.0, "_source" : { "num" : 9, "name" : "e", "age" : 9, "desc" : [ "hhhh" ] } } ] } }
本節主要講了ES
中的檔案查詢API操作,該部分內容較多, 下節繼續給大家講,就先消化這麼多~API
大家都不要去背,多敲幾遍就記住了,關鍵是多用,多總結 。
以上就是ElasticSearch查詢檔案基本操作範例的詳細內容,更多關於ElasticSearch查詢檔案的資料請關注it145.com其它相關文章!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45