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(SolrParams params) throws SolrServerException, IOException 

Source Link

Document

Performs a query to the Solr server

Usage

From source file:com.ngdata.hbaseindexer.mr.TestUtils.java

License:Apache License

public static void validateSolrServerDocumentCount(File solrHomeDir, FileSystem fs, Path outDir,
        int expectedDocs, int expectedShards) throws IOException, SolrServerException {

    long actualDocs = 0;
    int actualShards = 0;
    for (FileStatus dir : fs.listStatus(outDir)) { // for each shard
        if (dir.getPath().getName().startsWith("part") && dir.isDirectory()) {
            actualShards++;//w  w w  . j  av a  2  s .c om
            EmbeddedSolrServer solr = createEmbeddedSolrServer(solrHomeDir, fs, dir.getPath());

            try {
                SolrQuery query = new SolrQuery();
                query.setQuery("*:*");
                QueryResponse resp = solr.query(query);
                long numDocs = resp.getResults().getNumFound();
                actualDocs += numDocs;
            } finally {
                solr.close();
            }
        }
    }
    assertEquals(expectedShards, actualShards);
    assertEquals(expectedDocs, actualDocs);
}

From source file:fr.gael.dhus.service.SystemService.java

License:Open Source License

/**
 * Performs Solr restoration./*from w w  w.j  a va 2 s.com*/
 *
 * @param properties properties containing arguments to execute the restoration.
 */
private static void restoreSolr5Index(Properties properties) throws IOException, SolrServerException {
    String solrHome = properties.getProperty("dhus.solr.home");
    String coreName = properties.getProperty("dhus.solr.core.name");
    final String name = properties.getProperty("dhus.solr.backup.name");
    final String location = properties.getProperty("dhus.solr.backup.location");

    if (solrHome == null || coreName == null || name == null || location == null) {
        throw new UnsupportedOperationException();
    }

    System.setProperty("solr.solr.home", solrHome);
    CoreContainer core = new CoreContainer(solrHome);
    EmbeddedSolrServer server = new EmbeddedSolrServer(core, coreName);
    try {
        server.getCoreContainer().load();

        SolrQuery query = new SolrQuery();
        query.setRequestHandler("/replication");
        query.set("command", "restore");
        query.set("name", name);
        query.set("location", location);

        server.query(query);
        LOGGER.info("SolR indexes restored.");
    } finally {
        server.close();
    }

}

From source file:org.ala.lucene.SolrIndexTest.java

License:Open Source License

/**
 * @param args//  w ww  . jav a2  s.  c  o m
 */
public static void main(String[] args) throws Exception {

    System.out.print("Quick search test >>> ");
    String input = "";

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    while (!"q".equals((input = br.readLine()))) {
        long start = System.currentTimeMillis();
        if (StringUtils.trimToNull(input) != null) {
            System.out.println("---------------------------------------------");
            input = StringUtils.trimToNull(input).toLowerCase();
            //            IndexSearcher is = new IndexSearcher("/data/lucene/taxonConcept");

            String solrHome = "/data/solr/bie";

            /**
              * Initialise the SOLR server instance
              */
            System.setProperty("solr.solr.home", solrHome);
            CoreContainer coreContainer = null;
            try {
                CoreContainer.Initializer initializer = new CoreContainer.Initializer();
                coreContainer = initializer.initialize();
            } catch (Exception e) {
                //FIXME this is a hack - there must be a better way of initialising SOLR here
                Directory dir = FSDirectory.open(new File(solrHome + "/index"));
                IndexWriterConfig indexWriterConfig = new IndexWriterConfig(SolrUtils.BIE_LUCENE_VERSION,
                        new StandardAnalyzer(SolrUtils.BIE_LUCENE_VERSION));
                IndexWriter idxWriter = new IndexWriter(dir, indexWriterConfig);
                idxWriter.commit();
                idxWriter.close();
                CoreContainer.Initializer initializer = new CoreContainer.Initializer();
                coreContainer = initializer.initialize();
            }

            EmbeddedSolrServer server = new EmbeddedSolrServer(coreContainer, "");

            SolrQuery query = new SolrQuery(input);
            QueryResponse qr = server.query(query);

            SolrDocumentList sdl = qr.getResults();
            Iterator iter = sdl.iterator();
            while (iter.hasNext()) {
                SolrDocument doc = (SolrDocument) iter.next();
                System.out.print(doc.getFieldValue("guid"));
                System.out.print("\t\t");
                Object valuesList = doc.getFieldValue("name");

                if (valuesList instanceof List) {
                    List<String> values = (List<String>) valuesList;
                    for (String s : values) {
                        System.out.println(">>  name: " + s);
                    }
                } else {
                    System.out.println(">>  name: " + valuesList);
                }

                System.out.print(doc.getFieldValue("guid"));
                System.out.println();
            }

            System.out.println();
        }
        System.out.print("Quick search test >>> ");
    }

    System.out.println("bye bye");
    System.exit(1);
}