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);
}
}
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;
}
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;
}
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.);
}
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()]);
}
Aggregations