1. 準(zhǔn)備數(shù)據(jù)
PUT h1/doc/1
{
"name": "rose",
"gender": "female",
"age": 18,
"tags": ["白", "漂亮", "高"]
}
PUT h1/doc/2
{
"name": "lila",
"gender": "female",
"age": 18,
"tags": ["黑", "漂亮", "高"]
}
PUT h1/doc/3
{
"name": "john",
"gender": "male",
"age": 18,
"tags": ["黑", "帥", "高"]
}
運(yùn)行結(jié)果:
{
"_index" : "h1",
"_type" : "doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
2. match 查詢
2.1 match 按條件查詢
# 查詢性別是男性的結(jié)果
GET h1/doc/_search
{
"query": {
"match": {
"gender": "male"
}
}
}
查詢結(jié)果:
{
"took" : 59,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "h1", # 索引
"_type" : "doc", # 文檔類型
"_id" : "3", # 文檔唯一 id
"_score" : 0.2876821, # 打分機(jī)制打出來的分?jǐn)?shù)
"_source" : { # 查詢結(jié)果
"name" : "john",
"gender" : "male",
"age" : 18,
"tags" : [
"黑",
"帥",
"高"
]
}
}
]
}
}
2.2 match_all 查詢?nèi)?/h3>
# 查詢 h1 中所有文檔
GET h1/doc/_search
{
"query": {
"match_all": {}
}
}
# 查詢 h1 中所有文檔
GET h1/doc/_search
{
"query": {
"match_all": {}
}
}
match_all
的值為空,表示沒有查詢條件,那就是查詢?nèi)俊>拖?code>select * from table_name 一樣。
查詢結(jié)果:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 1.0,
"hits" : [
{
"_index" : "h1",
"_type" : "doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "lila",
"gender" : "female",
"age" : 18,
"tags" : [
"黑",
"漂亮",
"高"
]
}
},
{
"_index" : "h1",
"_type" : "doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "rose",
"gender" : "female",
"age" : 18,
"tags" : [
"白",
"漂亮",
"高"
]
}
},
{
"_index" : "h1",
"_type" : "doc",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "john",
"gender" : "male",
"age" : 18,
"tags" : [
"黑",
"帥",
"高"
]
}
}
]
}
}
2.3 match_phrase 短語查詢
match
查詢時(shí)散列映射,包含了我們希望搜索的字段和字符串,即只要文檔中有我們希望的那個(gè)關(guān)鍵字,但也會(huì)帶來一些問題。
es
會(huì)將文檔中的內(nèi)容進(jìn)行拆分,對于英文來說可能沒有太大的影響,但是中文短語就不太適用,一旦拆分就會(huì)失去原有的含義,比如以下:
1、準(zhǔn)備數(shù)據(jù):
PUT t1/doc/1
{
"title": "中國是世界上人口最多的國家"
}
PUT t1/doc/2
{
"title": "美國是世界上軍事實(shí)力最強(qiáng)大的國家"
}
PUT t1/doc/3
{
"title": "北京是中國的首都"
}
2、先使用 match
查詢含有中國的文檔:
GET t1/doc/_search
{
"query": {
"match": {
"title": "中國"
}
}
}
查詢結(jié)果:
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 3,
"max_score" : 0.68324494,
"hits" : [
{
"_index" : "t1",
"_type" : "doc",
"_id" : "1",
"_score" : 0.68324494,
"_source" : {
"title" : "中國是世界上人口最多的國家"
}
},
{
"_index" : "t1",
"_type" : "doc",
"_id" : "3",
"_score" : 0.5753642,
"_source" : {
"title" : "北京是中國的首都"
}
},
{
"_index" : "t1",
"_type" : "doc",
"_id" : "2",
"_score" : 0.39556286,
"_source" : {
"title" : "美國是世界上軍事實(shí)力最強(qiáng)大的國家"
}
}
]
}
}
發(fā)現(xiàn)三篇文檔都被返回,與我們的預(yù)期有偏差;這是因?yàn)?title
中的內(nèi)容被拆分成一個(gè)個(gè)單獨(dú)的字,而 id=2
的文檔包含了 國 字也符合,所以也被返回了。es
自帶的中文分詞處理不太好用,后面可以使用 ik
中文分詞器來處理。
3、match_phrase
查詢短語
不過可以使用 match_phrase
來匹配短語,將上面的 match
換成 match_phrase
試試:
# 短語查詢
GET t1/doc/_search
{
"query": {
"match_phrase": {
"title": "中國"
}
}
}
查詢結(jié)果:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "t1",
"_type" : "doc",
"_id" : "1",
"_score" : 0.5753642,
"_source" : {
"title" : "中國是世界上人口最多的國家"
}
},
{
"_index" : "t1",
"_type" : "doc",
"_id" : "3",
"_score" : 0.5753642,
"_source" : {
"title" : "北京是中國的首都"
}
}
]
}
}
4、slop
間隔查詢
當(dāng)我們要查詢的短語,中間有別的詞時(shí),可以使用 slop
來跳過。比如上述要查詢 中國世界,這個(gè)短語中間被 是 隔開了,這時(shí)可以使用 slop
來跳過,相當(dāng)于正則中的中國.*?世界
:
# 短語查詢,查詢中國世界,加 slop
GET t1/doc/_search
{
"query": {
"match_phrase": {
"title": {
"query": "中國世界",
"slop": 1
}
}
}
}
查詢結(jié)果:
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.7445889,
"hits" : [
{
"_index" : "t1",
"_type" : "doc",
"_id" : "1",
"_score" : 0.7445889,
"_source" : {
"title" : "中國是世界上人口最多的國家"
}
}
]
}
}
2.4 match_phrase_prefix 最左前綴查詢
場景:當(dāng)我們要查詢的詞只能想起前幾個(gè)字符時(shí)
# 最左前綴查詢,查詢名字為 rose 的文檔
GET h1/doc/_search
{
"query": {
"match_phrase_prefix": {
"name": "ro"
}
}
}
查詢結(jié)果:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "h1",
"_type" : "doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"name" : "rose",
"gender" : "female",
"age" : 18,
"tags" : [
"白",
"漂亮",
"高"
]
}
}
]
}
}
限制結(jié)果集
最左前綴查詢很費(fèi)性能,返回的是一個(gè)很大的集合,一般很少使用,使用的時(shí)候最好對結(jié)果集進(jìn)行限制,max_expansions
參數(shù)可以設(shè)置最大的前綴擴(kuò)展數(shù)量:
# 最左前綴查詢
GET h1/doc/_search
{
"query": {
"match_phrase_prefix": {
"gender": {
"query": "fe",
"max_expansions": 1
}
}
}
}
查詢結(jié)果:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "h1",
"_type" : "doc",
"_id" : "2",
"_score" : 0.2876821,
"_source" : {
"name" : "lila",
"gender" : "female",
"age" : 18,
"tags" : [
"黑",
"漂亮",
"高"
]
}
},
{
"_index" : "h1",
"_type" : "doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"name" : "rose",
"gender" : "female",
"age" : 18,
"tags" : [
"白",
"漂亮",
"高"
]
}
}
]
}
}
2.5 multi_match 多字段查詢
1、準(zhǔn)備數(shù)據(jù):
# 多字段查詢
PUT t3/doc/1
{
"title": "maggie is beautiful girl",
"desc": "beautiful girl you are beautiful so"
}
PUT t3/doc/2
{
"title": "beautiful beach",
"desc": "I like basking on the beach,and you? beautiful girl"
}
2、查詢包含 beautiful
字段的文檔:
GET t3/doc/_search
{
"query": {
"multi_match": {
"query": "beautiful", # 要查詢的詞
"fields": ["desc", "title"] # 要查詢的字段
}
}
}
還可以當(dāng)做 match_phrase
和match_phrase_prefix
使用,只需要指定type
類型即可:
GET t3/doc/_search
{
"query": {
"multi_match": {
"query": "gi",
"fields": ["title"],
"type": "phrase_prefix"
}
}
}
GET t3/doc/_search
{
"query": {
"multi_match": {
"query": "girl",
"fields": ["title"],
"type": "phrase"
}
}
}
3. term 查詢
3.1 初始 es 的分析器
term
查詢用于精確查詢,但是不適用于 text
類型的字段查詢。
在此之前我們先了解 es
的分析機(jī)制,默認(rèn)的標(biāo)準(zhǔn)分析器會(huì)對文檔進(jìn)行:
- 刪除大多數(shù)的標(biāo)點(diǎn)符號
- 將文檔拆分為單個(gè)詞條,稱為
token
- 將
token
轉(zhuǎn)換為小寫
最后保存到倒排序索引上,而倒排序索引用來查詢,如 Beautiful girl
經(jīng)過分析后是這樣的:
POST _analyze
{
"analyzer": "standard",
"text": "Beautiful girl"
}
# 結(jié)果,轉(zhuǎn)換為小寫了
{
"tokens" : [
{
"token" : "beautiful",
"start_offset" : 0,
"end_offset" : 9,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "girl",
"start_offset" : 10,
"end_offset" : 14,
"type" : "<ALPHANUM>",
"position" : 1
}
]
}
3.2 term 查詢
1、準(zhǔn)備數(shù)據(jù):
# 創(chuàng)建索引,自定義 mapping,后面會(huì)講到
PUT t4
{
"mappings": {
"doc":{
"properties":{
"t1":{
"type": "text" # 定義字段類型為 text
}
}
}
}
}
PUT t4/doc/1
{
"t1": "Beautiful girl!"
}
PUT t4/doc/2
{
"t1": "sexy girl!"
}
2、match
查詢:
GET t4/doc/_search
{
"query": {
"match": {
"t1": "Beautiful girl!"
}
}
}
經(jīng)過分析后,會(huì)得到 beautiful、girl
兩個(gè) token
,然后再去 t4
索引上去查詢,會(huì)返回兩篇文檔:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 0.5753642,
"hits" : [
{
"_index" : "t4",
"_type" : "doc",
"_id" : "1",
"_score" : 0.5753642,
"_source" : {
"title" : "Beautiful girl"
}
},
{
"_index" : "t4",
"_type" : "doc",
"_id" : "2",
"_score" : 0.2876821,
"_source" : {
"title" : "sex girl"
}
}
]
}
}
3、但是我們只想精確查詢包含 Beautiful girl
的文檔,這時(shí)就需要使用 term
來精確查詢:
GET t4/doc/_search
{
"query": {
"term": {
"title": "beautiful"
}
}
}
查詢結(jié)果:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.2876821,
"hits" : [
{
"_index" : "t4",
"_type" : "doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"title" : "Beautiful girl"
}
}
]
}
}
注意:
term
查詢不適用于類型是text
的字段,可以使用match
查詢;另外Beautiful
經(jīng)過分析后變?yōu)?beautiful
,查詢時(shí)使用Beautiful
是查詢不到的~文章來源:http://www.zghlxwxcb.cn/news/detail-418513.html
3.3 查詢多個(gè)
精確查詢多個(gè)字段:文章來源地址http://www.zghlxwxcb.cn/news/detail-418513.html
GET t4/doc/_search
{
"query": {
"terms": {
"title": ["beautiful", "sex"]
}
}
}
到了這里,關(guān)于elasticsearch term & match 查詢的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!