本文介绍了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":"[email protected]"}}]}}}

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

ES returns all emails which contain "gmail.com"

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

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"上进行匹配时,这是一件了不起的事情.但这也意味着将"[email protected]"视为三个单词-"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 "[email protected]" 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":"[email protected]"}}]}}}

这篇关于Elasticsearch匹配VS query_string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 23:49