Search in sources :

Example 36 with SolrServer

use of org.apache.solr.client.solrj.SolrServer in project stanbol by apache.

the class TopicClassificationEngine method removeConcept.

@Override
public void removeConcept(String conceptId) throws ClassifierException {
    if (conceptId == null || conceptId.isEmpty()) {
        throw new ClassifierException("conceptId must not be null or empty");
    }
    SolrServer solrServer = getActiveSolrServer();
    try {
        solrServer.deleteByQuery(conceptUriField + ":" + ClientUtils.escapeQueryChars(conceptId));
        solrServer.commit();
    } catch (Exception e) {
        String msg = String.format("Error removing concept '%s' on Solr Core '%s'", conceptId, solrCoreId);
        throw new ClassifierException(msg, e);
    }
}
Also used : EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) SolrServer(org.apache.solr.client.solrj.SolrServer) ManagedSolrServer(org.apache.stanbol.commons.solr.managed.ManagedSolrServer) EngineException(org.apache.stanbol.enhancer.servicesapi.EngineException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ConfigurationException(org.osgi.service.cm.ConfigurationException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) TrainingSetException(org.apache.stanbol.enhancer.topic.api.training.TrainingSetException) ClassifierException(org.apache.stanbol.enhancer.topic.api.ClassifierException) InvalidContentException(org.apache.stanbol.enhancer.servicesapi.InvalidContentException) EntityhubException(org.apache.stanbol.entityhub.servicesapi.EntityhubException) ChainException(org.apache.stanbol.enhancer.servicesapi.ChainException) IOException(java.io.IOException) ClassifierException(org.apache.stanbol.enhancer.topic.api.ClassifierException)

Example 37 with SolrServer

use of org.apache.solr.client.solrj.SolrServer in project stanbol by apache.

the class TopicClassificationEngine method getNarrowerConcepts.

@Override
public Set<String> getNarrowerConcepts(String broadTopicId) throws ClassifierException {
    LinkedHashSet<String> narrowerConcepts = new LinkedHashSet<String>();
    if (broaderField == null) {
        return narrowerConcepts;
    }
    SolrServer solrServer = getActiveSolrServer();
    SolrQuery query = new SolrQuery("*:*");
    query.addFilterQuery(entryTypeField + ":" + METADATA_ENTRY);
    query.addFilterQuery(broaderField + ":" + ClientUtils.escapeQueryChars(broadTopicId));
    query.addField(conceptUriField);
    query.addSortField(conceptUriField, SolrQuery.ORDER.asc);
    try {
        for (SolrDocument result : solrServer.query(query).getResults()) {
            narrowerConcepts.add(result.getFirstValue(conceptUriField).toString());
        }
    } catch (SolrServerException e) {
        String msg = String.format("Error while fetching narrower topics of '%s' on Solr Core '%s'.", broadTopicId, solrCoreId);
        throw new ClassifierException(msg, e);
    }
    return narrowerConcepts;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SolrDocument(org.apache.solr.common.SolrDocument) SolrServerException(org.apache.solr.client.solrj.SolrServerException) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) SolrServer(org.apache.solr.client.solrj.SolrServer) ManagedSolrServer(org.apache.stanbol.commons.solr.managed.ManagedSolrServer) SolrQuery(org.apache.solr.client.solrj.SolrQuery) ClassifierException(org.apache.stanbol.enhancer.topic.api.ClassifierException)

Example 38 with SolrServer

use of org.apache.solr.client.solrj.SolrServer in project stanbol by apache.

the class TopicClassificationEngine method getRootConcepts.

@Override
public Set<String> getRootConcepts() throws ClassifierException {
    LinkedHashSet<String> rootConcepts = new LinkedHashSet<String>();
    SolrServer solrServer = getActiveSolrServer();
    SolrQuery query = new SolrQuery("*:*");
    // TODO: this can be very big on flat thesauri: should we enable a paging API instead?
    query.setRows(MAX_ROOTS);
    query.setFields(conceptUriField);
    query.setSortField(conceptUriField, SolrQuery.ORDER.asc);
    query.addFilterQuery(entryTypeField + ":" + METADATA_ENTRY);
    if (broaderField != null) {
        // find any topic with an empty broaderField
        query.addFilterQuery(" -" + broaderField + ":" + SOLR_NON_EMPTY_FIELD);
    }
    try {
        QueryResponse response = solrServer.query(query);
        if (response.getResults().size() >= MAX_ROOTS) {
            log.warn(String.format("TopicClassifier '%s' has more than %d registered topic roots." + " Some roots might be ignored.", engineName, MAX_ROOTS));
        }
        for (SolrDocument result : response.getResults()) {
            rootConcepts.add(result.getFirstValue(conceptUriField).toString());
        }
    } catch (SolrServerException e) {
        String msg = String.format("Error while fetching root topics on Solr Core '%s'.", solrCoreId);
        throw new ClassifierException(msg, e);
    }
    return rootConcepts;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrServerException(org.apache.solr.client.solrj.SolrServerException) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) SolrServer(org.apache.solr.client.solrj.SolrServer) ManagedSolrServer(org.apache.stanbol.commons.solr.managed.ManagedSolrServer) SolrQuery(org.apache.solr.client.solrj.SolrQuery) ClassifierException(org.apache.stanbol.enhancer.topic.api.ClassifierException)

Example 39 with SolrServer

use of org.apache.solr.client.solrj.SolrServer in project stanbol by apache.

the class TopicClassificationEngine method updateTopic.

/**
     * @param conceptUri
     *            the topic model to update
     * @param metadataEntryId
     *            of the metadata entry id of the topic
     * @param modelEntryId
     *            of the model entry id of the topic
     * @param impactedTopics
     *            the list of impacted topics (e.g. the topic node and direct children)
     * @param primaryTopicUri
     * @param broaderConcepts
     *            the collection of broader to re-add in the broader field
     */
protected void updateTopic(String conceptUri, String metadataId, String modelId, List<String> impactedTopics, String primaryTopicUri, Collection<Object> broaderConcepts) throws TrainingSetException, ClassifierException {
    long start = System.currentTimeMillis();
    Batch<Example> examples = Batch.emtpyBatch(Example.class);
    StringBuffer sb = new StringBuffer();
    int offset = 0;
    do {
        examples = getTrainingSet().getPositiveExamples(impactedTopics, examples.nextOffset);
        for (Example example : examples.items) {
            if ((cvFoldCount != 0) && (offset % cvFoldCount == cvFoldIndex)) {
                // we are performing a cross validation session and this example belong to the test
                // fold hence should be skipped
                offset++;
                continue;
            }
            offset++;
            sb.append(StringUtils.join(example.contents, "\n\n"));
            sb.append("\n\n");
        }
    } while (sb.length() < MAX_CHARS_PER_TOPIC && examples.hasMore);
    // reindex the topic with the new text data collected from the examples
    SolrInputDocument modelEntry = new SolrInputDocument();
    modelEntry.addField(entryIdField, modelId);
    modelEntry.addField(conceptUriField, conceptUri);
    modelEntry.addField(entryTypeField, MODEL_ENTRY);
    if (sb.length() > 0) {
        modelEntry.addField(similarityField, sb);
    }
    // update the metadata of the topic model
    SolrInputDocument metadataEntry = new SolrInputDocument();
    metadataEntry.addField(entryIdField, metadataId);
    metadataEntry.addField(modelEntryIdField, modelId);
    metadataEntry.addField(entryTypeField, METADATA_ENTRY);
    metadataEntry.addField(conceptUriField, conceptUri);
    if (primaryTopicUriField != null) {
        metadataEntry.addField(primaryTopicUriField, primaryTopicUri);
    }
    if (broaderConcepts != null && broaderField != null) {
        metadataEntry.addField(broaderField, broaderConcepts);
    }
    if (modelUpdateDateField != null) {
        metadataEntry.addField(modelUpdateDateField, UTCTimeStamper.nowUtcDate());
    }
    SolrServer solrServer = getActiveSolrServer();
    try {
        UpdateRequest request = new UpdateRequest();
        request.add(metadataEntry);
        request.add(modelEntry);
        solrServer.request(request);
    // the commit is done by the caller in batch
    } catch (Exception e) {
        String msg = String.format("Error updating topic with id '%s' on Solr Core '%s'", conceptUri, solrCoreId);
        throw new ClassifierException(msg, e);
    }
    long stop = System.currentTimeMillis();
    log.debug("Sucessfully updated topic {} in {}s", conceptUri, (double) (stop - start) / 1000.);
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) Example(org.apache.stanbol.enhancer.topic.api.training.Example) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) SolrServer(org.apache.solr.client.solrj.SolrServer) ManagedSolrServer(org.apache.stanbol.commons.solr.managed.ManagedSolrServer) EngineException(org.apache.stanbol.enhancer.servicesapi.EngineException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ConfigurationException(org.osgi.service.cm.ConfigurationException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) TrainingSetException(org.apache.stanbol.enhancer.topic.api.training.TrainingSetException) ClassifierException(org.apache.stanbol.enhancer.topic.api.ClassifierException) InvalidContentException(org.apache.stanbol.enhancer.servicesapi.InvalidContentException) EntityhubException(org.apache.stanbol.entityhub.servicesapi.EntityhubException) ChainException(org.apache.stanbol.enhancer.servicesapi.ChainException) IOException(java.io.IOException) ClassifierException(org.apache.stanbol.enhancer.topic.api.ClassifierException)

Example 40 with SolrServer

use of org.apache.solr.client.solrj.SolrServer in project stanbol by apache.

the class RegisteredSolrServerTracker method getServices.

/**
     * Overrides to provide a array of {@link SolrServer} that is sorted by
     * {@link Constants#SERVICE_RANKING}.
     * @see ServiceTracker#getServices()
     */
@Override
public SolrServer[] getServices() {
    ServiceReference[] refs = getServiceReferences();
    Collection<SolrServer> servers = new ArrayList<SolrServer>(refs.length);
    if (refs != null) {
        for (ServiceReference ref : refs) {
            SolrServer server = getService(ref);
            if (server != null) {
                servers.add(server);
            }
        //else null ... ignore
        }
    }
    return servers.isEmpty() ? null : servers.toArray(new SolrServer[servers.size()]);
}
Also used : ArrayList(java.util.ArrayList) SolrServer(org.apache.solr.client.solrj.SolrServer) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

SolrServer (org.apache.solr.client.solrj.SolrServer)42 ManagedSolrServer (org.apache.stanbol.commons.solr.managed.ManagedSolrServer)21 SolrServerException (org.apache.solr.client.solrj.SolrServerException)20 EmbeddedSolrServer (org.apache.solr.client.solrj.embedded.EmbeddedSolrServer)17 IOException (java.io.IOException)13 ClassifierException (org.apache.stanbol.enhancer.topic.api.ClassifierException)13 SolrQuery (org.apache.solr.client.solrj.SolrQuery)12 SolrDocument (org.apache.solr.common.SolrDocument)11 ConfigurationException (org.osgi.service.cm.ConfigurationException)11 TrainingSetException (org.apache.stanbol.enhancer.topic.api.training.TrainingSetException)10 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)10 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)9 OakSolrConfiguration (org.apache.jackrabbit.oak.plugins.index.solr.configuration.OakSolrConfiguration)8 ArrayList (java.util.ArrayList)7 ChainException (org.apache.stanbol.enhancer.servicesapi.ChainException)7 EngineException (org.apache.stanbol.enhancer.servicesapi.EngineException)7 InvalidContentException (org.apache.stanbol.enhancer.servicesapi.InvalidContentException)7 EntityhubException (org.apache.stanbol.entityhub.servicesapi.EntityhubException)7 SolrInputDocument (org.apache.solr.common.SolrInputDocument)6 Test (org.junit.Test)6