Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

One of the differences between ES query_string match

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

A problem was found online today, which leads to thinking about the difference between query_string and match.

Curl-XGET 'http://localhost:9200/*/*/_search?pretty'-d' {"from": 0, "size": 10, "fields": ["title"], "query": {"query_string": {"query": "100CrMo7 + round steel", "fields": ["title"]}}'| grep "100CrMo7"

In this example, look in title for data that contains 100CrMo7 and round steel.

Curl-XGET 'http://localhost:9200/*/*/_search?pretty'-d' {"from": 0, "size": 10, "fields": ["title"], "query": {"query_string": {"query": "100CrMo7-round steel", "fields": ["title"]}}'| grep "100CrMo7"

In this example, look in title for data that contains 100CrMo7 but does not contain round steel.

Note: the result of replacing it with simple_query_string seems to be unsatisfactory. One or two pieces of data have not been filtered out.

Take a look at the following example

Curl-XGET 'http://localhost:9200/*/*/_search?pretty'-d' {"size": 10, "fields": ["title"], "query": {"bool": {"must": {"match": {"_ all": {"query": "100CrMo7-round steel" "type": "boolean", "operator": "AND", "boost": 1.0}}'| grep "100CrMo7"

Whether it is 100CrMo7-round steel or 100CrMo7 + round steel, the result does not change.

Look at the document and explain it as follows:

The match family of queries does not go through a "query parsing" process. It does not support field name prefixes, wildcard characters, or other "advanced" features.

That is to say, the latter has not gone through the process of query and analysis.

So if you use the latter with the following code: QueryParser.escape (keyword.trim ()), if there is a special symbol such as "-" in the keyword, such as 100CrMo7-3, then your query result may be empty. So use QueryParser.escape (keyword.trim ()) on a case-by-case basis.

Let's take a look at its source code:

Public static String escape (String s) {StringBuilder sb = new StringBuilder (); for (int I = 0; I

< s.length(); i++) { char c = s.charAt(i); // These characters are part of the query syntax and must be escaped if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')' || c == ':' || c == '^' || c == '[' || c == ']' || c == '\"' || c == '{' || c == '}' || c == '~' || c == '*' || c == '?' || c == '|' || c == '&' || c == '/') { sb.append('\\'); } sb.append(c); } return sb.toString(); } 因为符号- 被转译了。 先看QueryStringQueryParser的解析过程。

Let's look at the MatchQueryParser class.

The latter does not translate special characters, which explains that the result cannot be found using QueryParser.escape (keyword.trim ()).

Continue to view matchQuery.parse

It simply invokes the query analyzer (set in mapping) to parse the fields.

Click parseContext.queryParser

See that there are a lot of parses, including

< >

Wait for symbols.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 284

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report