|
在Elasticsearch查询中, 经常用到 match 与 match_phrase 查询, 下面我们说下两个查询方式的用法
在我们的索引用,有一个test, 里面存储了部分学生信息, 下面我们用关键字“He is”进行检索
POST /test/_doc/_search
[C#] 纯文本查看 复制代码
{
"query": {
"match": {
"description": "He is"
}
}
执行查询,得到结果如下, 得到4条数据:
[C#] 纯文本查看 复制代码
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 0.2169777,
"hits": [
{
"_index": "test",
"_type": "student",
"_id": "2",
"_score": 0.2169777,
"_source": {
"name": "februus",
"sex": "male",
"age": 24,
"description": "He is passionate.",
"interests": "reading, programing"
}
},
{
"_index": "test",
"_type": "student",
"_id": "1",
"_score": 0.16273327,
"_source": {
"name": "leotse",
"sex": "male",
"age": 25,
"description": "He is a big data engineer.",
"interests": "reading, swiming, hiking"
}
},
{
"_index": "test",
"_type": "student",
"_id": "4",
"_score": 0.01989093,
"_source": {
"name": "pascal",
"sex": "male",
"age": 25,
"description": "He works very hard because he wanna go to Canada.",
"interests": "programing, reading"
}
},
{
"_index": "test",
"_type": "student",
"_id": "3",
"_score": 0.016878016,
"_source": {
"name": "yolovon",
"sex": "female",
"age": 24,
"description": "She is so charming and beautiful.",
"interests": "reading, shopping"
}
}
]
}
}
实用match_phrase 进行检索:
POST /test/_doc/_search
[C#] 纯文本查看 复制代码
{
"query": {
"match_phrase": {
"description": "He is"
}
}
}
得到如下2条结果:
[C#] 纯文本查看 复制代码
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.30685282,
"hits": [
{
"_index": "test",
"_type": "student",
"_id": "2",
"_score": 0.30685282,
"_source": {
"name": "februus",
"sex": "male",
"age": 24,
"description": "He is passionate.",
"interests": "reading, programing"
}
},
{
"_index": "test",
"_type": "student",
"_id": "1",
"_score": 0.23013961,
"_source": {
"name": "leotse",
"sex": "male",
"age": 25,
"description": "He is a big data engineer.",
"interests": "reading, swiming, hiking"
}
}
]
}
}
通过上面的查询条件, 可以清晰的看到, 两则得到的数据结果是不一样的, 分析得到:
1. 对于match的结果,我们可以可以看到,结果的Document中description这个field可以包含“He is”,“He”或者“is”;
2. match_phrased的结果中的description字段,必须包含“He is”这一个词组;
通过上面分析就可以看到, match查询,会将词进行拆分, 只要包含相关Document, 都会出现在最终的结果集中
那么,如果我们不想将我们的查询条件拆分,应该怎么办呢?这时候我们就可以使用match_phrase:
match_phrase是短语搜索,亦即它会将给定的短语(phrase)当成一个完整的查询条件。当使用match_phrase进行搜索的时候,你的结果集中,所有的Document都必须包含你指定的查询词组,在这里是“He is”。这看起来有点像关系型数据库的like查询操作。
|
|