You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@uima.apache.org by Ar...@bka.bund.de on 2014/11/27 14:09:33 UTC

A simple CAS Consumer for populating a Solr Index

Hi Rob!

This simple code example sends annotations of type Person, Location and Organization to a Solr server. There must be the fields text, person, location, and organization defined in Solr, as well. You need org.apache.solr:solr-solrj:4.9.0  or higher jar.

Regards,
Armin


public class SolrPopulator extends CasConsumer_ImplBase {
	private Logger mLogger;

	/**
	 * A Solr server.
	 */
	private SolrServer mSolrServer;

	public static final String PARAMETER_SOLR_SERVER_URL = "solrServerUrl";
	@ConfigurationParameter(name = PARAMETER_SOLR_SERVER_URL, mandatory = true)
	private URL mSolrServerUrl;

	@Override
	public void initialize(final UimaContext context) throws ResourceInitializationException {
		super.initialize(context);
		mLogger = context.getLogger();

		mSolrServer = new HttpSolrServer(mSolrServerUrl.toString());
	}

	@Override
	public void process(final CAS cas) throws AnalysisEngineProcessException {
		final SolrInputDocument document = new SolrInputDocument();
		document.addField("text", cas.getDocumentText());

		try {
			for (final Person person : JCasUtil.select(cas.getJCas(), Person.class)) {
				document.addField("person", person.getCoveredText());
			}
			for (final Organization organization : JCasUtil.select(cas.getJCas(), Organization.class)) {
				document.addField("organization", organization.getCoveredText());
			}
			for (final Location location : JCasUtil.select(cas.getJCas(), Location.class)) {
				document.addField("location", location.getCoveredText());
			}
		} catch (final CASException cause) {
			throw new AnalysisEngineProcessException(cause);
		}

		try {
			mSolrServer.add(document);
		} catch (final IOException | SolrServerException cause) {
			throw new AnalysisEngineProcessException(cause);
		}
	}

	@Override
	public void collectionProcessComplete() throws AnalysisEngineProcessException {
		super.collectionProcessComplete();
		try {
			mSolrServer.commit();
		} catch (final IOException | SolrServerException cause) {
			throw new AnalysisEngineProcessException(cause);
		}
	}
}