match 匹配字段,会对条件分词,然后每个词以or的关系在文档倒排索引内进行查询
GET bank/_search
{
"query": {
"match": {
"address": "244 Columbus Place"
}
}
}
可以看到
上面两条数据的 address 的相同点就是 都有244。
GET _analyze
{
"text": ["244 Columbus Place"]
}
# res 可以看到,拆分成了 244,columbus,place;
# 实际查询条件为 244 or columbus or place
{
"tokens" : [
{
"token" : "244",
"start_offset" : 0,
"end_offset" : 3,
"type" : "" ,
"position" : 0
},
{
"token" : "columbus",
"start_offset" : 4,
"end_offset" : 12,
"type" : "" ,
"position" : 1
},
{
"token" : "place",
"start_offset" : 13,
"end_offset" : 18,
"type" : "" ,
"position" : 2
}
]
}
不对条件值分词,直接拿到文档里面找;只要能找到这个完整的关键词就能匹配。
文档字段包含这个词就能匹配
GET bank/_search
{
"query": {
"match_phrase": {
"address": "244 Columbus Place"
}
}
}
# 可以看到,只查询到一条数据
{
"took" : 7,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 14.199377,
"hits" : [
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "0",
"_score" : 14.199377,
"_source" : {
"account_number" : 0,
"balance" : 16623,
"firstname" : "Bradshaw",
"lastname" : "Mckenzie",
"age" : 29,
"gender" : "F",
"address" : "244 Columbus Place",
"employer" : "Euron",
"email" : "[email protected]",
"city" : "Hobucken",
"state" : "CO"
}
}
]
}
}
在实际使用中,往往是在多个条件下查询数据。
在 query 下使用 bool 聚合多条件。
以下查询条件为:
GET bank/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"address": "244 Columbus Place"
}
},
{
"match": {
"age": 29
}
}
]
}
}
}