ElasticsSearch——Query String
2015-11-25 10:49
507 查看
之前一直觉的QueryString这种查询方式挺好用的,用它的主要场合就是对同一个词针对多字段可以查询,这好像是其他方式所不支持的。今天一个Bug 出来才发现,原来还有很多不了解的地方。
queryString 是能够自动解析查询的字符串的,而且其中还可以使用正则表达式。既方便,但是对于不用这个功能的我,就显得尤为没有必要,因为我的字符串如果包含了他已经实现定义有特定意义的字符,就会报错,因为他误会我的意思了。
Query
string syntax
Elasticsearch
Reference:
master2.x
2.12.0 (current)
1.71.6
1.51.4
1.30.90
Getting
Started
Setup
Breaking
changes
API
Conventions
Document
APIs
Search
APIs
Aggregations
Indices
APIs
cat
APIs
Cluster
APIs
Query
DSL
Query
and filter context
Match
All Query
Full
text queries
Match
Query
Multi
Match Query
Common
Terms Query
Query
String Query
Simple
Query String Query
Term
level queries
Compound
queries
Joining
queries
Geo
queries
Specialized
queries
Span
queries
Minimum
Should Match
Multi
Term Query Rewrite
Mapping
Analysis
Modules
Index
Modules
Testing
Glossary
of terms
Release
Notes
A query that uses a queryparserin order to parse its content.
也就是说这种查询方式有一个自己的解析器会解析输入的查询字符串。
The
can also run against multiple fields. Fields can be provided via the
(example below).
The idea of running the
against multiple fields is to expand each query term to an OR clauselike this:
For example, the following query
matches the same words as
还可以做通配符查询,模糊查询等,但是万万没想到
If you need to use any of the characters which function as operators in your query itself (and not as operators), then you should escape them with a leading backslash. For instance, to search for
you would need to write your query as
The reserved characters are:
Failing to escape these special characters correctly could lead to a syntax error which prevents your query from running.
Watch this space
A space may also be a reserved character. For instance, if you have a synonym list which converts
a
for
Escaping the space will protect it from being touched by the query string parser:
下面来看看错误长什么样子吧
还是改成短语匹配吧,有时间再好好研究规避保留字符的方法。
queryString 是能够自动解析查询的字符串的,而且其中还可以使用正则表达式。既方便,但是对于不用这个功能的我,就显得尤为没有必要,因为我的字符串如果包含了他已经实现定义有特定意义的字符,就会报错,因为他误会我的意思了。
Query String Queryedit
On this page
Querystring syntax
Elasticsearch
Reference:
master2.x
2.12.0 (current)
1.71.6
1.51.4
1.30.90
Getting
Started
Setup
Breaking
changes
API
Conventions
Document
APIs
Search
APIs
Aggregations
Indices
APIs
cat
APIs
Cluster
APIs
Query
DSL
Query
and filter context
Match
All Query
Full
text queries
Match
Query
Multi
Match Query
Common
Terms Query
Query
String Query
Simple
Query String Query
Term
level queries
Compound
queries
Joining
queries
Geo
queries
Specialized
queries
Span
queries
Minimum
Should Match
Multi
Term Query Rewrite
Mapping
Analysis
Modules
Index
Modules
Testing
Glossary
of terms
Release
Notes
A query that uses a queryparserin order to parse its content.
也就是说这种查询方式有一个自己的解析器会解析输入的查询字符串。
Multi Fieldedit
The query_stringquery
can also run against multiple fields. Fields can be provided via the
"fields"parameter
(example below).
The idea of running the
query_stringquery
against multiple fields is to expand each query term to an OR clauselike this:
field1:query_term OR field2:query_term | ...
For example, the following query
{ "query_string" : { "fields" : ["content", "name"], "query" : "this AND that" } }
matches the same words as
{ "query_string": { "query": "(content:this OR name:this) AND (content:that OR name:that)" } }
还可以做通配符查询,模糊查询等,但是万万没想到
Reserved
characters 保留字符edit
If you need to use any of the characters which function as operators in your query itself (and not as operators), then you should escape them with a leading backslash. For instance, to search for (1+1)=2,
you would need to write your query as
\(1\+1\)\=2.
The reserved characters are:
+ - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ /
Failing to escape these special characters correctly could lead to a syntax error which prevents your query from running.
Watch this space
A space may also be a reserved character. For instance, if you have a synonym list which converts
"wi fi"to
"wifi",
a
query_stringsearch
for
"wi fi"would fail. The query string parser would interpret your query as a search for
"wi OR fi", while the token stored in your index is actually
"wifi".
Escaping the space will protect it from being touched by the query string parser:
"wi\ fi".
下面来看看错误长什么样子吧
还是改成短语匹配吧,有时间再好好研究规避保留字符的方法。
现在贴上将字符串中所有正则表达式的字符进行转义的方法
public class StringHelper { /// <summary> /// 转义字符串中所有正则特殊字符 /// </summary> /// <param name="input">传入字符串</param> /// <returns></returns> public static string FilterString(string input) { input = input.Replace("\\", "\\\\");//先替换“\”,不然后面会因为替换出现其他的“\” var r = new Regex("[\\*\\.\\?\\+\\$\\^\\[\\]\\(\\)\\{\\}\\|\\/]"); var ms = r.Matches(input); var list = new List<string>(); foreach (Match item in ms) { if (list.Contains(item.Value)) continue; input = input.Replace(item.Value, "\\" + item.Value); list.Add(item.Value); } return input; } }
相关文章推荐
- UIBarButtonSystemItem样式
- 更换免开发证书账号出现的Your build settings specify a provisioning profile with the UUID"",however,no such provisioning profile was found
- vue.js学习笔记
- 2.AsyncQueryHandler、内容提供者
- R12.2.0 buildStage 运行结果
- Bringing up interface eth0: Error: No suitable device found: no device found for connection 'System
- UCGUI移植
- 这三种特殊权限(suid、sgid、sticky)
- UIBarButtonItem (系统 Item 样式)
- [Built-in Functions] - H
- 【HDU5568 BestCoder Round 63 (div1)A】【DP java高精度】sequence2 长度恰好为m的LIS数
- android studio中R文件丢失以及project目录下build文件丢失解决办法
- Android UI效果实现——Activity滑动退出效果
- Mining the Network Value of Customers
- 初探UiAutomator(四)
- [Built-in Functions] - G
- UESTC 1218 Ancient Go (我的递归~~)
- UITextField详解
- iOS UIButton添加圆角,添加边框
- BlockingQueue的使用