1.match_phrase:相比于mathch,match通常会进行分词处理,而match_phrase不会分词处理
GET /bank/_search
{
"query": {
"match_phrase": {
"address": "Holmes Lane"
}
}
}
2.multi_match:多字段匹配
GET /bank/_search
{
"query": {
"multi_match": {
"query": "mill Movico",
"fields": ["address","city"]
}
}
}
以下是查询的结果集:通过结果集评分我们不难看出multi_match依然是会对query的值来进行分词
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 6.505949,
"hits" : [
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "472",
"_score" : 6.505949,
"_source" : {
"account_number" : 472,
"balance" : 25571,
"firstname" : "Lee",
"lastname" : "Long",
"age" : 32,
"gender" : "F",
"address" : "288 Mill Street",
"employer" : "Comverges",
"email" : "leelong@comverges.com",
"city" : "Movico",
"state" : "MT"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "970",
"_score" : 5.4032025,
"_source" : {
"account_number" : 970,
"balance" : 19648,
"firstname" : "Forbes",
"lastname" : "Wallace",
"age" : 28,
"gender" : "M",
"address" : "990 Mill Road",
"employer" : "Pheast",
"email" : "forbeswallace@pheast.com",
"city" : "Lopezo",
"state" : "AK"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "136",
"_score" : 5.4032025,
"_source" : {
"account_number" : 136,
"balance" : 45801,
"firstname" : "Winnie",
"lastname" : "Holland",
"age" : 38,
"gender" : "M",
"address" : "198 Mill Lane",
"employer" : "Neteria",
"email" : "winnieholland@neteria.com",
"city" : "Urie",
"state" : "IL"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "345",
"_score" : 5.4032025,
"_source" : {
"account_number" : 345,
"balance" : 9812,
"firstname" : "Parker",
"lastname" : "Hines",
"age" : 38,
"gender" : "M",
"address" : "715 Mill Avenue",
"employer" : "Baluba",
"email" : "parkerhines@baluba.com",
"city" : "Blackgum",
"state" : "KY"
}
}
]
}
}
3.复合查询bool:顾名思义符合查询即为多条件查询:
GET /bank/_search
{
"query": {
"bool": {
"must": [
{"match": {
"address": "mill"
}},
{
"match": {
"gender": "M"
}
}
],
"must_not": [
{"term": {
"age": "58"
}}
],
"should": [
{"match": {
"firstname": "winnie"
}}
]
}
}
}
上述例子: must为必须满足的条件,must_not为必须不能满足的条件,should则为可满足也不可满足的条件,那么should有什么作用呢,参考一下查询结果集不难发现满足should可以提升相关性得分
{
"took" : 15,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 12.585751,
"hits" : [
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "136",
"_score" : 12.585751,
"_source" : {
"account_number" : 136,
"balance" : 45801,
"firstname" : "Winnie",
"lastname" : "Holland",
"age" : 38,
"gender" : "M",
"address" : "198 Mill Lane",
"employer" : "Neteria",
"email" : "winnieholland@neteria.com",
"city" : "Urie",
"state" : "IL"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "970",
"_score" : 6.0824604,
"_source" : {
"account_number" : 970,
"balance" : 19648,
"firstname" : "Forbes",
"lastname" : "Wallace",
"age" : 28,
"gender" : "M",
"address" : "990 Mill Road",
"employer" : "Pheast",
"email" : "forbeswallace@pheast.com",
"city" : "Lopezo",
"state" : "AK"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "345",
"_score" : 6.0824604,
"_source" : {
"account_number" : 345,
"balance" : 9812,
"firstname" : "Parker",
"lastname" : "Hines",
"age" : 38,
"gender" : "M",
"address" : "715 Mill Avenue",
"employer" : "Baluba",
"email" : "parkerhines@baluba.com",
"city" : "Blackgum",
"state" : "KY"
}
}
]
}
}
4.filter:过滤,用法和must/must_not/should一样,区别在于filter中的条件不会提升相关性得分
GET /bank/_search
{
"query": {
"bool": {
"filter": {
"range": {
"age": {
"gte": 10,
"lte": 20
}
}
}
}
}
}
可以看下结果集,即使满足条件得分也都是0
{
"took" : 8,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 44,
"relation" : "eq"
},
"max_score" : 0.0,
"hits" : [
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "157",
"_score" : 0.0,
"_source" : {
"account_number" : 157,
"balance" : 39868,
"firstname" : "Claudia",
"lastname" : "Terry",
"age" : 20,
"gender" : "F",
"address" : "132 Gunnison Court",
"employer" : "Lumbrex",
"email" : "claudiaterry@lumbrex.com",
"city" : "Castleton",
"state" : "MD"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "215",
"_score" : 0.0,
"_source" : {
"account_number" : 215,
"balance" : 37427,
"firstname" : "Copeland",
"lastname" : "Solomon",
"age" : 20,
"gender" : "M",
"address" : "741 McDonald Avenue",
"employer" : "Recognia",
"email" : "copelandsolomon@recognia.com",
"city" : "Edmund",
"state" : "ME"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "816",
"_score" : 0.0,
"_source" : {
"account_number" : 816,
"balance" : 9567,
"firstname" : "Cornelia",
"lastname" : "Lane",
"age" : 20,
"gender" : "F",
"address" : "384 Bainbridge Street",
"employer" : "Sulfax",
"email" : "cornelialane@sulfax.com",
"city" : "Elizaville",
"state" : "MS"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "905",
"_score" : 0.0,
"_source" : {
"account_number" : 905,
"balance" : 29438,
"firstname" : "Schultz",
"lastname" : "Moreno",
"age" : 20,
"gender" : "F",
"address" : "761 Cedar Street",
"employer" : "Paragonia",
"email" : "schultzmoreno@paragonia.com",
"city" : "Glenshaw",
"state" : "SC"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "95",
"_score" : 0.0,
"_source" : {
"account_number" : 95,
"balance" : 1650,
"firstname" : "Dominguez",
"lastname" : "Le",
"age" : 20,
"gender" : "M",
"address" : "539 Grace Court",
"employer" : "Portica",
"email" : "dominguezle@portica.com",
"city" : "Wollochet",
"state" : "KS"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "172",
"_score" : 0.0,
"_source" : {
"account_number" : 172,
"balance" : 18356,
"firstname" : "Marie",
"lastname" : "Whitehead",
"age" : 20,
"gender" : "M",
"address" : "704 Monaco Place",
"employer" : "Sultrax",
"email" : "mariewhitehead@sultrax.com",
"city" : "Dragoon",
"state" : "IL"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "228",
"_score" : 0.0,
"_source" : {
"account_number" : 228,
"balance" : 10543,
"firstname" : "Rosella",
"lastname" : "Albert",
"age" : 20,
"gender" : "M",
"address" : "185 Gotham Avenue",
"employer" : "Isoplex",
"email" : "rosellaalbert@isoplex.com",
"city" : "Finzel",
"state" : "NY"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "273",
"_score" : 0.0,
"_source" : {
"account_number" : 273,
"balance" : 11181,
"firstname" : "Murphy",
"lastname" : "Chandler",
"age" : 20,
"gender" : "F",
"address" : "569 Bradford Street",
"employer" : "Zilch",
"email" : "murphychandler@zilch.com",
"city" : "Vicksburg",
"state" : "FL"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "292",
"_score" : 0.0,
"_source" : {
"account_number" : 292,
"balance" : 26679,
"firstname" : "Morrow",
"lastname" : "Greene",
"age" : 20,
"gender" : "F",
"address" : "691 Nassau Street",
"employer" : "Columella",
"email" : "morrowgreene@columella.com",
"city" : "Sanborn",
"state" : "FL"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "367",
"_score" : 0.0,
"_source" : {
"account_number" : 367,
"balance" : 40458,
"firstname" : "Elaine",
"lastname" : "Workman",
"age" : 20,
"gender" : "M",
"address" : "188 Ridge Boulevard",
"employer" : "Colaire",
"email" : "elaineworkman@colaire.com",
"city" : "Herbster",
"state" : "AK"
}
}
]
}
}
5.term:term相比match都是匹配的意思,但是match涉及到分词,所以一般规范来说文本内容尽可能使用match,像数值等类型使用term;以下示例对比了短语匹配和精确匹配
##全文检索
GET /bank/_search
{
"query": {
"match": {
"address": "990 Mill Road"
}
}
}
##精确查找
GET /bank/_search
{
"query": {
"match": {
"address.keyword": "990 Mill Road"
}
}
}
##term
GET /bank/_search
{
"query": {
"term": {
"address": {
"value": "990 Mill Road"
}
}
}
}
##
GET /bank/_search
{
"query": {
"match_phrase": {
"address": "990 Mill Road"
}
}
}