Elasticsearch匹配VS query

编程入门 行业动态 更新时间:2024-10-18 20:31:19
本文介绍了Elasticsearch匹配VS query_string的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我的测试:

使用比赛

{"query":{"bool":{"must":[{"match":{"name":{"query":"ka"}}},{"term":{"kind":"k1"}}]}}}

0次点击

然后使用query_string

Then using query_string

{"query":{"bool":{"must":[{"query_string":{"fields":["name"],"query":"*ka*"}},{"term":{"kind":"k1"}}]}}}

约1000多个点击一些名称,例如"katyperry","KathleenLights"等.使用 match

另外,让我更加怀疑的另一个示例是,当我使用 match 搜索电子邮件

In addition, another example that make me even have more doubt is, when I use match to search email

{"query": {"bool": {"must": [{ "match":{"email":"testname@gmail"}}]}}}

ES返回包含"gmail"的所有电子邮件

ES returns all emails which contain "gmail"

那么在这些情况下匹配"如何工作?

So how "match" works in these cases ?

推荐答案

您的第一个查询未返回任何结果,因为您没有使用通配符搜索,即使您不想这样做也不可能,因为"match"不支持通配符.:)改用它:

Your first query returns no results because you aren't using a wildcard search, which you couldn't even if you wanted to because "match" doesn't support wildcards. :) Use this instead:

{"query":{"bool":{"must":[{"wildcard":{"name":"*ka*"}},{"term":{"kind":"k1"}}]}}}

您的上一个查询返回这些结果,因为您将电子邮件另存为已分析的字符串,并且标准分析器将字符串拆分为空白和标点符号.当您为"hello,world"建立索引并且能够在"hello"和"world"上进行匹配时,这是一件了不起的事情.但这也意味着将"testname@gmail"视为三个单词-"testname","gmail"和"com".

Your last query returns those results because your saving the email as an analyzed string, and the standard analyzer splits strings on whitespace and punctuation. This is a great thing when you index "hello,world" and are able to match on "hello" and "world". But it also means that "testname@gmail" is treated as three words -- "testname", "gmail", and "com".

要解决此问题,需要将电子邮件"定义为映射中未经分析的字符串.除非您使用的是v5.0或更高版本,否则在这种情况下-好消息!您已经有一个未经分析的关键字"字段,以下查询将非常适合您:

Fixing this problem requires that define "email" to be a nonanalyzed string in your mapping. Unless you're using v5.0 or greater, in which case -- good news! You already have a "keyword" field that is not analyzed and the following query will magically just work for you:

{"query": {"bool": {"must": [{ "match":{"email.keyword":"testname@gmail"}}]}}}

更多推荐

Elasticsearch匹配VS query

本文发布于:2023-11-01 12:52:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1549385.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Elasticsearch   query

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!