Added the following community contribution from Ray Gauss II "Add configuration to allow basic search to be changed to search X, Y, Z attributes by default."

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@5649 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Kevin Roast
2007-05-09 16:40:28 +00:00
parent 0234890142
commit c9ceb5fc4f
4 changed files with 132 additions and 7 deletions

View File

@@ -123,6 +123,9 @@ public final class SearchContext implements Serializable
/** content mimetype to restrict search against */
private String mimeType = null;
/** any extra simple query attributes to add to the search */
protected List<QName> simpleSearchAdditionalAttrs = new ArrayList<QName>(4);
/** any extra query attributes to add to the search */
private Map<QName, String> queryAttributes = new HashMap<QName, String>(5, 1.0f);
@@ -159,7 +162,17 @@ public final class SearchContext implements Serializable
String text = this.text.trim();
StringBuilder fullTextBuf = new StringBuilder(64);
StringBuilder nameAttrBuf = new StringBuilder(64);
StringBuilder nameAttrBuf = new StringBuilder(128);
StringBuilder additionalAttrsBuf = new StringBuilder(128);
/*Map<QName, StringBuilder> simpleAdditionalAttrBufs = new HashMap<QName, StringBuilder>();
if (this.simpleSearchAdditionalAttrs != null)
{
for (QName qName : this.simpleSearchAdditionalAttrs)
{
simpleAdditionalAttrBufs.put(qName, new StringBuilder(64));
}
}*/
if (text.length() != 0 && text.length() >= minimum)
{
@@ -181,9 +194,14 @@ public final class SearchContext implements Serializable
{
fullTextBuf.append(OP_NOT);
nameAttrBuf.append(OP_NOT);
additionalAttrsBuf.append(OP_NOT);
}
processSearchTextAttribute(nameAttr, text, nameAttrBuf, fullTextBuf);
for (QName qname : this.simpleSearchAdditionalAttrs)
{
processSearchAttribute(qname, text, additionalAttrsBuf, false);
}
}
}
else
@@ -195,6 +213,11 @@ public final class SearchContext implements Serializable
String quotedSafeText = '"' + QueryParser.escape(text.substring(1, text.length() - 1)) + '"';
fullTextBuf.append("TEXT:").append(quotedSafeText);
nameAttrBuf.append("@").append(nameAttr).append(":").append(quotedSafeText);
for (QName qname : this.simpleSearchAdditionalAttrs)
{
additionalAttrsBuf.append(" @").append(
Repository.escapeQName(qname)).append(":").append(quotedSafeText);
}
}
else
{
@@ -203,6 +226,7 @@ public final class SearchContext implements Serializable
fullTextBuf.append('(');
nameAttrBuf.append('(');
additionalAttrsBuf.append('(');
int termCount = 0;
int tokenCount = t.countTokens();
@@ -233,6 +257,7 @@ public final class SearchContext implements Serializable
{
fullTextBuf.append("OR ");
nameAttrBuf.append("OR ");
additionalAttrsBuf.append("OR ");
}
// prepend NOT operator if supplied
@@ -240,24 +265,33 @@ public final class SearchContext implements Serializable
{
fullTextBuf.append(OP_NOT);
nameAttrBuf.append(OP_NOT);
additionalAttrsBuf.append(OP_NOT);
}
// prepend AND operator if supplied
if (operatorAND)
{
fullTextBuf.append(OP_AND);
nameAttrBuf.append(OP_AND);
additionalAttrsBuf.append(OP_AND);
}
processSearchTextAttribute(nameAttr, term, nameAttrBuf, fullTextBuf);
for (QName qname : this.simpleSearchAdditionalAttrs)
{
processSearchAttribute(qname, term, additionalAttrsBuf, false);
}
fullTextBuf.append(' ');
nameAttrBuf.append(' ');
additionalAttrsBuf.append(' ');
termCount++;
}
}
fullTextBuf.append(')');
nameAttrBuf.append(')');
additionalAttrsBuf.append(')');
}
}
@@ -388,14 +422,16 @@ public final class SearchContext implements Serializable
String fullTextQuery = fullTextBuf.toString();
String nameAttrQuery = nameAttrBuf.toString();
String additionalAttrsQuery = additionalAttrsBuf.toString();
if (text.length() != 0 && text.length() >= minimum)
{
// text query for name and/or full text specified
switch (mode)
{
case SearchContext.SEARCH_ALL:
query = '(' + fileTypeQuery + " AND " + '(' + nameAttrQuery + ' ' + fullTextQuery + ')' + ')' + " OR " +
'(' + folderTypeQuery + " AND " + nameAttrQuery + ')';
query = '(' + fileTypeQuery + " AND " + '(' + nameAttrQuery + ' ' + additionalAttrsQuery + ' ' + fullTextQuery + ')' + ')' + " OR " +
'(' + folderTypeQuery + " AND " + nameAttrQuery + ' ' + additionalAttrsQuery + ')';
break;
case SearchContext.SEARCH_FILE_NAMES:
@@ -472,6 +508,20 @@ public final class SearchContext implements Serializable
* @param buf Buffer to append lucene terms to
*/
private static void processSearchAttribute(QName qname, String value, StringBuilder buf)
{
processSearchAttribute(qname, value, buf, true);
}
/**
* Build the lucene search terms required for the specified attribute and append to a buffer.
* Supports text values with a wildcard '*' character as the prefix and/or the suffix.
*
* @param qname QName of the attribute
* @param value Non-null value of the attribute
* @param buf Buffer to append lucene terms to
* @param andOp If true apply the '+' AND operator as the prefix to the attribute term
*/
private static void processSearchAttribute(QName qname, String value, StringBuilder buf, boolean andOp)
{
if (value.indexOf(' ') == -1)
{
@@ -510,14 +560,16 @@ public final class SearchContext implements Serializable
}
}
buf.append(" +@").append(Repository.escapeQName(qname)).append(":")
.append(prefix).append(safeValue).append(suffix);
if (andOp) buf.append('+');
buf.append('@').append(Repository.escapeQName(qname)).append(":")
.append(prefix).append(safeValue).append(suffix).append(' ');
}
else
{
// phrase multi-word search
String safeValue = QueryParser.escape(value);
buf.append(" +@").append(Repository.escapeQName(qname)).append(":\"").append(safeValue).append('"');
if (andOp) buf.append('+');
buf.append('@').append(Repository.escapeQName(qname)).append(":\"").append(safeValue).append('"').append(' ');
}
}
@@ -733,6 +785,27 @@ public final class SearchContext implements Serializable
this.mimeType = mimeType;
}
/**
* Add an additional attribute to search against for simple searches
*
* @param qname QName of the attribute to search against
* @param value Value of the attribute to use
*/
public void addSimpleAttributeQuery(QName qname)
{
this.simpleSearchAdditionalAttrs.add(qname);
}
/**
* Sets the additional attribute to search against for simple searches.
*
* @param attrs The list of attributes to search against
*/
public void setSimpleSearchAdditionalAttributes(List<QName> attrs)
{
this.simpleSearchAdditionalAttrs = attrs;
}
/**
* Add an additional attribute to search against
*

View File

@@ -24,6 +24,8 @@
*/
package org.alfresco.web.config;
import java.util.List;
import javax.faces.context.FacesContext;
import org.alfresco.config.ConfigElement;
@@ -31,6 +33,7 @@ import org.alfresco.config.JNDIConstants;
import org.alfresco.config.element.ConfigElementAdapter;
import org.alfresco.mbeans.VirtServerRegistry;
import org.alfresco.repo.cache.ExpiringValueCache;
import org.alfresco.service.namespace.QName;
import org.alfresco.web.bean.repository.Repository;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -68,6 +71,7 @@ public class ClientConfigElement extends ConfigElementAdapter
private boolean clipboardStatusVisible = true;
private boolean pasteAllAndClear = true;
private boolean allowGuestConfig = false;
private List<QName> simpleSearchAdditionalAttributes = null;
/**
* Default Constructor
@@ -204,6 +208,12 @@ public class ClientConfigElement extends ConfigElementAdapter
combinedElement.setAllowGuestConfig(newElement.getAllowGuestConfig());
}
if (newElement.getSimpleSearchAdditionalAttributes() != null &&
newElement.getSimpleSearchAdditionalAttributes().equals(combinedElement.getSimpleSearchAdditionalAttributes()) == false)
{
combinedElement.setSimpleSearchAdditionalAttributes(newElement.getSimpleSearchAdditionalAttributes());
}
return combinedElement;
}
@@ -547,4 +557,20 @@ public class ClientConfigElement extends ConfigElementAdapter
{
return this.allowGuestConfig;
}
/**
* @return Returns the additional attributes to search on a simple search
*/
public List<QName> getSimpleSearchAdditionalAttributes()
{
return this.simpleSearchAdditionalAttributes;
}
/**
* @param simpleSearchAdditionalAttributes The additional simple search attributes
*/
/*package*/ void setSimpleSearchAdditionalAttributes(List<QName> simpleSearchAdditionalAttributes)
{
this.simpleSearchAdditionalAttributes = simpleSearchAdditionalAttributes;
}
}

View File

@@ -24,9 +24,14 @@
*/
package org.alfresco.web.config;
import java.util.ArrayList;
import java.util.List;
import org.alfresco.config.ConfigElement;
import org.alfresco.config.ConfigException;
import org.alfresco.config.xml.elementreader.ConfigElementReader;
import org.alfresco.service.namespace.QName;
import org.dom4j.Element;
/**
@@ -54,6 +59,8 @@ public class ClientElementReader implements ConfigElementReader
public static final String ELEMENT_CLIPBOARDSTATUS = "clipboard-status-visible";
public static final String ELEMENT_PASTEALLANDCLEAR = "paste-all-and-clear";
public static final String ELEMENT_GUESTCONFIG = "allow-guest-config";
public static final String ELEMENT_SIMPLESEARCHADDITIONALATTRS = "simple-search-additional-attributes";
public static final String ELEMENT_SIMPLESEARCHADDITIONALATTRSQNAME = "qname";
/**
* @see org.alfresco.config.xml.elementreader.ConfigElementReader#parse(org.dom4j.Element)
@@ -160,7 +167,7 @@ public class ClientElementReader implements ConfigElementReader
configElement.setLoginPage(loginPage.getTextTrim());
}
// get the ajax enabled flag
// get the node summary popup enabled flag
Element ajaxEnabled = element.element(ELEMENT_NODESUMMARY_ENABLED);
if (ajaxEnabled != null)
{
@@ -204,6 +211,23 @@ public class ClientElementReader implements ConfigElementReader
boolean allow = Boolean.parseBoolean(guestConfigElement.getTextTrim());
configElement.setAllowGuestConfig(allow);
}
// get the additional simple search attributes
Element simpleSearchAdditionalAttributesElement = element.element(ELEMENT_SIMPLESEARCHADDITIONALATTRS);
if (simpleSearchAdditionalAttributesElement != null)
{
List<Element> attrbElements =
simpleSearchAdditionalAttributesElement.elements(ELEMENT_SIMPLESEARCHADDITIONALATTRSQNAME);
if (attrbElements != null && attrbElements.size() != 0)
{
List<QName> simpleSearchAddtlAttrb = new ArrayList<QName>(4);
for (Element elem : attrbElements)
{
simpleSearchAddtlAttrb.add(QName.createQName(elem.getTextTrim()));
}
configElement.setSimpleSearchAdditionalAttributes(simpleSearchAddtlAttrb);
}
}
}
return configElement;

View File

@@ -42,6 +42,7 @@ import org.alfresco.web.bean.SearchContext;
import org.alfresco.web.ui.common.Utils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* @author Kevin Roast
*/
@@ -151,6 +152,7 @@ public class UISimpleSearch extends UICommand
context.setText(searchEvent.SearchText);
context.setMode(searchEvent.SearchMode);
context.setForceAndTerms(Application.getClientConfig(fc).getForceAndTerms());
context.setSimpleSearchAdditionalAttributes(Application.getClientConfig(fc).getSimpleSearchAdditionalAttributes());
this.search = context;
super.broadcast(event);