xsltParameters); query = (String) processingResult.getAttributes().get(AttributeNames.QUERY); documents = processingResult.getDocuments(); this.clusters = processingResult.getClusters();
/** * Loads a {@link ProcessingResult} from the provided {@link InputStream}, applying * XSLT transform if specified. The provided {@link InputStream} will be closed. */ public ProcessingResult loadProcessingResult(InputStream xml, Templates stylesheet, Map<String, String> xsltParameters) throws Exception { InputStream carrot2XmlStream = null; try { carrot2XmlStream = getCarrot2XmlStream(xml, stylesheet, xsltParameters); return ProcessingResult.deserialize(carrot2XmlStream); } finally { CloseableUtils.close(carrot2XmlStream, xml); } }
/** * Replace document refids with the actual references upon deserialization. */ private void documentIdToReference(Cluster cluster, Map<String, Document> documents) { if (cluster.documentIds != null) { for (Cluster.DocumentRefid documentRefid : cluster.documentIds) { cluster.addDocuments(documents.get(documentRefid.refid)); } } for (Cluster subcluster : cluster.getSubclusters()) { documentIdToReference(subcluster, documents); } }
tempAttributes.put(AttributeNames.DOCUMENTS, getDocuments()); tempAttributes.put(AttributeNames.CLUSTERS, getClusters());
{response.toXContent(builder, request);} if (processingResult != null && processingResult.getClusters() != null) { final Collection<Cluster> clusters = processingResult.getClusters(); final Map<String, Object> attributes = processingResult.getAttributes();
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); } }
redirectStrategy); final List<Document> documents = processingResult.getDocuments(); if (documents != null) final Map<String, Object> resultAttributes = processingResult.getAttributes(); response.metadata .put(SearchEngineResponse.RESULTS_TOTAL_KEY, resultAttributes
/** * Updates the statistics */ void update(ProcessingResult processingResult) { synchronized (this) { totalQueries++; if (processingResult != null) { goodQueries++; final Map<String, Object> attributes = processingResult .getAttributes(); addTimeToAverage(attributes, AttributeNames.PROCESSING_TIME_SOURCE, sourceTimeAverage); addTimeToAverage(attributes, AttributeNames.PROCESSING_TIME_ALGORITHM, algorithmTimeAverage); addTimeToAverage(attributes, AttributeNames.PROCESSING_TIME_TOTAL, totalTimeAverage); } } }
final Map<String, Object> attrs = prepareAttributesForSerialization( saveDocuments, saveClusters, saveOtherAttributes);
processingResult = new ProcessingResult(resultAttributes); } catch (IllegalArgumentException e) { throw new ProcessingException(e);
@Override protected void afterFetch(SearchEngineResponse response, ProcessingResult processingResult) { if (readClusters) { final Set<String> ids = Sets.newHashSet(); List<Document> documents = processingResult.getDocuments(); if (documents == null) documents = Collections.emptyList(); List<Cluster> clusters = processingResult.getClusters(); if (documents != null && clusters != null) { for (Document doc : documents) { ids.add(doc.getStringId()); } Predicate<Document> docFilter = new Predicate<Document>() { @Override public boolean apply(Document input) { return input != null && ids.contains(input.getStringId()); } }; this.clusters = sanityCheck(clusters, docFilter); } } }
@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); } }
/** * Serializes this {@link ProcessingResult} to a byte stream. Documents, clusters and * other attributes can be included or skipped in the output as requested. * <p> * This method is not thread-safe, external synchronization must be applied if needed. * </p> * * @param stream the stream to serialize this {@link ProcessingResult} to. The stream * will <strong>not</strong> be closed. * @param saveDocuments if <code>false</code>, documents will not be serialized. * Notice that when deserializing XML containing clusters but not * documents, document references in {@link Cluster#getDocuments()} will * not be restored. * @param saveClusters if <code>false</code>, clusters will not be serialized * @param saveOtherAttributes if <code>false</code>, other attributes will not be * serialized * @throws Exception in case of any problems with serialization */ public void serialize(OutputStream stream, boolean saveDocuments, boolean saveClusters, boolean saveOtherAttributes) throws Exception { final Map<String, Object> backupAttributes = attributes; attributes = prepareAttributesForSerialization(saveDocuments, saveClusters, saveOtherAttributes); new Persister().write(this, stream); attributes = backupAttributes; }
if (getDocuments() != null) documents = Lists.newArrayList(getDocuments()); if (getClusters() != null) clusters = Lists.newArrayList(getClusters());
/** * Transfers document and cluster lists to the attributes map after deserialization. */ @Commit private void afterDeserialization() throws Exception { if (otherAttributesForSerialization != null) { attributes = SimpleXmlWrappers.unwrap(otherAttributesForSerialization); } attributesView = Collections.unmodifiableMap(attributes); attributes.put(AttributeNames.QUERY, query != null ? query.trim() : null); attributes.put(AttributeNames.DOCUMENTS, documents); attributes.put(AttributeNames.CLUSTERS, clusters); // Convert document ids to the actual references if (clusters != null && documents != null) { final Map<String, Document> documentsById = Maps.newHashMap(); for (Document document : documents) { documentsById.put(document.getStringId(), document); } for (Cluster cluster : clusters) { documentIdToReference(cluster, documentsById); } } }