简体   繁体   中英

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:

Special characters that require escaping include the following: + - & |? ( ) { } [ ] ^ " ~ *: : \ /

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.

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:

"search=banking -account" and "search=banking \-account" (with escaped "-")produce different number of results.

Is there a way to programmatically decide when special charachter needs escaping or am I missing something obvious here?

If you are using standard.lucence most of the special characters will act as a delimiter. 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

Are you expecting the user to enter operators? If not you can escape all the special character that user enters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM