Solr查询中需要转义哪些特殊字符? [英] Which special characters need escaping in a solr query?

查看:200
本文介绍了Solr查询中需要转义哪些特殊字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:我认为这个问题通常与solr语法有关,而与Chef无关.因此,当我与Chef进行这项工作时,我想与Solr合作的任何人也会遇到这种情况...

Update: I think this question has to do with solr syntax in general, and not Chef in particular. So while I ran into this working with Chef, I presume that anyone working with Solr will also experience this...

我正在开发一个与Chef服务器的搜索API通信以查找特定节点的应用程序.

I'm working on an application that communicates with the Chef server's search API to find particular nodes.

基于此 http://docs.opscode.com/essentials_search.html#special-characters,似乎需要转义一些特殊字符.

Based on this http://docs.opscode.com/essentials_search.html#special-characters, it seems that a number of special characters need to be escaped.

注意::我只关注完全匹配的模式,通配符.我意识到其中某些字符是通配符的原因.

Note: I'm only concerned with exact-matching patterns, not wildcards. I realize that the reason some of these characters are wildcards.

这是撰写本文时的清单,该清单是从上面的URL复制的:

Here's the list at the time of this writing, as copied from the URL above:

+  -  &&  | |  !  ( )  { }  [ ]  ^  "  ~  *  ?  :  \

但是,当我尝试使用这些字符的各种knife search命令时,我发现行为不一致.

When I try various knife search commands with these characters, however, I see inconsistent behaviour.

对于以下示例,我设置了一个标有+&|!(){}[]^\"~*?:\\"

For the following examples, I set up a node that is tagged with +&|!(){}[]^\"~*?:\\"

这些命令是在bash外壳中的Linux盒中运行的:

$ knife search node 'tags:+&|!(){}[]^"~*?:\'
ERROR: knife search failed: invalid search query: 'tags:+&|!(){}[]^"~*?:\'

由于没有发生任何泄漏,因此其行为符合预期.现在,我按照文档建议用一个\转义了所有内容:

That behaved as expected, since nothing was escaped. Now, I escape everything with a single \ as the docs suggest:

$ knife search node 'tags:\+\&\|\!\(\)\{\}\[\]\^\"\~\*\?\:\\'
ERROR: knife search failed: invalid search query: 'tags:\+\&\|\!\(\)\{\}\[\]\^\"\~\*\?\:\\'

奇怪.

任何人都可以对此有所了解,并可能提出一个可以匹配该标签的查询吗?

Can anyone shed some light on this, and maybe suggest a query that's capable of matching that tag?

显然不可能有人拥有包含所有那些特殊字符的属性,但是我想更好地理解应该如何转义特殊字符.

It's obviously unlikely that anyone will ever have an attribute containing all those special characters, but I'd like to understand better how the special characters should be escaped.

谢谢!

推荐答案

您需要对正则表达式使用lucene solr语法:

You need to use the lucene solr syntax for regexes: http://lucene.apache.org/core/6_5_1/queryparser/org/apache/lucene/queryparser/classic/package-summary.html#Escaping_Special_Characters

这篇关于Solr查询中需要转义哪些特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆