Lucene's Highlighter.getBestFragments method return empty string when a query contains forward slash

874 Views Asked by At

I have an application in which Highlighter.getBestFragments return correctly when a query like "fulltext:rajath AND filepath:rajath" is entered. However if the same query contain a "/" in the filepath is entered, meaning if the query is like "fulltext:rajath AND filepath:rajath/rajath1", Highlighter.getBestFragments method returns an empty string. Please let me know how this problem could be solved. I am suspecting heavily that the issue is because of the forward slash in the query. Is there any way to escape it?

Currently I am using lucene 2.9.3 in my application where the highlighting is not happening correctly. But the query "fulltext:rajath AND filepath:rajath/rajath1" used to highlight keyword rajath correctly in lucene 2.2.0. Both in 2.2.0 and 2.9.3 When "fulltext:rajath AND filepath:rajath/rajath1" is entered, internal query changes to [+fulltext:rajath +filepath:"rajath rajath1"]

1

There are 1 best solutions below

1
On

in Lucene 4.x forward slash (/) is now a special character, which is used to signal a regular expression search. You will need to escape your slashes, like:

String query = "filepath:rajath\\/rajath1";

Or you can have lucene do the escaping for you, like:

String searchfor = "rajath/rajath1";
String escapedsearch = QueryParserBase.escape(searchfor);
String query = "filepath:" + escapedsearch;