Search in sources :

Example 21 with SolrServer

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

the class LDPathTestBase method setup.

@BeforeClass
public static void setup() throws Exception {
    // get the working directory
    // use property substitution to test this feature!
    String prefix = System.getProperty("basedir") == null ? "." : "${basedir}";
    String solrServerDir = prefix + TEST_INDEX_REL_PATH;
    log.info("Test Solr Server Directory: {}", solrServerDir);
    System.setProperty(ManagedSolrServer.MANAGED_SOLR_DIR_PROPERTY, solrServerDir);
    SolrYardConfig config = new SolrYardConfig(TEST_YARD_ID, TEST_SOLR_CORE_NAME);
    config.setAllowInitialisation(false);
    //the dbpedia default data
    config.setIndexConfigurationName(TEST_SOLR_CORE_CONFIGURATION);
    //init from datafile provider
    config.setAllowInitialisation(true);
    config.setName("DBpedia.org default data");
    config.setDescription("Data used for the LDPath setup");
    // create the Yard used for the tests
    IndexReference solrIndexRef = IndexReference.parse(config.getSolrServerLocation());
    SolrServer server = StandaloneEmbeddedSolrServerProvider.getInstance().getSolrServer(solrIndexRef, config.getIndexConfigurationName());
    Assert.assertNotNull("Unable to initialise SolrServer for testing", server);
    yard = new SolrYard(server, config, null);
    backend = new YardBackend(yard);
}
Also used : YardBackend(org.apache.stanbol.entityhub.ldpath.backend.YardBackend) IndexReference(org.apache.stanbol.commons.solr.IndexReference) ManagedSolrServer(org.apache.stanbol.commons.solr.managed.ManagedSolrServer) SolrServer(org.apache.solr.client.solrj.SolrServer) SolrYard(org.apache.stanbol.entityhub.yard.solr.impl.SolrYard) SolrYardConfig(org.apache.stanbol.entityhub.yard.solr.impl.SolrYardConfig) BeforeClass(org.junit.BeforeClass)

Example 22 with SolrServer

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

the class FstLinkingEngineTest method setup.

@BeforeClass
public static void setup() throws Exception {
    // get the working directory
    // use property substitution to test this feature!
    String prefix = System.getProperty("basedir") == null ? "." : "${basedir}";
    String solrServerDir = prefix + TEST_INDEX_REL_PATH;
    log.info("Test Solr Server Directory: {}", solrServerDir);
    System.setProperty(ManagedSolrServer.MANAGED_SOLR_DIR_PROPERTY, solrServerDir);
    SolrYardConfig config = new SolrYardConfig(TEST_YARD_ID, TEST_SOLR_CORE_NAME);
    config.setAllowInitialisation(false);
    //the dbpedia default data
    config.setIndexConfigurationName(TEST_SOLR_CORE_CONFIGURATION);
    //init from datafile provider
    config.setAllowInitialisation(true);
    config.setName("DBpedia.org default data");
    config.setDescription("Data used for FstLinkingEngie tests");
    // create the Yard used for the tests
    IndexReference solrIndexRef = IndexReference.parse(config.getSolrServerLocation());
    SolrServer server = StandaloneEmbeddedSolrServerProvider.getInstance().getSolrServer(solrIndexRef, config.getIndexConfigurationName());
    Assert.assertNotNull("Unable to initialise SolrServer for testing", server);
    core = ((EmbeddedSolrServer) server).getCoreContainer().getCore(solrIndexRef.getIndex());
    Assert.assertNotNull("Unable to get SolrCore '" + config.getIndexConfigurationName() + "' from SolrServer " + server, core);
    yard = new SolrYard(server, config, null);
    //setup the index configuration
    LanguageConfiguration langConf = new LanguageConfiguration("not.used", new String[] { "en;field=dbpedia-ont:surfaceForm;generate=true" });
    fstConfig = new IndexConfiguration(langConf, core, FieldEncodingEnum.SolrYard, "");
    fstConfig.setExecutorService(Executors.newFixedThreadPool(1));
    fstConfig.setTypeField("rdf:type");
    fstConfig.setRankingField("entityhub:entityRank");
    //fstConfig.setEntityCacheManager(new FastLRUCacheManager(2048));
    fstConfig.setOrigin(new PlainLiteralImpl(TEST_ORIGIN));
    //activate the FST config
    //activate this configuration
    fstConfig.activate();
    //validate that the index contains the expected entities
    validateTestIndex();
    //now create the FST models
    List<Future<?>> creationTasks = new ArrayList<Future<?>>();
    for (CorpusInfo corpus : fstConfig.getCorpora()) {
        Assert.assertTrue("Failure in UnitTest - all FST models need to be generate=true", corpus.allowCreation);
        if (!corpus.isFstFile()) {
            //create a task on the FST corpus creation service
            creationTasks.add(fstConfig.getExecutorService().submit(new CorpusCreationTask(fstConfig, corpus)));
        }
    }
    //typical hardware
    for (Future<?> future : creationTasks) {
        try {
            future.get(FST_CREATION_WAIT_TIME, TimeUnit.SECONDS);
        } catch (TimeoutException e) {
        // we assert on future.isDone instead
        }
        Assert.assertTrue("FST Model creation not finished after " + FST_CREATION_WAIT_TIME + "seconds", future.isDone());
    }
}
Also used : PlainLiteralImpl(org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl) ArrayList(java.util.ArrayList) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) SolrServer(org.apache.solr.client.solrj.SolrServer) ManagedSolrServer(org.apache.stanbol.commons.solr.managed.ManagedSolrServer) SolrYard(org.apache.stanbol.entityhub.yard.solr.impl.SolrYard) SolrYardConfig(org.apache.stanbol.entityhub.yard.solr.impl.SolrYardConfig) IndexConfiguration(org.apache.stanbol.enhancer.engines.lucenefstlinking.IndexConfiguration) CorpusInfo(org.apache.stanbol.enhancer.engines.lucenefstlinking.CorpusInfo) Future(java.util.concurrent.Future) CorpusCreationTask(org.apache.stanbol.enhancer.engines.lucenefstlinking.CorpusCreationTask) LanguageConfiguration(org.apache.stanbol.enhancer.nlp.utils.LanguageConfiguration) IndexReference(org.apache.stanbol.commons.solr.IndexReference) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) TimeoutException(java.util.concurrent.TimeoutException) BeforeClass(org.junit.BeforeClass)

Example 23 with SolrServer

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

the class SolrYardComponent method initManagedSolrIndex.

/**
     * initialise ManagedSolrIndex and that starts tracking for the {@link SolrCore}
     * @param managedServer the managedServer to init the SolrCore (if necessary)
     * @param config the {@link SolrYardConfig}
     * @throws IllegalStateException if the initialization fails
     */
private void initManagedSolrIndex(final ManagedSolrServer managedServer, final SolrYardConfig config) {
    if (managedServer == null || config == null) {
        //component was not yet activated ... will be called again
        return;
    }
    IndexReference solrIndexRef = IndexReference.parse(config.getSolrServerLocation());
    if (config.isAllowInitialisation()) {
        //are we allowed to create the SolrServer
        //get the name of the config to be used (default: default.solrindex.zip")
        String configName = config.getIndexConfigurationName();
        IndexMetadata metadata = managedServer.getIndexMetadata(solrIndexRef.getIndex());
        if (metadata == null) {
            //create a new index
            log.info(" ... creating Managed SolrIndex {} (configName: {}) on Server {}", new Object[] { solrIndexRef.getIndex(), configName, managedServer.getServerName() });
            try {
                metadata = managedServer.createSolrIndex(solrIndexRef.getIndex(), configName, null);
            } catch (IOException e) {
                throw new IllegalStateException("Unable to create Managed SolrIndex " + solrIndexRef.getIndex() + " (configName: " + configName + ") on Server " + managedServer.getServerName() + "!", e);
            }
        } else if (metadata.getState() != ManagedIndexState.ACTIVE) {
            log.info(" ... activating Managed SolrIndex {} on Server {} (current state: {})", new Object[] { solrIndexRef.getIndex(), managedServer.getServerName(), metadata.getState() });
            try {
                managedServer.activateIndex(metadata.getIndexName());
            } catch (IOException e) {
                throw new IllegalStateException("Unable to activate Managed SolrIndex " + solrIndexRef.getIndex() + " (configName: " + configName + ") on Server " + managedServer.getServerName() + "!", e);
            } catch (SAXException e) {
                throw new IllegalStateException("Unable to activate Managed SolrIndex " + solrIndexRef.getIndex() + " (configName: " + configName + ") on Server " + managedServer.getServerName() + "!", e);
            }
        }
        //else already active nothing todo
        solrIndexRef = metadata.getIndexReference();
    }
    //else the SolrServer will be supplied (e.g. created by installing a full index)
    try {
        registeredServerTracker = new RegisteredSolrServerTracker(context, solrIndexRef, null) {

            @Override
            public void removedService(ServiceReference reference, Object service) {
                updateSolrYardRegistration(registeredServerTracker.getService(), config);
                super.removedService(reference, service);
            }

            @Override
            public void modifiedService(ServiceReference reference, Object service) {
                updateSolrYardRegistration(registeredServerTracker.getService(), config);
                super.modifiedService(reference, service);
            }

            @Override
            public SolrServer addingService(ServiceReference reference) {
                SolrServer server = super.addingService(reference);
                if (solrServer != null) {
                    log.warn("Multiple SolrServer for IndexLocation {} available!", config.getSolrServerLocation());
                } else {
                    updateSolrYardRegistration(server, config);
                }
                return server;
            }
        };
        log.info(" ... start tracking for SolrCore based on {}", solrIndexRef);
        //start tracking
        registeredServerTracker.open();
    } catch (InvalidSyntaxException e) {
        throw new IllegalStateException("Unable to track Managed SolrIndex " + solrIndexRef.getIndex() + "on Server " + managedServer.getServerName() + "!", e);
    }
}
Also used : RegisteredSolrServerTracker(org.apache.stanbol.commons.solr.RegisteredSolrServerTracker) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) IndexMetadata(org.apache.stanbol.commons.solr.managed.IndexMetadata) IndexReference(org.apache.stanbol.commons.solr.IndexReference) ManagedSolrServer(org.apache.stanbol.commons.solr.managed.ManagedSolrServer) HttpSolrServer(org.apache.solr.client.solrj.impl.HttpSolrServer) SolrServer(org.apache.solr.client.solrj.SolrServer) SAXException(org.xml.sax.SAXException) ServiceReference(org.osgi.framework.ServiceReference)

Example 24 with SolrServer

use of org.apache.solr.client.solrj.SolrServer in project qi4j-sdk by Qi4j.

the class SolrEntityQueryMixin method search.

@Override
public SolrDocumentList search(String queryString) throws SolrServerException {
    SolrServer server = solr.solrServer();
    NamedList<Object> list = new NamedList<Object>();
    list.add("q", queryString);
    QueryResponse query = server.query(SolrParams.toSolrParams(list));
    return query.getResults();
}
Also used : NamedList(org.apache.solr.common.util.NamedList) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrServer(org.apache.solr.client.solrj.SolrServer)

Example 25 with SolrServer

use of org.apache.solr.client.solrj.SolrServer in project play-cookbook by spinscale.

the class SolrPlugin method onEvent.

@Override
public void onEvent(String message, Object context) {
    if (!StringUtils.startsWith(message, "JPASupport.")) {
        return;
    }
    try {
        Model model = (Model) context;
        String entityId = model.getClass().getName() + ":" + model.getId().toString();
        SolrServer server = getSearchServer();
        server.deleteById(entityId);
        //			if ("JPASupport.objectPersisted".equals(message) || "JPASupport.objectUpdated".equals(message)) {
        if ("JPASupport.objectUpdated".equals(message)) {
            SolrInputDocument doc = new SolrInputDocument();
            doc.addField("id", entityId);
            doc.addField("searchClass", model.getClass().getName());
            for (java.lang.reflect.Field field : context.getClass().getFields()) {
                String fieldName = field.getName();
                Field annot = field.getAnnotation(Field.class);
                if (annot == null) {
                    continue;
                }
                String annotationValue = annot.value();
                if (annotationValue != null && !"#default".equals(annotationValue)) {
                    fieldName = annotationValue;
                }
                doc.addField(fieldName, field.get(context));
            }
            server.add(doc);
            Logger.info("Added entity %s with fields %s on event %s", model.getClass().getSimpleName(), doc.getFieldNames(), message);
        }
        server.commit();
    } catch (Exception e) {
        Logger.error(e, "Problem updating entity %s on event %s with error %s", context, message, e.getMessage());
    }
}
Also used : Field(org.apache.solr.client.solrj.beans.Field) SolrInputDocument(org.apache.solr.common.SolrInputDocument) Model(play.db.jpa.Model) SolrServer(org.apache.solr.client.solrj.SolrServer) CommonsHttpSolrServer(org.apache.solr.client.solrj.impl.CommonsHttpSolrServer) MalformedURLException(java.net.MalformedURLException)

Aggregations

SolrServer (org.apache.solr.client.solrj.SolrServer)40 ManagedSolrServer (org.apache.stanbol.commons.solr.managed.ManagedSolrServer)21 SolrServerException (org.apache.solr.client.solrj.SolrServerException)18 EmbeddedSolrServer (org.apache.solr.client.solrj.embedded.EmbeddedSolrServer)17 ClassifierException (org.apache.stanbol.enhancer.topic.api.ClassifierException)13 IOException (java.io.IOException)12 SolrQuery (org.apache.solr.client.solrj.SolrQuery)12 ConfigurationException (org.osgi.service.cm.ConfigurationException)11 SolrDocument (org.apache.solr.common.SolrDocument)10 TrainingSetException (org.apache.stanbol.enhancer.topic.api.training.TrainingSetException)10 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)10 OakSolrConfiguration (org.apache.jackrabbit.oak.plugins.index.solr.configuration.OakSolrConfiguration)8 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)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