Demo
搜索名字为Sophia zhou的文档
// request body
{
"query":{
"match":{
"name":"Sophia zhou"
}
}
}
响应内容,可以很明显看到Sophia Wu和John Zhou都被查询出来了,对应的score如下
name | _score |
---|---|
Sophia Zhou | 0.5753642 |
Sophia Wu | 0.2876821 |
John Zhou | 0.2876821 |
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0.5753642,
"hits": [
{
"_index": "corp",
"_type": "employee",
"_id": "2",
"_score": 0.5753642,
"_source": {
"country": "CA",
"name": "Sophia Zhou",
"hobbies": [
"foods",
"read"
],
"age": 15
}
},
{
"_index": "corp",
"_type": "employee",
"_id": "1",
"_score": 0.2876821,
"_source": {
"country": "US",
"name": "Sophia Wu",
"birthday": "1631096936000",
"hobbies": [
"foods",
"play games",
"read",
"talk"
],
"age": 1
}
},
{
"_index": "corp",
"_type": "employee",
"_id": "3",
"_score": 0.2876821,
"_source": {
"country": "CA",
"name": "John Zhou",
"hobbies": [
"swim",
"read"
],
"birthday": 1315477736000,
"age": 11
}
}
]
}
}
解释
可以理解为ES作为全文搜索引擎,会使用分词策略进行查询,Sophia zhou 会被分词为Sophia、zhou两个词,同理三条文档中的name都会被分词,例如John Zhou会被分词为John、Zhou;经过分词后ES会计算文档与查询条件的相关性,查询结果默认根据相关性倒序排列。这也就是ES适合做搜索引擎的原因之一。