Search in sources :

Example 1 with DocumentClusteringEngine

use of org.apache.solr.handler.clustering.DocumentClusteringEngine in project SearchServices by Alfresco.

the class AlfrescoSolrClusteringComponent method process.

@Override
public void process(ResponseBuilder rb) throws IOException {
    SolrParams params = rb.req.getParams();
    if (!params.getBool(COMPONENT_NAME, false)) {
        return;
    }
    final String name = getClusteringEngineName(rb);
    boolean useResults = params.getBool(ClusteringParams.USE_SEARCH_RESULTS, false);
    if (useResults == true) {
        SearchClusteringEngine engine = searchClusteringEngines.get(name);
        if (engine != null) {
            checkAvailable(name, engine);
            DocListAndSet results = rb.getResults();
            Map<SolrDocument, Integer> docIds = Maps.newHashMapWithExpectedSize(results.docList.size());
            SolrDocumentList solrDocList = docListToSolrDocumentList(results.docList, rb.req, docIds);
            Object clusters = engine.cluster(rb.getQuery(), solrDocList, docIds, rb.req);
            rb.rsp.add("clusters", clusters);
        } else {
            log.warn("No engine named: " + name);
        }
    }
    boolean useCollection = params.getBool(ClusteringParams.USE_COLLECTION, false);
    if (useCollection == true) {
        DocumentClusteringEngine engine = documentClusteringEngines.get(name);
        if (engine != null) {
            checkAvailable(name, engine);
            boolean useDocSet = params.getBool(ClusteringParams.USE_DOC_SET, false);
            NamedList<?> nl = null;
            // that runs in an executor
            if (useDocSet == true) {
                nl = engine.cluster(rb.getResults().docSet, params);
            } else {
                nl = engine.cluster(params);
            }
            rb.rsp.add("clusters", nl);
        } else {
            log.warn("No engine named: " + name);
        }
    }
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) DocListAndSet(org.apache.solr.search.DocListAndSet) SolrParams(org.apache.solr.common.params.SolrParams) SearchClusteringEngine(org.apache.solr.handler.clustering.SearchClusteringEngine) SolrDocumentList(org.apache.solr.common.SolrDocumentList) DocumentClusteringEngine(org.apache.solr.handler.clustering.DocumentClusteringEngine)

Example 2 with DocumentClusteringEngine

use of org.apache.solr.handler.clustering.DocumentClusteringEngine in project SearchServices by Alfresco.

the class AlfrescoSolrClusteringComponent method inform.

@SuppressWarnings("unchecked")
@Override
public void inform(SolrCore core) {
    if (initParams != null) {
        log.info("Initializing Clustering Engines");
        // Our target list of engines, split into search-results and
        // document clustering.
        SolrResourceLoader loader = core.getResourceLoader();
        for (Map.Entry<String, Object> entry : initParams) {
            if ("engine".equals(entry.getKey())) {
                NamedList<Object> engineInitParams = (NamedList<Object>) entry.getValue();
                Boolean optional = engineInitParams.getBooleanArg("optional");
                optional = (optional == null ? Boolean.FALSE : optional);
                String engineClassName = StringUtils.defaultIfBlank((String) engineInitParams.get("classname"), CarrotClusteringEngine.class.getName());
                // Instantiate the clustering engine and split to
                // appropriate map.
                final ClusteringEngine engine = loader.newInstance(engineClassName, ClusteringEngine.class);
                final String name = StringUtils.defaultIfBlank(engine.init(engineInitParams, core), "");
                if (!engine.isAvailable()) {
                    if (optional) {
                        log.info("Optional clustering engine not available: " + name);
                    } else {
                        throw new SolrException(ErrorCode.SERVER_ERROR, "A required clustering engine failed to initialize, check the logs: " + name);
                    }
                }
                final ClusteringEngine previousEntry;
                if (engine instanceof SearchClusteringEngine) {
                    previousEntry = searchClusteringEngines.put(name, (SearchClusteringEngine) engine);
                } else if (engine instanceof DocumentClusteringEngine) {
                    previousEntry = documentClusteringEngines.put(name, (DocumentClusteringEngine) engine);
                } else {
                    log.warn("Unknown type of a clustering engine for class: " + engineClassName);
                    continue;
                }
                if (previousEntry != null) {
                    log.warn("Duplicate clustering engine component named '" + name + "'.");
                }
            }
        }
        // Set up the default engine key for both types of engines.
        setupDefaultEngine("search results clustering", searchClusteringEngines);
        setupDefaultEngine("document clustering", documentClusteringEngines);
        log.info("Finished Initializing Clustering Engines");
    }
}
Also used : NamedList(org.apache.solr.common.util.NamedList) DocumentClusteringEngine(org.apache.solr.handler.clustering.DocumentClusteringEngine) SolrResourceLoader(org.apache.solr.core.SolrResourceLoader) CarrotClusteringEngine(org.apache.solr.handler.clustering.carrot2.CarrotClusteringEngine) SearchClusteringEngine(org.apache.solr.handler.clustering.SearchClusteringEngine) CarrotClusteringEngine(org.apache.solr.handler.clustering.carrot2.CarrotClusteringEngine) ClusteringEngine(org.apache.solr.handler.clustering.ClusteringEngine) SearchClusteringEngine(org.apache.solr.handler.clustering.SearchClusteringEngine) DocumentClusteringEngine(org.apache.solr.handler.clustering.DocumentClusteringEngine) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) SolrException(org.apache.solr.common.SolrException)

Aggregations

DocumentClusteringEngine (org.apache.solr.handler.clustering.DocumentClusteringEngine)2 SearchClusteringEngine (org.apache.solr.handler.clustering.SearchClusteringEngine)2 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 SolrDocument (org.apache.solr.common.SolrDocument)1 SolrDocumentList (org.apache.solr.common.SolrDocumentList)1 SolrException (org.apache.solr.common.SolrException)1 SolrParams (org.apache.solr.common.params.SolrParams)1 NamedList (org.apache.solr.common.util.NamedList)1 SolrResourceLoader (org.apache.solr.core.SolrResourceLoader)1 ClusteringEngine (org.apache.solr.handler.clustering.ClusteringEngine)1 CarrotClusteringEngine (org.apache.solr.handler.clustering.carrot2.CarrotClusteringEngine)1 DocListAndSet (org.apache.solr.search.DocListAndSet)1