How to properly escape OR and AND in lucene query?

我的梦境 提交于 2020-01-03 09:03:40

问题


When I passed in a query "state:OR" lucene gave an error because it considers "OR" as a keyword for boolean clause, but here I actually man the abbreviation of Oregon, the state.

I have seen that quoting OR so the query becomes 'state:"OR"' makes it work.

but this doesn't sound like a very good approach, since I'll have to do a string substitution for EACH of the keywords that lucene uses: AND OR NOT and others?? I don't how many

I tried directly constructing the query instead of doing queryParser.parse(), but it seems that this does not go through the analyzers, which is a big problem.


回答1:


There are a number of ways to escape this, the cleaner is to escape AND, OR, & NOT with leading backslashes eg:

\\AND \\OR \\NOT

alternately, the code parser will not parse their lowercase equivalents as operators




回答2:


There are only 3 standalone keywords in the Lucene query syntax -- AND, OR, and NOT. ("TO" is also used, but is only recognized inside of a range query.)

It may help that your quoting code only needs to recognize the Lucene keywords actually used as terms in your application (like the "OR" above in your example).



来源:https://stackoverflow.com/questions/10337908/how-to-properly-escape-or-and-and-in-lucene-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!