ElasticSearch查詢文檔基本操作實(shí)例
查詢文檔 & 基本操作
為了方便學(xué)習(xí), 本節(jié)中所有示例沿用上節(jié)的索引
按照ID單個(gè)
GET class_1/_doc/1
查詢結(jié)果:
{
"_index" : "class_1",
"_type" : "_doc",
"_id" : "1",
"_version" : 4,
"_seq_no" : 4,
"_primary_term" : 3,
"found" : true,
"_source" : {
"name" : "l",
"num" : 6
}
}
按照ID批量
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"
]
}
}
]
}
查詢文檔是否存在 & 通過(guò)id判斷
HEAD class_1/_doc/1
返回:
200 - OK
HEAD class_1/_doc/1000
返回:
404 - Not Found
查詢部分字段內(nèi)容
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字段, 以上是一個(gè)基本的操作,下面給大家講下條件查詢~
查詢文檔 & 條件查詢
查詢的復(fù)雜度取決于它附加的條件約束,跟我們寫(xiě)sql一樣。下面就帶大家一步一步看一下ES中如何進(jìn)行條件查詢~
不附加任何條件
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" : "一年級(jí)",
"num" : 20
}
},
{
"_index" : "class_1",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "l",
"num" : 6
}
}
]
}
}
可以看到索引class_1中的所有數(shù)據(jù)都是上節(jié)添加的。這里提一下,我們也可以添加多個(gè)索引一起查,然后返回,用,逗號(hào)隔開(kāi)就可以了
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" : "一年級(jí)",
"num" : 20
}
},
....
]
}
}
可以看到返回了索引class_2中的數(shù)據(jù),并且合并到了一起。
相關(guān)字段解釋
有的小伙伴可能對(duì)返回的字段有點(diǎn)陌生,這里給大家統(tǒng)一解釋一下:
{
"took":"查詢操作耗時(shí),單位毫秒",
"timed_out":"是否超時(shí)",
"_shards":{
"total":"分片總數(shù)",
"successful":"執(zhí)行成功分片數(shù)",
"skipped":"執(zhí)行忽略分片數(shù)",
"failed":"執(zhí)行失敗分片數(shù)"
},
"hits":{
"total":{
"value":"條件查詢命中數(shù)",
"relation":"計(jì)數(shù)規(guī)則(eq計(jì)數(shù)準(zhǔn)確/gte計(jì)數(shù)不準(zhǔn)確)"
},
"max_score":"最大匹配度分值",
"hits":[
{
"_index":"命中結(jié)果索引",
"_id":"命中結(jié)果ID",
"_score":"命中結(jié)果分?jǐn)?shù)",
"_source":"命中結(jié)果原文檔信息"
}
]
}
}
下面我們看下帶條件的查詢~
基礎(chǔ)分頁(yè)查詢
基本語(yǔ)法: es中通過(guò)參數(shù)size和from來(lái)進(jìn)行基礎(chǔ)分頁(yè)的控制
from:指定跳過(guò)多少條數(shù)據(jù)size:指定返回多少條數(shù)據(jù)
下面看下示例:
url參數(shù)
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
}
}
]
}
}
body 參數(shù)
GET class_1/_search
{
"from" : 2,
"size" : 2
}
返回結(jié)果和上面是一樣的~
單字段全文索引查詢
這個(gè)大家應(yīng)該不陌生,前面幾節(jié)都見(jiàn)過(guò)。使用query.match進(jìn)行查詢,match適用與對(duì)單個(gè)字段基于全文索引進(jìn)行數(shù)據(jù)檢索。對(duì)于全文字段,match使用特定的分詞進(jìn)行全文檢索。而對(duì)于那些精確值,match同樣可以進(jìn)行精確匹配,match查詢短語(yǔ)時(shí),會(huì)對(duì)短語(yǔ)進(jìn)行分詞,再針對(duì)每個(gè)詞條進(jìn)行全文檢索。
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進(jìn)行查詢, 它與match的區(qū)別就是不進(jìn)行分詞,干說(shuō),可能有點(diǎn)抽象,下面我們通過(guò)一個(gè)例子給大家分清楚:
先造點(diǎn)數(shù)據(jù)進(jìn)去:
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}
假設(shè)有這么幾個(gè)句子,現(xiàn)在我有一個(gè)需求我要把I eat apple so zhen haochi2~這句話匹配出來(lái)
match分詞結(jié)果
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
}
}
]
}
}
從結(jié)果來(lái)看,剛剛的幾句話都被查出來(lái)了,但是結(jié)果并大符合預(yù)期。從score來(lái)看,"_score" : 2.2169428得分最高,排在了第一,語(yǔ)句是I eat apple so zhen haochi2~,說(shuō)明匹配度最高,這個(gè)句子正是我們想要的結(jié)果~
match_phrase 不分詞查詢結(jié)果
GET class_1/_search
{
"query": {
"match_phrase": {
"name": "apple so zhen"
}
}
}
結(jié)果:
{
"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
}
}
]
}
}
結(jié)果符合預(yù)期,只返回了我們想要的那句。那么match為什么都返回了,這就是前面講到的分詞,首先會(huì)對(duì)name: apple so zhen進(jìn)行分詞,也就是說(shuō)存在apple的都會(huì)被返回。
當(dāng)然,真正業(yè)務(wù)中的需求比這個(gè)復(fù)雜多了,這里只是為了給大家做區(qū)分~ 下面接著看~
多字段全文索引查詢
相當(dāng)于對(duì)多個(gè)字段執(zhí)行了match查詢, 這里需要注意的是query的類(lèi)型要和字段類(lèi)型一致,不然會(huì)報(bào)類(lèi)型異常
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來(lái)進(jìn)行范圍查詢,適用于數(shù)組,時(shí)間等字段
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進(jìn)行非分詞字段的精確查詢。需要注意的是,對(duì)于那些分詞的字段,即使查詢的value是一個(gè)完全匹配的短語(yǔ),也無(wú)法完成查詢
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一樣, 區(qū)別在于可以匹配一個(gè)字段的多個(gè)值,滿足一個(gè)即檢索成功
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
}
}
]
}
}
文檔包含字段查詢
為了確定當(dāng)前索引有哪些文檔包含了對(duì)應(yīng)的字段,es中使用exists來(lái)實(shí)現(xiàn)
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"
]
}
}
]
}
}
結(jié)束語(yǔ)
本節(jié)主要講了ES中的文檔查詢API操作,該部分內(nèi)容較多, 下節(jié)繼續(xù)給大家講,就先消化這么多~API大家都不要去背,多敲幾遍就記住了,關(guān)鍵是多用,多總結(jié) 。
以上就是ElasticSearch查詢文檔基本操作實(shí)例的詳細(xì)內(nèi)容,更多關(guān)于ElasticSearch查詢文檔的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
關(guān)于Mybatis-Plus?Update更新策略問(wèn)題
這篇文章主要介紹了關(guān)于Mybatis-Plus?Update更新策略問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Spring-AOP 靜態(tài)正則表達(dá)式方法如何匹配切面
這篇文章主要介紹了Spring-AOP 靜態(tài)正則表達(dá)式方法如何匹配切面的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
SpringBoot?@RestControllerAdvice注解對(duì)返回值統(tǒng)一封裝的處理方法
這篇文章主要介紹了SpringBoot?@RestControllerAdvice注解對(duì)返回值統(tǒng)一封裝,使用@RestControllerAdvice對(duì)響應(yīng)進(jìn)行增強(qiáng),本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
Java的泛型擦除和運(yùn)行時(shí)泛型信息獲取方式
Java泛型在編譯時(shí)會(huì)發(fā)生類(lèi)型擦除,即泛型參數(shù)被替換為它們的限定類(lèi)型(如Object),這使得ArrayList<Integer>和ArrayList<String>在運(yùn)行時(shí)類(lèi)型相同,盡管如此,我們可以通過(guò)定義類(lèi)或匿名內(nèi)部類(lèi)的方式在運(yùn)行時(shí)獲取泛型信息2024-09-09
mybatis中sql語(yǔ)句CDATA標(biāo)簽的用法說(shuō)明
這篇文章主要介紹了mybatis中sql語(yǔ)句CDATA標(biāo)簽的用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
仿京東平臺(tái)框架開(kāi)發(fā)開(kāi)放平臺(tái)(包含需求,服務(wù)端代碼,SDK代碼)
現(xiàn)在開(kāi)放平臺(tái)越來(lái)越多了,下面針對(duì)仿京東開(kāi)放平臺(tái)框架,封裝自己的開(kāi)放平臺(tái),分享給大家。先感謝一下京東開(kāi)放平臺(tái)的技術(shù)大佬們,下面從開(kāi)放平臺(tái)需求,服務(wù)端代碼,SDK代碼三大塊進(jìn)行分享2021-06-06

