Search in sources :

Example 1 with FacetLabel

use of org.alfresco.repo.search.impl.solr.facet.handler.FacetLabel 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);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FacetLabelDisplayHandlerRegistry(org.alfresco.repo.search.impl.solr.facet.handler.FacetLabelDisplayHandlerRegistry) NodeRef(org.alfresco.service.cmr.repository.NodeRef) Entry(java.util.Map.Entry) ResultSet(org.alfresco.service.cmr.search.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) FacetLabelDisplayHandler(org.alfresco.repo.search.impl.solr.facet.handler.FacetLabelDisplayHandler) Pair(org.alfresco.util.Pair) FacetLabel(org.alfresco.repo.search.impl.solr.facet.handler.FacetLabel) NodeService(org.alfresco.service.cmr.repository.NodeService) SpellCheckResult(org.alfresco.service.cmr.search.SpellCheckResult) ResultSetRow(org.alfresco.service.cmr.search.ResultSetRow) FieldFacet(org.alfresco.service.cmr.search.SearchParameters.FieldFacet) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with FacetLabel

use of org.alfresco.repo.search.impl.solr.facet.handler.FacetLabel in project alfresco-repository by Alfresco.

the class SolrFacetQueriesDisplayHandlersTest method testGetMimetypeDisplayHandler.

/**
 * MimeType display handler test.
 *
 * @throws Exception
 */
@Test
public void testGetMimetypeDisplayHandler() throws Exception {
    // Mimetype handler
    FacetLabelDisplayHandler mimeTypeHandler = displayHandlerRegistry.getDisplayHandler("@{http://www.alfresco.org/model/content/1.0}content.mimetype");
    assertNotNull(mimeTypeHandler);
    FacetLabel mimetype = mimeTypeHandler.getDisplayLabel("someMimetype123");
    assertNotNull(mimetype);
    assertEquals("someMimetype123 is not a registered mimetype, hence, the handler should return the passed-in mimetype.", "someMimetype123", mimetype.getLabel());
    mimetype = mimeTypeHandler.getDisplayLabel("text/plain");
    assertNotNull(mimetype);
    assertEquals("Expected [text/plain] display name.", "Plain Text", mimetype.getLabel());
}
Also used : FacetLabel(org.alfresco.repo.search.impl.solr.facet.handler.FacetLabel) FacetLabelDisplayHandler(org.alfresco.repo.search.impl.solr.facet.handler.FacetLabelDisplayHandler)

Example 3 with FacetLabel

use of org.alfresco.repo.search.impl.solr.facet.handler.FacetLabel in project alfresco-repository by Alfresco.

the class SolrFacetQueriesDisplayHandlersTest method testGetCreatedDateBucketsDisplayHandler.

/**
 * Created date buckets display handler test.
 *
 * @throws Exception
 */
@Test
public void testGetCreatedDateBucketsDisplayHandler() throws Exception {
    final String createdDateField = "@{http://www.alfresco.org/model/content/1.0}created";
    FacetLabelDisplayHandler dateBucketeHandler = displayHandlerRegistry.getDisplayHandler(createdDateField);
    assertNotNull(dateBucketeHandler);
    FacetLabel dateLabel = dateBucketeHandler.getDisplayLabel(createdDateField + ":[NOW/DAY-1DAY TO NOW/DAY+1DAY]");
    assertNotNull(dateLabel);
    assertEquals("faceted-search.date.one-day.label", dateLabel.getLabel());
    assertEquals("Yesterday date bucket should have a sorting index of 0.", 0, dateLabel.getLabelIndex());
    dateLabel = dateBucketeHandler.getDisplayLabel(createdDateField + ":[NOW/DAY-7DAYS TO NOW/DAY+1DAY]");
    assertNotNull(dateLabel);
    assertEquals("faceted-search.date.one-week.label", dateLabel.getLabel());
    assertEquals("Last week date bucket should have a sorting index of 1.", 1, dateLabel.getLabelIndex());
    dateLabel = dateBucketeHandler.getDisplayLabel(createdDateField + ":[NOW/DAY-1MONTH TO NOW/DAY+1DAY]");
    assertNotNull(dateLabel);
    assertEquals("faceted-search.date.one-month.label", dateLabel.getLabel());
    assertEquals("Last month date bucket should have a sorting index of 2.", 2, dateLabel.getLabelIndex());
    dateLabel = dateBucketeHandler.getDisplayLabel(createdDateField + ":[NOW/DAY-6MONTHS TO NOW/DAY+1DAY]");
    assertNotNull(dateLabel);
    assertEquals("faceted-search.date.six-months.label", dateLabel.getLabel());
    assertEquals("Last 6 months date bucket should have a sorting index of 3.", 3, dateLabel.getLabelIndex());
    dateLabel = dateBucketeHandler.getDisplayLabel(createdDateField + ":[NOW/DAY-1YEAR TO NOW/DAY+1DAY]");
    assertNotNull(dateLabel);
    assertEquals("faceted-search.date.one-year.label", dateLabel.getLabel());
    assertEquals("Last year date bucket should have a sorting index of 4.", 4, dateLabel.getLabelIndex());
}
Also used : FacetLabel(org.alfresco.repo.search.impl.solr.facet.handler.FacetLabel) FacetLabelDisplayHandler(org.alfresco.repo.search.impl.solr.facet.handler.FacetLabelDisplayHandler)

Example 4 with FacetLabel

use of org.alfresco.repo.search.impl.solr.facet.handler.FacetLabel in project alfresco-repository by Alfresco.

the class SolrFacetQueriesDisplayHandlersTest method testGetContentSizeBucketsDisplayHandler.

/**
 * Content size buckets display handler test.
 *
 * @throws Exception
 */
@Test
public void testGetContentSizeBucketsDisplayHandler() throws Exception {
    final String contentSizeField = "@{http://www.alfresco.org/model/content/1.0}content.size";
    FacetLabelDisplayHandler contentSizeBucketeHandler = displayHandlerRegistry.getDisplayHandler(contentSizeField);
    assertNotNull(contentSizeBucketeHandler);
    int KB = 1024;
    int MB = KB * 1024;
    int tiny = 10 * KB;
    int small = 100 * KB;
    int medium = MB;
    int large = 16 * MB;
    int huge = 128 * MB;
    FacetLabel sizeLabel = contentSizeBucketeHandler.getDisplayLabel(contentSizeField + ":[0 TO " + tiny + "]");
    assertNotNull(sizeLabel);
    assertEquals("faceted-search.size.0-10KB.label", sizeLabel.getLabel());
    assertEquals("0-10KB size bucket should have a sorting index of 0.", 0, sizeLabel.getLabelIndex());
    sizeLabel = contentSizeBucketeHandler.getDisplayLabel(contentSizeField + ":[" + tiny + " TO " + small + "]");
    assertNotNull(sizeLabel);
    assertEquals("faceted-search.size.10-100KB.label", sizeLabel.getLabel());
    assertEquals("10-100KB size bucket should have a sorting index of 1.", 1, sizeLabel.getLabelIndex());
    sizeLabel = contentSizeBucketeHandler.getDisplayLabel(contentSizeField + ":[" + small + " TO " + medium + "]");
    assertNotNull(sizeLabel);
    assertEquals("faceted-search.size.100KB-1MB.label", sizeLabel.getLabel());
    assertEquals("100KB-1MB size bucket should have a sorting index of 2.", 2, sizeLabel.getLabelIndex());
    sizeLabel = contentSizeBucketeHandler.getDisplayLabel(contentSizeField + ":[" + medium + " TO " + large + "]");
    assertNotNull(sizeLabel);
    assertEquals("faceted-search.size.1-16MB.label", sizeLabel.getLabel());
    assertEquals("1-16MB size bucket should have a sorting index of 3.", 3, sizeLabel.getLabelIndex());
    sizeLabel = contentSizeBucketeHandler.getDisplayLabel(contentSizeField + ":[" + large + " TO " + huge + "]");
    assertNotNull(sizeLabel);
    assertEquals("faceted-search.size.16-128MB.label", sizeLabel.getLabel());
    assertEquals("16-128MB size bucket should have a sorting index of 4.", 4, sizeLabel.getLabelIndex());
    sizeLabel = contentSizeBucketeHandler.getDisplayLabel(contentSizeField + ":[" + huge + " TO MAX]");
    assertNotNull(sizeLabel);
    assertEquals("faceted-search.size.over128.label", sizeLabel.getLabel());
    assertEquals("over128MB size bucket should have a sorting index of 5.", 5, sizeLabel.getLabelIndex());
}
Also used : FacetLabel(org.alfresco.repo.search.impl.solr.facet.handler.FacetLabel) FacetLabelDisplayHandler(org.alfresco.repo.search.impl.solr.facet.handler.FacetLabelDisplayHandler)

Example 5 with FacetLabel

use of org.alfresco.repo.search.impl.solr.facet.handler.FacetLabel in project alfresco-repository by Alfresco.

the class SolrFacetQueriesDisplayHandlersTest method testGetSiteTitleDisplayHandler.

/**
 * Site title display handler test.
 *
 * @throws Exception
 */
@Test
public void testGetSiteTitleDisplayHandler() throws Exception {
    final String siteShortName = "siteDisplayHandlerTest" + System.currentTimeMillis();
    final String siteTitle = "Site Title Display Handler Test";
    temporarySites.createSite("sitePreset", siteShortName, siteTitle, "site desc", SiteVisibility.PRIVATE, AuthenticationUtil.getAdminUserName());
    FacetLabelDisplayHandler siteHandler = displayHandlerRegistry.getDisplayHandler("SITE");
    assertNotNull(siteHandler);
    String randomSiteName = "randomSiteName" + System.currentTimeMillis();
    FacetLabel name = siteHandler.getDisplayLabel(randomSiteName);
    assertNotNull(name);
    assertEquals("There is no site with the name [" + randomSiteName + "], hence, the handler should return the passed-in short name.", randomSiteName, name.getLabel());
    name = siteHandler.getDisplayLabel(siteShortName);
    assertNotNull(name);
    assertEquals(siteTitle, name.getLabel());
}
Also used : FacetLabel(org.alfresco.repo.search.impl.solr.facet.handler.FacetLabel) FacetLabelDisplayHandler(org.alfresco.repo.search.impl.solr.facet.handler.FacetLabelDisplayHandler)

Aggregations

FacetLabel (org.alfresco.repo.search.impl.solr.facet.handler.FacetLabel)6 FacetLabelDisplayHandler (org.alfresco.repo.search.impl.solr.facet.handler.FacetLabelDisplayHandler)6 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 FacetLabelDisplayHandlerRegistry (org.alfresco.repo.search.impl.solr.facet.handler.FacetLabelDisplayHandlerRegistry)1 NodeRef (org.alfresco.service.cmr.repository.NodeRef)1 NodeService (org.alfresco.service.cmr.repository.NodeService)1 ResultSet (org.alfresco.service.cmr.search.ResultSet)1 ResultSetRow (org.alfresco.service.cmr.search.ResultSetRow)1 FieldFacet (org.alfresco.service.cmr.search.SearchParameters.FieldFacet)1 SpellCheckResult (org.alfresco.service.cmr.search.SpellCheckResult)1 Pair (org.alfresco.util.Pair)1