How to detect an entered search query as a surround query or as a normal lucene core library query

StackOverflow https://stackoverflow.com/questions/14776344

Is there a way where we can find out whether a query is for the normal lucene core library or for the surround contrib module. Can we merge a surround queryparser with the lucene core library's queryparser, so that we have only one queryparser which detects a syntax as a surround query or the normal lucene query.

有帮助吗?

解决方案

I don't really know all that much about the Surround query parser, but I'm fairly sure that both surround and the default query parser handle some of the same syntax, so you can't strictly differentiate for all cases. I would think, rather than trying to detect the syntax and pass it to a parser accordingly, instead pick which parser should be tried by default, and fall back to the other one if it fails.

Something like:

//Assuming you have a lucene default QueryParser and IndexSearcher lying around.
try {
    query = org.apache.lucene.queryparser.surround.parser.QueryParser.parse(queryString);
} catch (ParseException pe) {
    query = defaultParser.parse(queryString);
}

searcher.search(query, numHits);

You can certainly encapsulate that in your own QueryParser implementation, if you wish. Just implement parse(String) with that logic.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top