這篇文章,主要介紹ElasticSearch數(shù)據(jù)庫之查詢操作(match、must、must_not、should、_source、filter、range、exists、ids、term、terms)。
目錄
一、布爾查詢
1.1、主鍵查詢
1.2、兩種查詢方式
(1)路徑參數(shù)查詢
(2)請求體參數(shù)查詢
1.3、match查詢
(1)match
(2)match_all
1.4、過濾字段
1.5、布爾查詢(must)
1.6、布爾查詢(should)
1.7、布爾查詢(must_not)
二、filter過濾查詢
2.1、range范圍查詢
2.2、exists是否存在
2.3、ids過濾查詢
2.4、term關(guān)鍵詞查詢
2.5、terms多關(guān)鍵詞查詢
一、布爾查詢
1.1、主鍵查詢
# 主鍵查詢
GET /索引名稱/_doc/doc文檔的id
# 測試案例
GET /idx_20221124/_doc/2022001
這種查詢方式,每一次只能夠查詢一條doc文檔,如果要查詢很多doc文檔,那么就需要通過【_search】命令。
1.2、兩種查詢方式
(1)路徑參數(shù)查詢
將查詢條件放在請求路徑后面,查詢條件使用【q=字段名稱:字段值】這種格式,如下所示:
# 單個(gè)查詢條件
# 查詢age字段等于20的doc文檔
GET /idx_20221124/_search?q=age:20
(2)請求體參數(shù)查詢
當(dāng)查詢條件有很多個(gè)的時(shí)候,如果將所有的查詢條件都放在請求路徑上面,顯然不合適,所以ES可以將查詢條件放到請求體里面,請求體里面的查詢條件需要按照指定的格式,不然ES會(huì)解析報(bào)錯(cuò)。
#
# 查詢
GET /idx_20221124/_search
{
"query": {
"指定查詢類型": {
"查詢字段": "查詢值"
}
}
}
# 查詢
GET /idx_20221124/_search
{
"query": {
"match": {
"age": 20
}
}
}
1.3、match查詢
match關(guān)鍵字,相當(dāng)于mysql數(shù)據(jù)庫中的like查詢,match查詢的字段如果是text類型,那么text會(huì)被分詞,match就會(huì)匹配分詞,查詢所有包含分詞的doc文檔,如果不是text類型的,那就是精確查詢。
match有多種形式,如下所示:
- match:查詢指定條件的數(shù)據(jù),match會(huì)將查詢的條件進(jìn)行分詞操作,然后只有doc文檔中包含分詞,就都會(huì)查詢出來。
- match_all:查詢所有數(shù)據(jù)。
- match_phrase:匹配短語,match是會(huì)查詢所有包含分詞的doc文檔,而match_phrase則是匹配整個(gè)短語,才會(huì)返回對應(yīng)的doc文檔。
- match_phrase_prefix:匹配短語的前綴部分,這個(gè)只能使用在text類型字段。
(1)match
# 查詢所有數(shù)據(jù)
# 查詢age等于20的doc文檔
GET /idx_20221124/_search
{
"query": {
"match": {
"age": 20
}
}
}
(2)match_all
match_all表示查詢所有的數(shù)據(jù),默認(rèn)是10條,因?yàn)镋S默認(rèn)分頁是10條數(shù)據(jù),這個(gè)關(guān)鍵字不能寫查詢條件。
# 查詢所有數(shù)據(jù)
GET /idx_20221124/_search
{
"query": {
"match_all": {}
}
}
執(zhí)行結(jié)果:
1.4、過濾字段
ES查詢時(shí)候,默認(rèn)情況下,會(huì)將doc文檔中的所有字段都返回,如果查詢時(shí)候不想查詢某個(gè)字段,那么可以使用【_source】屬性設(shè)置查詢哪些字段。【_source】就相當(dāng)于mysql數(shù)據(jù)庫中的【select 字段1,字段2】形式。
【_source】過濾字段:
- 第一種:_source=false,表示所有字段都不返回。
- 第二種:_source: ["username", "age"],只返回指定的字段。
案例代碼:
# 查詢數(shù)據(jù),不返回字段
GET /idx_20221124/_search
{
"query": {
"match": {
"age": 20
}
},
"_source": false
}
# 查詢數(shù)據(jù),返回指定字段
GET /idx_20221124/_search
{
"query": {
"match": {
"age": 20
}
},
"_source": ["username", "age"]
}
執(zhí)行結(jié)果:
1.5、布爾查詢(must)
條件查詢,和mysql數(shù)據(jù)庫中的條件查詢是類似的,只不過ES中使用JSON的方式來組織查詢條件,條件查詢基本格式如下所示:
# 條件查詢數(shù)據(jù)
GET /索引名稱/_search
{
"query": {
"bool": {
"條件類型": [
{條件1},
{條件2}
]
}
}
}
條件查詢中的【must】和mysql數(shù)據(jù)庫中的【and】是相同作用,【must】接收一個(gè)數(shù)組,數(shù)組中的所有條件都成立,這個(gè)時(shí)候才會(huì)查詢對應(yīng)數(shù)據(jù)。
# 查詢數(shù)據(jù)
GET /idx_20221124/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"nickname": "java"
}
},
{
"match": {
"age": 25
}
}
]
}
}
}
執(zhí)行結(jié)果:
1.6、布爾查詢(should)
既然有and運(yùn)算,那當(dāng)然少不了or運(yùn)算,ES中使用【should】表示或運(yùn)算,相當(dāng)于mysql數(shù)據(jù)庫中的【or】連接符。
# 查詢數(shù)據(jù)
GET /idx_20221124/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"nickname": "java"
}
},
{
"match": {
"age": 31
}
}
]
}
}
}
執(zhí)行結(jié)果:
1.7、布爾查詢(must_not)
must_not是非運(yùn)算,相當(dāng)于mysql數(shù)據(jù)庫中的【!】非運(yùn)算,即:【not】。
# 查詢數(shù)據(jù)
# 查詢年齡不是25的所有doc文檔
GET /idx_20221124/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"age": 25
}
}
]
}
}
}
執(zhí)行結(jié)果:
二、filter過濾查詢
2.1、range范圍查詢
filter是用于過濾查詢的關(guān)鍵字,在filter里面可以使用多種查詢條件,例如:range、term、terms、exists、ids幾種常見的查詢,這里先介紹range范圍查詢,范圍查詢首先需要指定范圍,下面是幾個(gè)常見的范圍關(guān)鍵字。
range范圍關(guān)鍵字:
- gt:大于。
- lt:小于。
- gte:大于等于。
- lte:小于等于。
- eq:等于。
- ne:不等于。
range范圍查詢的語句格式:
# 查詢數(shù)據(jù)
GET /索引名稱/_search
{
"query": {
"bool": {
"filter": [
{
"range": {
"指定字段": {
"條件": 范圍值,
"條件": 范圍值
}
}
}
]
}
}
}
具體案例代碼:
# 查詢數(shù)據(jù)
GET /idx_20221124/_search
{
"query": {
"bool": {
"filter": [
{
"range": {
"age": {
"gt": 20,
"lt": 30
}
}
}
]
}
}
}
執(zhí)行結(jié)果:
2.2、exists是否存在
exists關(guān)鍵字,是表示指定的字段的值是否存在,類似于mysql數(shù)據(jù)庫中的【is null】,但是ES中exists用在filter里面時(shí)候,表示過濾掉不存在指定字段的doc文檔。
# 查詢數(shù)據(jù)
# 過濾nickname不存在的doc文檔數(shù)據(jù)
GET /idx_20221124/_search
{
"query": {
"bool": {
"filter": [
{
"exists": {
"field": "nickname"
}
}
]
}
}
}
運(yùn)行上面語句之后,會(huì)返回所有doc文檔中,nickname字段不存在的doc文檔結(jié)果。
注意:exists會(huì)返回指定字段存在的doc文檔,只有當(dāng)字段等于null,即:不存在時(shí)候才會(huì)匹配成功,如果字段等于空字符串不會(huì)匹配成功。
2.3、ids過濾查詢
ids查詢,這就相當(dāng)于mysql數(shù)據(jù)庫中的【in】條件查詢,多個(gè)條件值查詢,但是這里的只能夠?qū)?span style="color:#fe2c24;">doc文檔的id進(jìn)行多個(gè)值查詢。
# 查詢數(shù)據(jù)
# 查詢doc文檔id等于:2022001、2022005的數(shù)據(jù)
GET /idx_20221124/_search
{
"query": {
"bool": {
"filter": [
{
"ids": {
"values": [
"2022001", "2022005"
]
}
}
]
}
}
}
執(zhí)行結(jié)果:
2.4、term關(guān)鍵詞查詢
term表示關(guān)鍵字查詢,即:判斷doc文檔里面是否包含給定的term關(guān)鍵字,如果包含,則滿足條件,否則不滿足。
# 查詢數(shù)據(jù)
GET /索引名稱/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"字段名稱": "字段值"
}
}
]
}
}
}
# 測試案例
GET /idx_20221124/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"username": "java"
}
}
]
}
}
}
執(zhí)行結(jié)果:
2.5、terms多關(guān)鍵詞查詢
term每次只能夠匹配一個(gè)關(guān)鍵字,而terms則允許多個(gè)關(guān)鍵字匹配。
# 查詢數(shù)據(jù)
GET /idx_20221124/_search
{
"query": {
"bool": {
"filter": [
{
"terms": {
"字段名稱": [
"字段值1",
"字段值2",
"字段值n"
]
}
}
]
}
}
}
# 測試案例
GET /idx_20221124/_search
{
"query": {
"bool": {
"filter": [
{
"terms": {
"username": [
"java",
"python"
]
}
}
]
}
}
}
執(zhí)行結(jié)果:
到此,ES中的一些基本查詢操作就介紹完啦。文章來源:http://www.zghlxwxcb.cn/news/detail-407634.html
綜上,這篇文章結(jié)束了,主要介紹ElasticSearch數(shù)據(jù)庫之查詢操作(match、must、must_not、should、_source、filter、range、exists、ids、term、terms)。文章來源地址http://www.zghlxwxcb.cn/news/detail-407634.html
到了這里,關(guān)于【ES筆記02】ElasticSearch數(shù)據(jù)庫之查詢操作(match、must、must_not、should、_source、filter、range、exists、ids、term、terms)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!