SHA-1771: Update Search script object to support search term highlighting

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@131523 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
David Draper
2016-10-18 16:02:13 +00:00
parent 829474d7e4
commit edba48ea75

View File

@@ -49,6 +49,8 @@ import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.search.FieldHighlightParameters;
import org.alfresco.service.cmr.search.GeneralHighlightParameters;
import org.alfresco.service.cmr.search.LimitBy;
import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.cmr.search.ResultSetRow;
@@ -558,7 +560,8 @@ public class Search extends BaseScopableProcessorExtension implements Initializi
return (Scriptable)queryResultSet(search).get("nodes", getScope());
}
public Scriptable queryResultSet(Object search)
@SuppressWarnings("unchecked")
public Scriptable queryResultSet(Object search)
{
Object[] results = null;
Map<String,Object> meta = null;
@@ -755,6 +758,42 @@ public class Search extends BaseScopableProcessorExtension implements Initializi
}
}
Map<Serializable, Serializable> highlighting = (Map<Serializable, Serializable>)def.get("highlighting");
if (highlighting != null)
{
int snippetCount = this.getIntegerValue("snippetCount", 20, highlighting);
int fragmentSize = this.getIntegerValue("fragmentSize", 50, highlighting);
int maxAnalyzedChars = this.getIntegerValue("maxAnalyzedChars", 500, highlighting);
boolean usePhraseHighlighter = this.getBooleanValue("usePhraseHighlighter", true, highlighting);
boolean mergeContiguous = this.getBooleanValue("mergeContiguous", false, highlighting);
String prefix = (String) highlighting.get("prefix");
if (prefix == null)
{
prefix = "<mark>";
}
String postfix = (String) highlighting.get("postfix");
if (postfix == null)
{
postfix = "</mark>";
}
List<FieldHighlightParameters> fieldHighlightParameters = new ArrayList<FieldHighlightParameters>();
List<Map<Serializable, Serializable>> fields = (List<Map<Serializable, Serializable>>)def.get("fields");
for (Map<Serializable, Serializable> field: fields)
{
String propertyName = (String) field.get("field");
if (propertyName != null)
{
fieldHighlightParameters.add(new FieldHighlightParameters(propertyName, snippetCount, fragmentSize, mergeContiguous, prefix, postfix));
}
}
GeneralHighlightParameters ghp = new GeneralHighlightParameters(snippetCount, fragmentSize, mergeContiguous, prefix, postfix, maxAnalyzedChars, usePhraseHighlighter, fieldHighlightParameters);
sp.setHighlight(ghp);
}
// error handling opions
boolean exceptionOnError = true;
if (onerror != null)
@@ -806,6 +845,54 @@ public class Search extends BaseScopableProcessorExtension implements Initializi
return res;
}
/**
* Attempts to retrieve and parse an attribute in the supplied object to an integer. If the attribute cannot be
* found or cannot be parsed then the supplied default is returned.
*
* @param attribute
* @param defaultValue
* @param sourceObject
* @return
*/
public int getIntegerValue(String attribute, int defaultValue, Map<Serializable, Serializable> sourceObject)
{
int integer = defaultValue;
String stringValue = (String) sourceObject.get(attribute);
try
{
integer = Integer.parseInt(stringValue);
}
catch(NumberFormatException nfe)
{
// No action required
}
return integer;
}
/**
* Attempts to retrieve and parse an attribute in the supplied object to an integer. If the attribute cannot be
* found or cannot be parsed then the supplied default is returned.
*
* @param attribute
* @param defaultValue
* @param sourceObject
* @return
*/
public boolean getBooleanValue(String attribute, boolean defaultValue, Map<Serializable, Serializable> sourceObject)
{
boolean bool = defaultValue;
String stringValue = (String) sourceObject.get(attribute);
try
{
bool = Boolean.getBoolean(stringValue);
}
catch(NumberFormatException nfe)
{
// No action required
}
return bool;
}
/**
* Encode a string to ISO9075 - used to build valid paths for Lucene queries etc.
*
@@ -951,6 +1038,8 @@ public class Search extends BaseScopableProcessorExtension implements Initializi
// results metadata
meta.put("numberFound", results.getNumberFound());
meta.put("hasMore", results.hasMore());
meta.put("highlighting", results.getHighlighting());
// results facets
FacetLabelDisplayHandlerRegistry facetLabelDisplayHandlerRegistry = services.getFacetLabelDisplayHandlerRegistry();
Map<String, List<ScriptFacetResult>> facetMeta = new HashMap<>();