term
和 match
總結(jié)
在實(shí)際的項(xiàng)目查詢中,term
和match
是最常用的兩個查詢,而經(jīng)常搞不清兩者有什么區(qū)別,趁機(jī)總結(jié)有空總結(jié)下。
term
用法
先看看term的定義像棘,term是代表完全匹配,也就是精確查詢壶冒,搜索前不會再對搜索詞進(jìn)行分詞拆解缕题。
這里通過例子來說明,先存放一些數(shù)據(jù):
{
"title": "love China",
"content": "people very love China",
"tags": ["China", "love"]
}
{
"title": "love HuBei",
"content": "people very love HuBei",
"tags": ["HuBei", "love"]
}
來使用term
查詢下:
{
"query": {
"term": {
"title": "love"
}
}
}
結(jié)果是依痊,上面的兩條數(shù)據(jù)都能查詢到:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.6931472,
"hits": [
{
"_index": "test",
"_type": "doc",
"_id": "8",
"_score": 0.6931472,
"_source": {
"title": "love HuBei",
"content": "people very love HuBei",
"tags": ["HuBei","love"]
}
},
{
"_index": "test",
"_type": "doc",
"_id": "7",
"_score": 0.6931472,
"_source": {
"title": "love China",
"content": "people very love China",
"tags": ["China","love"]
}
}
]
}
}
發(fā)現(xiàn)避除,title里有關(guān)love的關(guān)鍵字都查出來了,但是我只想精確匹配 love China
這個胸嘁,按照下面的寫法看看能不能查出來:
{
"query": {
"term": {
"title": "love China"
}
}
}
執(zhí)行發(fā)現(xiàn)無數(shù)據(jù)瓶摆,從概念上看,term屬于精確匹配性宏,只能查單個詞群井。我想用term匹配多個詞怎么做?可以使用terms
來:
{
"query": {
"terms": {
"title": ["love", "China"]
}
}
}
查詢結(jié)果為:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.6931472,
"hits": [
{
"_index": "test",
"_type": "doc",
"_id": "8",
"_score": 0.6931472,
"_source": {
"title": "love HuBei",
"content": "people very love HuBei",
"tags": ["HuBei","love"]
}
},
{
"_index": "test",
"_type": "doc",
"_id": "7",
"_score": 0.6931472,
"_source": {
"title": "love China",
"content": "people very love China",
"tags": ["China","love"]
}
}
]
}
}
發(fā)現(xiàn)全部查詢出來毫胜,為什么书斜?因?yàn)閠erms里的[ ]
多個是或者的關(guān)系,只要滿足其中一個詞就可以酵使。想要通知滿足兩個詞的話荐吉,就得使用bool的must來做,如下:
{
"query": {
"bool": {
"must": [
{
"term": {
"title": "love"
}
},
{
"term": {
"title": "china"
}
}
]
}
}
}
可以看到口渔,我們上面使用china
是小寫的样屠。當(dāng)使用的是大寫的China
我們進(jìn)行搜索的時(shí)候,發(fā)現(xiàn)搜不到任何信息缺脉。這是為什么了痪欲?title這個詞在進(jìn)行存儲的時(shí)候,進(jìn)行了分詞處理攻礼。我們這里使用的是默認(rèn)的分詞處理器進(jìn)行了分詞處理业踢。我們可以看看如何進(jìn)行分詞處理的?
分詞處理器
GET test/_analyze
{
"text" : "love China"
}
結(jié)果為:
{
"tokens": [
{
"token": "love",
"start_offset": 0,
"end_offset": 4,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "china",
"start_offset": 5,
"end_offset": 10,
"type": "<ALPHANUM>",
"position": 1
}
]
}
分析出來的為love
和china
的兩個詞礁扮。而term
只能完完整整的匹配上面的詞知举,不做任何改變的匹配瞬沦。所以,我們使用China
這樣的方式進(jìn)行的查詢的時(shí)候雇锡,就會失敗蛙埂。稍后會有一節(jié)專門講解分詞器。
match
用法
先用 love China
來匹配遮糖。
GET test/doc/_search
{
"query": {
"match": {
"title": "love China"
}
}
}
結(jié)果是:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1.3862944,
"hits": [
{
"_index": "test",
"_type": "doc",
"_id": "7",
"_score": 1.3862944,
"_source": {
"title": "love China",
"content": "people very love China",
"tags": [
"China",
"love"
]
}
},
{
"_index": "test",
"_type": "doc",
"_id": "8",
"_score": 0.6931472,
"_source": {
"title": "love HuBei",
"content": "people very love HuBei",
"tags": [
"HuBei",
"love"
]
}
}
]
}
}
發(fā)現(xiàn)兩個都查出來了,為什么叠赐?因?yàn)閙atch進(jìn)行搜索的時(shí)候欲账,會先進(jìn)行分詞拆分,拆完后芭概,再來匹配赛不,上面兩個內(nèi)容,他們title的詞條為: love china hubei
罢洲,我們搜索的為love China
我們進(jìn)行分詞處理得到為love china
踢故,并且屬于或的關(guān)系,只要任何一個詞條在里面就能匹配到惹苗。如果想 love
和 China
同時(shí)匹配到的話殿较,怎么做?使用 match_phrase
match_phrase
用法
match_phrase
稱為短語搜索桩蓉,要求所有的分詞必須同時(shí)出現(xiàn)在文檔中淋纲,同時(shí)位置必須緊鄰一致。
GET test/doc/_search
{
"query": {
"match_phrase": {
"title": "love china"
}
}
}
結(jié)果為:
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1.3862944,
"hits": [
{
"_index": "test",
"_type": "doc",
"_id": "7",
"_score": 1.3862944,
"_source": {
"title": "love China",
"content": "people very love China",
"tags": [
"China",
"love"
]
}
}
]
}
}
這次好像符合我們的需求了院究,結(jié)果只出現(xiàn)了一條記錄洽瞬。