【Elasticsearch】关于match_phrase, simple_query_string, query_string这三种查询的区别

通配符"*"在三种查询关键字中的区别,以下是在elasticsearch采用standard analyze的前提下在ES集群中um.qa.article.2019.07.25做的测试

  • 1
查询方式 查询DSL 查询结果描述
match_phrase 1 {"query" : {"bool" : {"must" : [{"bool" : {"should" : [{"match_phrase" : {"mainContent" : "CS"}}]}}]}}} 返回包含“CS”关键字并且该关键字前后没有字母或数字的数据
query_string 1 {"query" : {"bool" : {"must" : [{"bool" : {"should" : [{"query_string" : {"default_field" : "mainContent","query" : "CS"}}]}}]}}} 同match_phrase 1
simple_query_string 1 {"query" : {"bool" : {"must" : [{"simple_query_string" : {"fields" : ["mainContent"],"query" : "CS"}}]}}]}}} 同match_phrase 1

结论:以上三种查询等价


  • 2
查询方式 查询DSL 查询结果描述
match_phrase 2 {"query" : {"bool" : {"must" : [{"bool" : {"should" : [{"match_phrase" : {"mainContent" : "CS*"}}]}}]}}} 会把*去掉再进行匹配,同match_phrase 1
query_string 2 {"query" : {"bool" : {"must" : [{"bool" : {"should" : [{"query_string" : {"default_field" : "mainContent","query" : "CS*"}}]}}]}}} 返回包含“CS”关键字并且该关键字面无字母或数字的数据,“*”是一个通配符
simple_query_string 2 {"query" : {"bool" : {"must" : [{"simple_query_string" : {"fields" : ["mainContent"],"query" : "CS*"}}]}}]}}} 结果同query_string 2,但是此处的“*”表示前缀查询

结论:*在match_phrase中,会先对其进行删除再查询;在query_string和simple_query_string中会被当做一个通配符,表示0个或多个字符,但是只能表示一个词项的前缀匹配;另外query_string的通配符可以放在最前面来表示一个前置通配符,而simple_query_string第一个词如果是“*”,将没有任何返回结果


  • 3
查询方式 查询DSL 查询结果描述
match_phrase 3 {"query" : {"bool" : {"must" : [{"bool" : {"should" : [{"match_phrase" : {"mainContent" : "CS35有*"}}]}}]}}} 会把*去掉再进行匹配
query_string 3 {"query" : {"bool" : {"must" : [{"bool" : {"should" : [{"query_string" : {"default_field" : "mainContent","query" : "CS35有*"}}]}}]}}} 无返回结果
simple_query_string 3 {"query" : {"bool" : {"must" : [{"simple_query_string" : {"fields" : ["mainContent"],"query" : "CS35有*"}}]}}]}}} 无返回结果

结论:“CS35有*”在match_phrase中会被当成“CS35有”来进行匹配,只要包含“CS35有”就会命中;在query_string和simple_query_string中,被当作一个词项的前缀表达式,由于默认的分词器永远不可能把“CS35有”当作一个此项,所以不会有任何结果返回

发布了14 篇原创文章 · 获赞 1 · 访问量 842

猜你喜欢

转载自blog.csdn.net/linclaus/article/details/104596675