match匹配查询
基本类型(非字符串),会进行精确匹配。
字符串,会进行全文检索,还会进行分词
基本类型(非字符串),match返回account_number=20的
GET bank/_search { "query": { "match": { "account_number": "20" } } }
字符串,会按照评分进行排序,并会对检索条件进行分词匹配,记录中包含Holmes 和Lane的都会返回
GET bank/_search { "query": { "match": { "address": "Holmes Lane" } } }
全文检索返回结果
{ "took" : 3, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 16, "relation" : "eq" }, "max_score" : 10.605789, "hits" : [ { "_index" : "bank", "_type" : "account", "_id" : "1", "_score" : 10.605789, "_source" : { "account_number" : 1, "balance" : 39225, "firstname" : "Amber", "lastname" : "Duke", "age" : 32, "gender" : "M", "address" : "880 Holmes Lane", "employer" : "Pyrami", "email" : "amberduke@pyrami.com", "city" : "Brogan", "state" : "IL" } }, { "_index" : "bank", "_type" : "account", "_id" : "70", "_score" : 4.1042743, "_source" : { "account_number" : 70, "balance" : 38172, "firstname" : "Deidre", "lastname" : "Thompson", "age" : 33, "gender" : "F", "address" : "685 School Lane", "employer" : "Netplode", "email" : "deidrethompson@netplode.com", "city" : "Chestnut", "state" : "GA" } }, …… ] } }
如果不想分词,可使用match_phrase语法,即将match替换成match_phrase即可
GET bank/_search { "query": { "match_phrase": { "address": "Holmes Lane" } } }
全部返回:
{ "took" : 9, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 10.605789, "hits" : [ { "_index" : "bank", "_type" : "account", "_id" : "1", "_score" : 10.605789, "_source" : { "account_number" : 1, "balance" : 39225, "firstname" : "Amber", "lastname" : "Duke", "age" : 32, "gender" : "M", "address" : "880 Holmes Lane", "employer" : "Pyrami", "email" : "amberduke@pyrami.com", "city" : "Brogan", "state" : "IL" } } ] } }
多字段匹配
在字段address或city包含的mill Movico
GET bank/_search { "query": { "multi_match": { "query": "mill Movico", "fields": ["address","city"] } } }
返回结果:
{ "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" : "account", "_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" : "account", "_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" : "account", "_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" : "account", "_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" } } ] } }
从返回结果来看,多字段匹配时仍会分词
term:和match一样。匹配某个属性的值,如果用term修饰text,会查不出来数据
全文检索字段用match,
其他非text字段匹配用term。
GET bank/_search { "query": { "term": { "balance":39225 } } }
keyword:也是精确匹配,和match_phrase的区别是,match_phrase的功能相当于java的String.contains,.keyword相当于java的String.equals
GET bank/_search { "query": { "match": { "address.keyword": "Holmes Lane"##address必须完全等于Holmes Lane } } }
结果:
{ "took" : 0, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 0, "relation" : "eq" }, "max_score" : null, "hits" : [ ] } }