[英]C# NOT escaping search operators in Lucene query
According to https://docs.microsoft.com/en-us/azure/search/query-lucene-syntax special charachters should be escaped:根据https://docs.microsoft.com/en-us/azure/search/query-lucene-syntax特殊字符应该被转义:
Special characters that require escaping include the following: + - & |?需要 escaping 的特殊字符包括: + - & |? ( ) { } [ ] ^ " ~ *: : \ / ( ) { } [ ] ^ " ~ *: : \ /
The issue is that some of them are search operators: + - |问题是其中一些是搜索运算符:+ - | ". ”。
Same issue with quotations marks ". I need to distinguish case when they are operators: "banking account" and banking account (without quotations) are 2 different queries.与引号相同的问题“。当它们是运算符时,我需要区分大小写:“银行账户”和银行账户(不带引号)是 2 个不同的查询。
And for example quotations could be inches: ten" (ten inches).例如,报价可能是英寸:十英寸(十英寸)。
Would be nice to have like this:要是有这样的就好了:
string searchPhrase = banking -account // no need to escape because "-" is a search operator
string searchPhrase = "banking :-\) sucks" // needed to escape because "-" is not a search operator
string searchPhrase = bank "account opening" // no needed to escape
string searchPhrase = 100\" // inches not a search operator must be escaped
Written above was tested with search query against Azure search index:上面写的内容是针对 Azure 搜索索引的搜索查询进行测试的:
"search=banking -account" and "search=banking \-account" (with escaped "-")produce different number of results. “search=banking -account”和“search=banking \-account” (带有转义的“-”)产生不同数量的结果。
Is there a way to programmatically decide when special charachter needs escaping or am I missing something obvious here?有没有办法以编程方式决定特殊字符何时需要 escaping 或者我在这里遗漏了一些明显的东西?
If you are using standard.lucence most of the special characters will act as a delimiter.如果您使用的是standard.lucence,大多数特殊字符将充当分隔符。 And you data will not indexed along with special character.而且您的数据不会与特殊字符一起索引。 You can refer this link and test how your data is indexed using different analyzer.您可以参考此链接并测试您的数据如何使用不同的分析器进行索引。
https://docs.microsoft.com/en-us/rest/api/searchservice/test-analyzer https://docs.microsoft.com/en-us/rest/api/searchservice/test-analyzer
Are you expecting the user to enter operators?您是否希望用户输入运算符? If not you can escape all the special character that user enters.如果不是,您可以转义用户输入的所有特殊字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.