es中match_phrase和term区别

时间:2024-05-18 12:45:05

我们知道,match会对输入进行分词处理后再去查询。
但是match_phrase和term都是不分词的精确查询,这二者有什么区别呢?

【发现问题】
1.我们先放入一个文档:
es中match_phrase和term区别
2.然后使用match_phrase搜索:
es中match_phrase和term区别
es中match_phrase和term区别
嗯,不出所料,搜索到了。

3.然后我们用term试试
es中match_phrase和term区别
es中match_phrase和term区别
纳尼!!!居然没有?那么全句带进去搜总行了吧
es中match_phrase和term区别
es中match_phrase和term区别
居然还是不行!!这就奇怪了。

【问题分析】我们来看看这句话的分词结果:
es中match_phrase和term区别es中match_phrase和term区别
我们会发现,中文竟然被默认分词器分词单个字了!尴尬啊!!!
这就不难理解为啥term搜“学习”搜不到,搜全文也搜不到,因为存的是“学”和“习”。

【总结】如果使用term查询,要确保字段是no analyzed的。建索引的时候要注意。