Example usage for org.apache.solr.client.solrj.embedded EmbeddedSolrServer query

List of usage examples for org.apache.solr.client.solrj.embedded EmbeddedSolrServer query

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj.embedded EmbeddedSolrServer query.

Prototype

public QueryResponse query(String collection, SolrParams params) throws SolrServerException, IOException 

Source Link

Document

Performs a query to the Solr server

Usage

From source file:it.seralf.solrbook.client.java.EmbeddedSolrExample.java

License:Apache License

public static void main(String[] args) throws Exception {

    // NOTE: we can override this configuration, passing a valid slr home from command line
    System.setProperty("solr.solr.home", "solr-home");

    CoreContainer container = new CoreContainer();
    container.load();//from   w  w  w  .  j  ava  2 s. com
    EmbeddedSolrServer server = new EmbeddedSolrServer(container, "arts");

    // delete all documents
    server.deleteByQuery("*:*");

    Artist doc = new Artist("http://en.wikipedia.org/wiki/Leonardo_da_Vinci", "Leonardo Da Vinci", "Vinci",
            "Florence");
    server.addBean(doc);

    server.commit();

    QueryResponse rsp = server.query(new SolrQuery("leonardo"), METHOD.GET);

    SolrDocumentList oo = rsp.getResults();
    for (SolrDocument d : oo) {
        for (String field : d.getFieldNames()) {
            System.out.printf("%s = %s\n", field, d.getFieldValue(field));
        }
    }

    server.shutdown();
}

From source file:org.codice.solr.query.SolrQueryFilterVisitorTest.java

License:Open Source License

@Test
@Ignore// w w w .j  a va 2s  . c  o m
public void test() throws Exception {
    LOGGER.info("Running test ...");

    // setup
    String workingDir = System.getProperty("user.dir") + "/src/test/resources/";
    String solrConfDir = workingDir + "solr/conf/";
    File solrConfigFile = new File(solrConfDir + "solrconfig.xml"); //getConfigFile(solrConfigFileName, configProxy);
    assertTrue(solrConfigFile.exists());
    File solrSchemaFile = new File(solrConfDir + "schema.xml"); //getConfigFile(schemaFileName, configProxy);
    assertTrue(solrSchemaFile.exists());
    File solrFile = new File(solrConfDir + "solr.xml"); //getConfigFile(DEFAULT_SOLR_XML, configProxy);
    assertTrue(solrFile.exists());

    File solrConfigHome = new File(solrConfigFile.getParent());
    assertTrue(solrConfigHome.exists());

    SolrConfig solrConfig = null;
    IndexSchema indexSchema = null;
    SolrResourceLoader resourceLoader = null;
    SolrCoreContainer container = null;

    try {
        // NamedSPILoader uses the thread context classloader to lookup
        // codecs, posting formats, and analyzers
        solrConfig = new SolrConfig(solrConfigHome.getParent(), "solrConfig.xml",
                new InputSource(FileUtils.openInputStream(solrConfigFile)));
        assertNotNull(solrConfig);
        indexSchema = new IndexSchema(solrConfig, "schema.xml",
                new InputSource(FileUtils.openInputStream(solrSchemaFile)));
        assertNotNull(indexSchema);
        resourceLoader = new SolrResourceLoader(solrConfigHome.getAbsolutePath());
        assertNotNull(resourceLoader);
        container = new SolrCoreContainer(resourceLoader, solrFile);
        assertNotNull(container);
        CoreDescriptor coreDescriptor = new CoreDescriptor(container, CORE_NAME,
                solrConfig.getResourceLoader().getInstanceDir());
        assertNotNull(coreDescriptor);

        File dataDir = new File(workingDir + "data"); //configProxy.getDataDirectory();
        LOGGER.debug("Using data directory [{}]", dataDir);

        SolrCore core = new SolrCore(CORE_NAME, dataDir.getAbsolutePath(), solrConfig, indexSchema,
                coreDescriptor);
        container.register(CORE_NAME, core, false);
        assertNotNull(core);

        EmbeddedSolrServer solrServer = new EmbeddedSolrServer(container, CORE_NAME);

        // the test
        SolrQueryFilterVisitor visitor = new SolrQueryFilterVisitor(solrServer, CORE_NAME);
        Filter filter = ECQL.toFilter("Name = 'Hugh'");
        SolrQuery solrQuery = (SolrQuery) filter.accept(visitor, null);
        assertNotNull(solrQuery);

        // Solr does not support outside parenthesis in certain queries and throws EOF exception.
        String queryPhrase = solrQuery.getQuery().trim();
        if (queryPhrase.matches("\\(\\s*\\{!.*\\)")) {
            solrQuery.setQuery(queryPhrase.replaceAll("^\\(\\s*|\\s*\\)$", ""));
        }
        LOGGER.info("solrQuery = {}", solrQuery);

        QueryResponse solrResponse = solrServer.query(solrQuery, METHOD.POST);
        assertNotNull(solrResponse);
        long numResults = solrResponse.getResults().getNumFound();
        LOGGER.info("numResults = {}", numResults);
    } catch (ParserConfigurationException e) {
        LOGGER.warn("Parser configuration exception loading index schema", e);
    } catch (IOException e) {
        LOGGER.warn("IO exception loading index schema", e);
    } catch (SAXException e) {
        LOGGER.warn("SAX exception loading index schema", e);
    }
}