use of org.alfresco.repo.search.impl.solr.facet.handler.FacetLabelDisplayHandlerRegistry in project alfresco-repository by Alfresco.
the class Search method queryResultMeta.
/**
* Execute the query
*
* Removes any duplicates that may be present (ID search can cause duplicates -
* it is better to remove them here)
*
* @param sp SearchParameters describing the search to execute.
* @param exceptionOnError True to throw a runtime exception on error, false to return empty resultset
*
* @return Pair containing Object[] of Node objects, and the ResultSet metadata hash.
*/
protected Pair<Object[], Map<String, Object>> queryResultMeta(SearchParameters sp, boolean exceptionOnError) {
Collection<ScriptNode> set = null;
Map<String, Object> meta = new HashMap<>(8);
long time = 0L;
if (logger.isDebugEnabled()) {
logger.debug("query=" + sp.getQuery() + " limit=" + (sp.getLimitBy() != LimitBy.UNLIMITED ? sp.getLimit() : "none"));
time = System.currentTimeMillis();
}
// perform the search against the repo
ResultSet results = null;
try {
results = this.services.getSearchService().query(sp);
// results nodes
if (results.length() != 0) {
NodeService nodeService = this.services.getNodeService();
set = new LinkedHashSet<ScriptNode>(results.length(), 1.0f);
for (ResultSetRow row : results) {
NodeRef nodeRef = row.getNodeRef();
if (nodeService.exists(nodeRef)) {
set.add(new ScriptNode(nodeRef, this.services, getScope()));
}
}
}
// results metadata
meta.put("numberFound", results.getNumberFound());
meta.put("hasMore", results.hasMore());
Map<String, Map<String, List<String>>> highlightingMeta = new HashMap<>();
Map<NodeRef, List<Pair<String, List<String>>>> highlighting = results.getHighlighting();
for (Entry<NodeRef, List<Pair<String, List<String>>>> highlight : highlighting.entrySet()) {
NodeRef nodeRef = highlight.getKey();
Map<String, List<String>> scriptProperties = new HashMap<String, List<String>>();
List<Pair<String, List<String>>> highlights = highlight.getValue();
for (Pair<String, List<String>> propertyHighlight : highlights) {
String property = propertyHighlight.getFirst();
List<String> value = propertyHighlight.getSecond();
scriptProperties.put(property, value);
}
highlightingMeta.put(nodeRef.toString(), scriptProperties);
}
meta.put("highlighting", highlightingMeta);
// results facets
FacetLabelDisplayHandlerRegistry facetLabelDisplayHandlerRegistry = services.getFacetLabelDisplayHandlerRegistry();
Map<String, List<ScriptFacetResult>> facetMeta = new HashMap<>();
for (FieldFacet ff : sp.getFieldFacets()) {
// for each field facet, get the facet results
List<Pair<String, Integer>> fs = results.getFieldFacet(ff.getField());
List<ScriptFacetResult> facets = new ArrayList<>();
for (Pair<String, Integer> f : fs) {
// ignore zero hit fields
if (f.getSecond() > 0) {
String facetValue = f.getFirst();
FacetLabelDisplayHandler handler = facetLabelDisplayHandlerRegistry.getDisplayHandler(ff.getField());
String label = (handler == null) ? facetValue : handler.getDisplayLabel(facetValue).getLabel();
Integer labelIndex = (handler == null) ? -1 : handler.getDisplayLabel(facetValue).getLabelIndex();
facets.add(new ScriptFacetResult(facetValue, label, labelIndex, f.getSecond()));
}
}
// store facet results per field
facetMeta.put(ff.getField(), facets);
}
// with the results, otherwise the list remains empty.
for (String bucketedField : services.getSolrFacetHelper().getBucketedFieldFacets()) {
facetMeta.put(bucketedField, new ArrayList<ScriptFacetResult>());
}
Set<Entry<String, Integer>> facetQueries = results.getFacetQueries().entrySet();
for (Entry<String, Integer> entry : facetQueries) {
// ignore zero hit facet queries
if (entry.getValue() > 0) {
String key = entry.getKey();
// for example the key could be: {!afts}@{http://www.alfresco.org/model/content/1.0}created:[NOW/DAY-1DAY TO NOW/DAY+1DAY]
// qName => @{http://www.alfresco.org/model/content/1.0}created
// 7 => {!afts}
key = key.substring(7);
String qName = key.substring(0, key.lastIndexOf(':'));
// Retrieve the previous facet queries
List<ScriptFacetResult> fqs = facetMeta.get(qName);
if (fqs == null) {
fqs = new ArrayList<>();
logger.info("Field facet [" + key + "] has not been registered.");
}
// Get the handler for this qName
FacetLabelDisplayHandler handler = facetLabelDisplayHandlerRegistry.getDisplayHandler(qName);
FacetLabel facetLabel = (handler == null) ? new FacetLabel(key, key, -1) : handler.getDisplayLabel(key);
fqs.add(new ScriptFacetResult(facetLabel.getValue(), facetLabel.getLabel(), facetLabel.getLabelIndex(), entry.getValue()));
}
}
// End of bucketing
meta.put("facets", facetMeta);
SpellCheckResult spellCheckResult = results.getSpellCheckResult();
meta.put("spellcheck", new ScriptSpellCheckResult(sp.getSearchTerm(), spellCheckResult.getResultName(), spellCheckResult.isSearchedFor(), spellCheckResult.getResults(), spellCheckResult.isSpellCheckExist()));
} catch (Throwable err) {
if (exceptionOnError) {
throw new AlfrescoRuntimeException("Failed to execute search: " + sp.getQuery(), err);
} else {
if (logger.isDebugEnabled())
logger.debug("Failed to execute search: " + sp.getQuery(), err);
// put expected values to handle case where exception occurs in search
meta.put("numberFound", 0);
meta.put("hasMore", false);
}
} finally {
if (results != null) {
results.close();
}
if (logger.isDebugEnabled())
logger.debug("query time: " + (System.currentTimeMillis() - time) + "ms");
}
Object[] res = set != null ? set.toArray(new Object[(set.size())]) : new Object[0];
return new Pair<Object[], Map<String, Object>>(res, meta);
}
use of org.alfresco.repo.search.impl.solr.facet.handler.FacetLabelDisplayHandlerRegistry in project alfresco-repository by Alfresco.
the class ScriptSearchTest method mockServiceRegistry.
private ServiceRegistry mockServiceRegistry() {
SearchService searchService = mock(SearchService.class);
ResultSet results = mock(ResultSet.class);
List<Pair<String, Integer>> fieldFacets1 = new ArrayList<>();
fieldFacets1.add(new Pair<>(mimetype1, 1));
fieldFacets1.add(new Pair<>(mimetype2, 2));
List<Pair<String, Integer>> fieldFacets2 = new ArrayList<>();
fieldFacets2.add(new Pair<>(modifier, 1));
when(results.getFieldFacet(fieldFacet1)).thenReturn(fieldFacets1);
when(results.getFieldFacet(fieldFacet2)).thenReturn(fieldFacets2);
when(results.getFacetQueries()).thenReturn(new HashMap<>());
when(searchService.query((SearchParameters) any())).thenReturn(results);
FacetLabelDisplayHandlerRegistry displayHandlerRegistry = mock(FacetLabelDisplayHandlerRegistry.class);
ServiceRegistry services = mock(ServiceRegistry.class);
when(services.getSearchService()).thenReturn(searchService);
when(displayHandlerRegistry.getDisplayHandler(fieldFacet1)).thenReturn(new MimetypeOrderDisplayHandler());
when(displayHandlerRegistry.getDisplayHandler(fieldFacet2)).thenReturn(null);
SolrFacetHelper solrFacetHelper = mock(SolrFacetHelper.class);
when(solrFacetHelper.getBucketedFieldFacets()).thenReturn(new HashSet<>());
when(services.getSolrFacetHelper()).thenReturn(solrFacetHelper);
when(services.getFacetLabelDisplayHandlerRegistry()).thenReturn(displayHandlerRegistry);
return services;
}
Aggregations