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

@@ -48,7 +48,9 @@ import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.ContentReader; import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService; import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef; 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.LimitBy;
import org.alfresco.service.cmr.search.ResultSet; import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.cmr.search.ResultSetRow; 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()); return (Scriptable)queryResultSet(search).get("nodes", getScope());
} }
public Scriptable queryResultSet(Object search) @SuppressWarnings("unchecked")
public Scriptable queryResultSet(Object search)
{ {
Object[] results = null; Object[] results = null;
Map<String,Object> meta = null; Map<String,Object> meta = null;
@@ -585,7 +588,7 @@ public class Search extends BaseScopableProcessorExtension implements Initializi
List<Map<Serializable, Serializable>> sort = (List<Map<Serializable, Serializable>>)def.get("sort"); List<Map<Serializable, Serializable>> sort = (List<Map<Serializable, Serializable>>)def.get("sort");
Map<Serializable, Serializable> page = (Map<Serializable, Serializable>)def.get("page"); Map<Serializable, Serializable> page = (Map<Serializable, Serializable>)def.get("page");
List<String> facets = (List<String>)def.get("fieldFacets"); List<String> facets = (List<String>)def.get("fieldFacets");
List<String> filterQueries = (List<String>)def.get("filterQueries"); List<String> filterQueries = (List<String>)def.get("filterQueries");
String namespace = (String)def.get("namespace"); String namespace = (String)def.get("namespace");
String onerror = (String)def.get("onerror"); String onerror = (String)def.get("onerror");
String defaultField = (String)def.get("defaultField"); String defaultField = (String)def.get("defaultField");
@@ -753,8 +756,44 @@ public class Search extends BaseScopableProcessorExtension implements Initializi
{ {
sp.addFilterQuery(filter); sp.addFilterQuery(filter);
} }
} }
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 // error handling opions
boolean exceptionOnError = true; boolean exceptionOnError = true;
if (onerror != null) if (onerror != null)
@@ -776,7 +815,7 @@ public class Search extends BaseScopableProcessorExtension implements Initializi
// execute search based on search definition // execute search based on search definition
Pair<Object[], Map<String,Object>> r = queryResultMeta(sp, exceptionOnError); Pair<Object[], Map<String,Object>> r = queryResultMeta(sp, exceptionOnError);
results = r.getFirst(); results = r.getFirst();
meta = r.getSecond(); meta = r.getSecond();
} }
} }
@@ -804,6 +843,54 @@ public class Search extends BaseScopableProcessorExtension implements Initializi
res.put("nodes", res, Context.getCurrentContext().newArray(scope, results)); res.put("nodes", res, Context.getCurrentContext().newArray(scope, results));
res.put("meta", res, meta); res.put("meta", res, meta);
return res; 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;
} }
/** /**
@@ -950,7 +1037,9 @@ public class Search extends BaseScopableProcessorExtension implements Initializi
} }
// results metadata // results metadata
meta.put("numberFound", results.getNumberFound()); meta.put("numberFound", results.getNumberFound());
meta.put("hasMore", results.hasMore()); meta.put("hasMore", results.hasMore());
meta.put("highlighting", results.getHighlighting());
// results facets // results facets
FacetLabelDisplayHandlerRegistry facetLabelDisplayHandlerRegistry = services.getFacetLabelDisplayHandlerRegistry(); FacetLabelDisplayHandlerRegistry facetLabelDisplayHandlerRegistry = services.getFacetLabelDisplayHandlerRegistry();
Map<String, List<ScriptFacetResult>> facetMeta = new HashMap<>(); Map<String, List<ScriptFacetResult>> facetMeta = new HashMap<>();