diff --git a/source/java/org/alfresco/repo/jscript/Search.java b/source/java/org/alfresco/repo/jscript/Search.java index 16479a18c1..06279aa6cb 100644 --- a/source/java/org/alfresco/repo/jscript/Search.java +++ b/source/java/org/alfresco/repo/jscript/Search.java @@ -48,7 +48,9 @@ import org.alfresco.service.ServiceRegistry; 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.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 meta = null; @@ -585,7 +588,7 @@ public class Search extends BaseScopableProcessorExtension implements Initializi List> sort = (List>)def.get("sort"); Map page = (Map)def.get("page"); List facets = (List)def.get("fieldFacets"); - List filterQueries = (List)def.get("filterQueries"); + List filterQueries = (List)def.get("filterQueries"); String namespace = (String)def.get("namespace"); String onerror = (String)def.get("onerror"); String defaultField = (String)def.get("defaultField"); @@ -753,8 +756,44 @@ public class Search extends BaseScopableProcessorExtension implements Initializi { sp.addFilterQuery(filter); } - } - + } + + + Map highlighting = (Map)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 = ""; + } + String postfix = (String) highlighting.get("postfix"); + if (postfix == null) + { + postfix = ""; + } + + List fieldHighlightParameters = new ArrayList(); + List> fields = (List>)def.get("fields"); + for (Map 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) @@ -776,7 +815,7 @@ public class Search extends BaseScopableProcessorExtension implements Initializi // execute search based on search definition Pair> r = queryResultMeta(sp, exceptionOnError); 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("meta", res, meta); 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 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 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 meta.put("numberFound", results.getNumberFound()); - meta.put("hasMore", results.hasMore()); + meta.put("hasMore", results.hasMore()); + meta.put("highlighting", results.getHighlighting()); + // results facets FacetLabelDisplayHandlerRegistry facetLabelDisplayHandlerRegistry = services.getFacetLabelDisplayHandlerRegistry(); Map> facetMeta = new HashMap<>();