From b510f53e1c7c90677afe8676d2f783bbb1fb4db8 Mon Sep 17 00:00:00 2001 From: rjy7 Date: Thu, 30 Sep 2010 18:41:12 +0000 Subject: [PATCH] Merge r5985 from nihvivo-rel-1.1-maint: query string cleanup to prevent Lucene ParseExceptions --- .../search/controller/AutocompleteController.java | 11 +++++++++-- .../search/controller/PagedSearchController.java | 10 +++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/controller/AutocompleteController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/controller/AutocompleteController.java index 285bf62dd..1c42329d4 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/controller/AutocompleteController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/controller/AutocompleteController.java @@ -282,12 +282,19 @@ public class AutocompleteController extends FreemarkerHttpServlet implements Sea // of wildcard and non-wildcard queries. The query will look have only an implicit disjunction // operator: e.g., +(name:tales name:tales*) try { - Query query = parser.parse(querystr); + // Prevent org.apache.lucene.queryParser.ParseException: + // Cannot parse 'mary *': '*' or '?' not allowed as first character in WildcardQuery + // The * is redundant in this case anyway, so just remove it. + log.debug("Query string is '" + querystr + "'"); + querystr = querystr.replaceAll("([\\s^])[?*]", "$1"); + log.debug("Cleaned query string is '" + querystr + "'"); + log.debug("Adding non-wildcard query for " + querystr); + Query query = parser.parse(querystr); boolQuery.add(query, BooleanClause.Occur.SHOULD); - Query wildcardQuery = parser.parse(querystr + "*"); log.debug("Adding wildcard query for " + querystr); + Query wildcardQuery = parser.parse(querystr + "*"); boolQuery.add(wildcardQuery, BooleanClause.Occur.SHOULD); log.debug("Name query is: " + boolQuery.toString()); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/controller/PagedSearchController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/controller/PagedSearchController.java index 94c2c3847..89f50e00e 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/controller/PagedSearchController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/controller/PagedSearchController.java @@ -278,7 +278,7 @@ public class PagedSearchController extends VitroHttpServlet implements Searcher{ request.getRequestDispatcher(Controllers.BASIC_JSP).forward(request,response); } catch (Throwable e) { - log.error("SearchController.doGet(): " + e); + log.error("PagedSearchController.doGet(): " + e, e); doSearchError(request, response, e.getMessage(), null); return; } @@ -440,6 +440,14 @@ public class PagedSearchController extends VitroHttpServlet implements Searcher{ return null; } QueryParser parser = getQueryParser(analyzer); + + // Prevent org.apache.lucene.queryParser.ParseException: + // Cannot parse 'mary *': '*' or '?' not allowed as first character in WildcardQuery + // The * is redundant in this case anyway, so just remove it. + log.debug("Query string is '" + querystr + "'"); + querystr = querystr.replaceAll("([\\s^])[?*]", "$1"); + log.debug("Cleaned query string is '" + querystr + "'"); + query = parser.parse(querystr); String alpha = request.getParameter("alpha");