public Object cluster(Query query, DocList docList, SolrQueryRequest sreq) { try { // Prepare attributes for Carrot2 clustering call Map<String, Object> attributes = new HashMap<String, Object>(); List<Document> documents = getDocuments(docList, query, sreq); attributes.put(AttributeNames.DOCUMENTS, documents); attributes.put(AttributeNames.QUERY, query.toString()); // Pass extra overriding attributes from the request, if any extractCarrotAttributes(sreq.getParams(), attributes); // Perform clustering and convert to named list return clustersToNamedList(controller.process(attributes, clusteringAlgorithmClass).getClusters(), sreq.getParams()); } catch (Exception e) { log.error("Carrot2 clustering failed", e); throw new RuntimeException(e); } }
@Override public String init(NamedList config, final SolrCore core) { String result = super.init(config, core); SolrParams initParams = SolrParams.toSolrParams(config); // Initialize Carrot2 controller. Pass initialization attributes, if any. HashMap<String, Object> initAttributes = new HashMap<String, Object>(); extractCarrotAttributes(initParams, initAttributes); this.controller.init(initAttributes); this.idFieldName = core.getSchema().getUniqueKeyField().getName(); // Make sure the requested Carrot2 clustering algorithm class is available String carrotAlgorithmClassName = initParams.get(CarrotParams.ALGORITHM); Class<?> algorithmClass = core.getResourceLoader().findClass(carrotAlgorithmClassName); if (!IClusteringAlgorithm.class.isAssignableFrom(algorithmClass)) { throw new IllegalArgumentException("Class provided as " + CarrotParams.ALGORITHM + " must implement " + IClusteringAlgorithm.class.getName()); } this.clusteringAlgorithmClass = (Class<? extends IClusteringAlgorithm>) algorithmClass; return result; }
@Override public Object cluster(Query query, SolrDocumentList solrDocList, Map<SolrDocument, Integer> docIds, SolrQueryRequest sreq) { try { // Prepare attributes for Carrot2 clustering call Map<String, Object> attributes = new HashMap<>(); List<Document> documents = getDocuments(solrDocList, docIds, query, sreq); attributes.put(AttributeNames.DOCUMENTS, documents); attributes.put(AttributeNames.QUERY, query.toString()); // Pass the fields on which clustering runs. attributes.put("solrFieldNames", getFieldsForClustering(sreq)); // Pass extra overriding attributes from the request, if any extractCarrotAttributes(sreq.getParams(), attributes); // Perform clustering and convert to an output structure of clusters. // // Carrot2 uses current thread's context class loader to get // certain classes (e.g. custom tokenizer/stemmer) at runtime. // To make sure classes from contrib JARs are available, // we swap the context class loader for the time of clustering. return withContextClassLoader(core.getResourceLoader().getClassLoader(), () -> clustersToNamedList(controller.process(attributes, clusteringAlgorithmClass).getClusters(), sreq.getParams())); } catch (Exception e) { log.error("Carrot2 clustering failed", e); throw new SolrException(ErrorCode.SERVER_ERROR, "Carrot2 clustering failed", e); } }
extractCarrotAttributes(initParams, initAttributes);