Testing Solr via Embedded Server

前端 未结 3 1457
粉色の甜心
粉色の甜心 2021-02-01 07:45

I\'m coding some tests for my solr-indexer application. Following testing best practices, I want to write code self-dependant, just loading the schema.xml and

3条回答
  •  猫巷女王i
    2021-02-01 07:56

    First you need to set your Solr Home Directory which contains solr.xml and conf folder containing solrconfig.xml, schema.xml etc.

    After that you can use this simple and basic code for Solrj.

    File solrHome = new File("Your/Solr/Home/Dir/");
    File configFile = new File(solrHome, "solr.xml");
    CoreContainer coreContainer = new CoreContainer(solrHome.toString(), configFile);
    SolrServer solrServer = new EmbeddedSolrServer(coreContainer, "Your-Core-Name-in-solr.xml");
    SolrQuery query = new SolrQuery("Your Solr Query");
    QueryResponse rsp = solrServer.query(query);
    SolrDocumentList docs = rsp.getResults();
    Iterator i = docs.iterator();
    while (i.hasNext()) {
          System.out.println(i.next().toString());
    }
    

    I hope this helps.

提交回复
热议问题