]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-3017 Made keyword search quietly absorb * chars.
authorPatrick Schmitz <pschmitz@berkeley.edu>
Sat, 3 Dec 2011 01:47:53 +0000 (01:47 +0000)
committerPatrick Schmitz <pschmitz@berkeley.edu>
Sat, 3 Dec 2011 01:47:53 +0000 (01:47 +0000)
services/common/src/main/java/org/collectionspace/services/common/ResourceBase.java
services/common/src/main/java/org/collectionspace/services/common/query/nuxeo/QueryManagerNuxeoImpl.java

index 29b72a3524a315b9997ec7731ef5fde011a1065a..3bf2cbb457df4d97d6d318d9bd67ce617b56a37b 100644 (file)
@@ -27,6 +27,7 @@ import org.collectionspace.services.client.IQueryManager;
 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
@@ -277,10 +278,16 @@ public abstract class ResourceBase
         // 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
index 151f745fb9e09b8558142a4377bd3dc0bbf0fdda..470476f2511e4f0e53ad6e199a5cf0634d649c9b 100644 (file)
@@ -65,7 +65,7 @@ public class QueryManagerNuxeoImpl implements IQueryManager {
        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
@@ -157,8 +157,7 @@ public class QueryManagerNuxeoImpl implements IQueryManager {
        @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
@@ -177,16 +176,6 @@ public class QueryManagerNuxeoImpl implements IQueryManager {
                                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
@@ -198,89 +187,49 @@ public class QueryManagerNuxeoImpl implements IQueryManager {
                        // 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