import org.collectionspace.services.client.PoxPayloadIn;\r
import org.collectionspace.services.client.PoxPayloadOut;\r
import org.collectionspace.services.common.api.RefName;\r
+import org.collectionspace.services.common.api.Tools;\r
import org.collectionspace.services.common.authorityref.AuthorityRefList;\r
import org.collectionspace.services.common.context.MultipartServiceContextImpl;\r
import org.collectionspace.services.common.context.ServiceBindingUtils;\r
// perform a keyword search\r
if (keywords != null && !keywords.isEmpty()) {\r
String whereClause = QueryManager.createWhereClauseFromKeywords(keywords);\r
- DocumentFilter documentFilter = handler.getDocumentFilter();\r
- documentFilter.appendWhereClause(whereClause, IQueryManager.SEARCH_QUALIFIER_AND);\r
- if (logger.isDebugEnabled()) {\r
- logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());\r
+ if(Tools.isEmpty(whereClause)) {\r
+ if (logger.isDebugEnabled()) {\r
+ logger.debug("The WHERE clause is empty for keywords: ["+keywords+"]");\r
+ }\r
+ } else {\r
+ DocumentFilter documentFilter = handler.getDocumentFilter();\r
+ documentFilter.appendWhereClause(whereClause, IQueryManager.SEARCH_QUALIFIER_AND);\r
+ if (logger.isDebugEnabled()) {\r
+ logger.debug("The WHERE clause is: " + documentFilter.getWhereClause());\r
+ }\r
}\r
}\r
if (advancedSearch != null && !advancedSearch.isEmpty()) {\r
private static Pattern kwdTokenizer = Pattern.compile("(?:(['\"])(.*?)(?<!\\\\)(?>\\\\\\\\)*\\1|([^ ]+))");\r
private static Pattern unescapedDblQuotes = Pattern.compile("(?<!\\\\)\"");\r
private static Pattern unescapedSingleQuote = Pattern.compile("(?<!\\\\)'");\r
- private static Pattern kwdSearchProblemChars = Pattern.compile("[\\:\\(\\)]");\r
+ private static Pattern kwdSearchProblemChars = Pattern.compile("[\\:\\(\\)\\*\\%]");\r
private static Pattern kwdSearchHyphen = Pattern.compile(" - ");\r
\r
private static String getLikeForm() {\r
@Override\r
public String createWhereClauseFromKeywords(String keywords) {\r
String result = null;\r
- StringBuffer fullTextWhereClause = new StringBuffer(SEARCH_GROUP_OPEN);\r
- // StringBuffer phraseWhereClause = new StringBuffer(SEARCH_GROUP_OPEN);\r
+ StringBuffer fullTextWhereClause = new StringBuffer();\r
// Split on unescaped double quotes to handle phrases\r
Matcher regexMatcher = kwdTokenizer.matcher(keywords.trim());\r
boolean addNOT = false;\r
addNOT = true;\r
continue;\r
}\r
- if (newWordSet) {\r
- fullTextWhereClause.append(ECM_FULLTEXT_LIKE + "'");\r
- newWordSet = false;\r
- } else {\r
- fullTextWhereClause.append(SEARCH_TERM_SEPARATOR);\r
- }\r
- if(addNOT) {\r
- fullTextWhereClause.append("-"); // Negate the next term\r
- addNOT = false;\r
- }\r
// Next comment block of questionable value...\r
\r
// ignore the special chars except single quote here - can't hurt\r
// which triggers the back-up search. We can think about whether\r
// stripping\r
// short words not in a quoted phrase should trigger the backup.\r
- phrase = unescapedSingleQuote.matcher(phrase).replaceAll("\\\\'");\r
+ String escapedAndTrimmed = unescapedSingleQuote.matcher(phrase).replaceAll("\\\\'");\r
// If there are non-word chars in the phrase, we need to match the\r
// phrase exactly against the fulltext table for this object\r
// if(nonWordChars.matcher(trimmed).matches()) {\r
// }\r
// Replace problem chars with spaces. Patches CSPACE-4147,\r
// CSPACE-4106\r
- phrase = kwdSearchProblemChars.matcher(phrase).replaceAll(" ");\r
- phrase = kwdSearchHyphen.matcher(phrase).replaceAll(" ");\r
-\r
- fullTextWhereClause.append(phrase);\r
- \r
- if (logger.isTraceEnabled() == true) {\r
- logger.trace("Current built whereClause is: "\r
- + fullTextWhereClause.toString());\r
+ escapedAndTrimmed = kwdSearchProblemChars.matcher(escapedAndTrimmed).replaceAll(" ").trim();\r
+ escapedAndTrimmed = kwdSearchHyphen.matcher(escapedAndTrimmed).replaceAll(" ").trim();\r
+ if(escapedAndTrimmed.isEmpty()) {\r
+ if (logger.isDebugEnabled() == true) {\r
+ logger.debug("Phrase reduced to empty after replacements: " + phrase);\r
+ }\r
+ continue;\r
}\r
- }\r
- if (fullTextWhereClause.length()==0) {\r
- throw new RuntimeException(\r
- "No usable keywords specified in string:[" + keywords + "]");\r
- }\r
- fullTextWhereClause.append("'" + SEARCH_GROUP_CLOSE);\r
-\r
- result = fullTextWhereClause.toString();\r
- if (logger.isDebugEnabled()) {\r
- logger.debug("Final built WHERE clause is: " + result);\r
- }\r
\r
- return result;\r
- }\r
-\r
- public String createWhereClauseFromKeywordsOld(String keywords) {\r
- String result = null;\r
- StringBuffer fullTextWhereClause = new StringBuffer(SEARCH_GROUP_OPEN);\r
- // StringBuffer phraseWhereClause = new StringBuffer(SEARCH_GROUP_OPEN);\r
- boolean phrasesToAdd = false;\r
- // Split on unescaped double quotes to handle phrases\r
- String[] phrases = unescapedDblQuotes.split(keywords.trim());\r
- boolean first = true;\r
- for (String phrase : phrases) {\r
- String trimmed = phrase.trim();\r
- // Ignore empty strings from match, or goofy input\r
- if (trimmed.isEmpty())\r
- continue;\r
- // Add the phrase to the string to pass in for full text matching.\r
- // Note that we can pass in a set of words and it will do the OR for\r
- // us.\r
- if (first) {\r
+ if (fullTextWhereClause.length()==0) {\r
+ fullTextWhereClause.append(SEARCH_GROUP_OPEN);\r
+ }\r
+ if (newWordSet) {\r
fullTextWhereClause.append(ECM_FULLTEXT_LIKE + "'");\r
- first = false;\r
+ newWordSet = false;\r
} else {\r
fullTextWhereClause.append(SEARCH_TERM_SEPARATOR);\r
}\r
- // ignore the special chars except single quote here - can't hurt\r
- // TODO this should become a special function that strips things the\r
- // fulltext will ignore, including non-word chars and too-short\r
- // words,\r
- // and escaping single quotes. Can return a boolean for anything\r
- // stripped,\r
- // which triggers the back-up search. We can think about whether\r
- // stripping\r
- // short words not in a quoted phrase should trigger the backup.\r
- trimmed = unescapedSingleQuote.matcher(trimmed).replaceAll("\\\\'");\r
- // If there are non-word chars in the phrase, we need to match the\r
- // phrase exactly against the fulltext table for this object\r
- // if(nonWordChars.matcher(trimmed).matches()) {\r
- // }\r
- // Replace problem chars with spaces. Patches CSPACE-4147,\r
- // CSPACE-4106\r
- trimmed = kwdSearchProblemChars.matcher(trimmed).replaceAll(" ");\r
- trimmed = kwdSearchHyphen.matcher(trimmed).replaceAll(" ");\r
-\r
- fullTextWhereClause.append(trimmed);\r
+ if(addNOT) {\r
+ fullTextWhereClause.append("-"); // Negate the next term\r
+ addNOT = false;\r
+ }\r
+ fullTextWhereClause.append(escapedAndTrimmed);\r
+ \r
if (logger.isTraceEnabled() == true) {\r
logger.trace("Current built whereClause is: "\r
+ fullTextWhereClause.toString());\r
}\r
}\r
- if (first) {\r
- throw new RuntimeException(\r
- "No usable keywords specified in string:[" + keywords + "]");\r
+ if (fullTextWhereClause.length()==0) {\r
+ if (logger.isDebugEnabled() == true) {\r
+ logger.debug("No usable keywords specified in string:[" + keywords + "]");\r
+ }\r
+ } else {\r
+ fullTextWhereClause.append("'" + SEARCH_GROUP_CLOSE);\r
}\r
- fullTextWhereClause.append("'" + SEARCH_GROUP_CLOSE);\r
\r
result = fullTextWhereClause.toString();\r
if (logger.isDebugEnabled()) {\r