标签:account Term 查询 score bank 精确 address match
【ElasticSearch(九)进阶】Term精确数值查询,match keyword精确文本查询
一、Term精确数值查询
-
term查询,会返回那些 在提供的字段中包含确切信息 的文档内容。
-
查询text字段值,使用match。查询精确数值,使用term。
-
为什么避免使用term对text字段进行查询?
默认情况下,ES更改text字段的值作为词法分析的一部分。这会使查找text字段值的精确匹配变得困难。
查询年龄是33岁的数据:
GET bank/_search
{
"query":{
"term":{
"age": 33
}
}
}
返回结果:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 50,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "bank",
"_type" : "account",
"_id" : "18",
"_score" : 1.0,
"_source" : {
"account_number" : 18,
"balance" : 4180,
"firstname" : "Dale",
"lastname" : "Adams",
"age" : 33,
"gender" : "M",
"address" : "467 Hutchinson Court",
"employer" : "Boink",
"email" : "daleadams@boink.com",
"city" : "Orick",
"state" : "MD"
}
},
。。。
]
}
}
二、match keyword精确文本查询
下面对比下match
,match_phrase
,match中的属性加.keyword
的区别
1.match 模糊查询文本
会将address
的文本拆分成词,只要结果中包含有任意词的文档,都可以被筛选出来。
GET bank/_search
{
"query":{
"match":{
"address": "467 Hutchinson Court"
}
}
}
返回结果:
{
"took" : 17,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 120,
"relation" : "eq"
},
"max_score" : 14.617203,
"hits" : [
{
"_index" : "bank",
"_type" : "account",
"_id" : "18",
"_score" : 14.617203,
"_source" : {
"account_number" : 18,
"balance" : 4180,
"firstname" : "Dale",
"lastname" : "Adams",
"age" : 33,
"gender" : "M",
"address" : "467 Hutchinson Court",
"employer" : "Boink",
"email" : "daleadams@boink.com",
"city" : "Orick",
"state" : "MD"
}
},
{
"_index" : "bank",
"_type" : "account",
"_id" : "53",
"_score" : 5.990829,
"_source" : {
"account_number" : 53,
"balance" : 28101,
"firstname" : "Kathryn",
"lastname" : "Payne",
"age" : 29,
"gender" : "F",
"address" : "467 Louis Place",
"employer" : "Katakana",
"email" : "kathrynpayne@katakana.com",
"city" : "Harviell",
"state" : "SD"
}
},
{
"_index" : "bank",
"_type" : "account",
"_id" : "56",
"_score" : 2.1248586,
"_source" : {
"account_number" : 56,
"balance" : 14992,
"firstname" : "Josie",
"lastname" : "Nelson",
"age" : 32,
"gender" : "M",
"address" : "857 Tabor Court",
"employer" : "Emtrac",
"email" : "josienelson@emtrac.com",
"city" : "Sunnyside",
"state" : "UT"
}
},
。。。
]
}
}
2.match_phrase 短语查询
将address
的文本视为一个短语,不进行文本拆分,只要结果中包含这个短语的文档,都能被筛选出来。
GET bank/_search
{
"query":{
"match_phrase":{
"address": "467 Hutchinson"
}
}
}
返回结果:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 12.492344,
"hits" : [
{
"_index" : "bank",
"_type" : "account",
"_id" : "18",
"_score" : 12.492344,
"_source" : {
"account_number" : 18,
"balance" : 4180,
"firstname" : "Dale",
"lastname" : "Adams",
"age" : 33,
"gender" : "M",
"address" : "467 Hutchinson Court",
"employer" : "Boink",
"email" : "daleadams@boink.com",
"city" : "Orick",
"state" : "MD"
}
}
]
}
}
3.match中的属性加.keyword
keyword精确查询,只有结果中address
属性和address.keyword
的值完全一致的文档,才能被筛选出来。
结合2和3,可以发现同样的值,match_phrase 和 keyword的区别。
GET bank/_search
{
"query":{
"match":{
"address.keyword": "467 Hutchinson"
}
}
}
返回结果:
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
*我们一般规定:全文检索字段用 match,其他非text字段匹配用term
标签:account,Term,查询,score,bank,精确,address,match 来源: https://www.cnblogs.com/musecho/p/15179997.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。