text字段和keyword字段的區(qū)別
keyword
首先了解一下什么是keyword壶谒,舉個(gè)例子:
建立一個(gè)索引test_index艺栈,將字段name設(shè)置為keyword類型先紫。
PUT test_index
{
"mappings": {
"user": {
"properties": {
"name": {
"type": "keyword"
}
}
}
}
}
然后查詢該mapping是否已經(jīng)構(gòu)建成功。
GET test_index/_mapping
得到如下結(jié)果:
{
"test_index": {
"mappings": {
"user": {
"properties": {
"name": {
"type": "keyword"
}
}
}
}
}
}
表示已經(jīng)創(chuàng)建成功先煎。
在該索引test_index下建立數(shù)據(jù):
POST test_index/user
{
"name":"奧尼爾"
}
使用term查詢數(shù)據(jù)践叠,name為“奧尼爾”:
GET test_index/user/_search
{
"query": {
"term": {
"name": {
"value": "奧尼爾"
}
}
}
}
得到結(jié)果:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "test_index",
"_type": "user",
"_id": "71pbn2kBcbRJikqN_vnl",
"_score": 0.2876821,
"_source": {
"name": "奧尼爾"
}
}
]
}
}
接下來使用term查詢數(shù)據(jù),name為“奧尼”:
GET test_index/user/_search
{
"query": {
"term": {
"name": {
"value": "奧尼"
}
}
}
}
得到結(jié)果:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
因?yàn)樽侄伪辉O(shè)置成keyword類型膊存,錄入數(shù)據(jù)該字段是不會被分詞导而,所以使用term查詢時(shí)候忱叭,需要全匹配才能查詢到。
text
更新test_index的mapping:
PUT test_index/_mapping/user?update_all_types
{
"properties": {
"school": {
"type": "text"
}
}
}
查看mapping:
{
"test_index": {
"mappings": {
"user": {
"properties": {
"name": {
"type": "keyword"
},
"school": {
"type": "text"
}
}
}
}
}
}
添加數(shù)據(jù):
POST test_index/user
{
"name":"奧尼爾",
"school":"hello world"
}
然后我們查詢school字段:
GET test_index/user/_search
{
"query": {
"term": {
"school": {
"value": "world"
}
}
}
}
結(jié)果:
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "test_index",
"_type": "user",
"_id": "8lptn2kBcbRJikqNqvk0",
"_score": 0.2876821,
"_source": {
"name": "奧尼爾",
"school": "hello world"
}
}
]
}
}
可以發(fā)現(xiàn)今艺,即使沒有輸入完整的查詢條件窑多,也能將school為"hello world"的內(nèi)容查詢到。
如果我們完整查詢:
GET test_index/user/_search
{
"query": {
"term": {
"school": {
"value": "hello world"
}
}
}
}
結(jié)果:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
完整輸入結(jié)果查找不到洼滚。
因?yàn)閠ext埂息,會將字段進(jìn)行分詞,如“hello world”會被分詞為["hello","world",...]遥巴,而term必須匹配到數(shù)組中的一項(xiàng)千康,才能查出結(jié)果。
match铲掐,match_phrase和term區(qū)別
elastic會對查詢語句進(jìn)行分詞
term
term 查詢語句不分詞
term查詢keyword字段
keyword字段不分詞
term查詢keyword字段拾弃,需要完全匹配
term查詢text字段
text字段分詞
term查詢text字段,必須為text字段分詞后中的某一個(gè)才行摆霉。如“我真帥”分詞為["我","真","帥"]豪椿,term必須為“我”或“真”或“帥”,才能查到携栋,而“我?guī)洝贝疃堋ⅰ罢鎺洝辈恍小?/p>
match
match 查詢語句分詞
match查詢keyword字段
keyword字段不分詞
match查詢keyword字段,需要完全匹配
match查詢text字段
text字段分詞
match查詢text字段婉支,只需要match分詞結(jié)果中和text分詞有匹配就可以查出鸯隅。如“我真帥”分詞為["我","真","帥"],match的查詢語句“真帥”被分詞為["真","帥"]向挖,其中“真”蝌以、“帥”能匹配上text字段的分詞結(jié)果,所以能查出何之。
match_phrase
match_phrase 查詢語句分詞
match_phrase 查詢keyword字段
keyword字段不分詞
match_phrase 查詢keyword字段跟畅,需要完全匹配
match_phrase 查詢text字段
text字段分詞
match_phrase 查詢text字段,只需要match_phrase 分詞結(jié)果中和text分詞有匹配且查詢語句必須包含在text分詞結(jié)果中溶推,同時(shí)順序相同且連續(xù)徊件,才可以查出。如“我真帥”分詞為["我","真","帥",“真帥”]悼潭,match_phrase 的查詢語句“真帥”被分詞為["真帥"]庇忌,其中“真帥”能匹配上text字段的分詞結(jié)果舞箍,連續(xù)且順序相同舰褪,所以能查出。