solrcould javaAPI测试

public class SolrTest {

```
private CloudSolrServer solrServer;

@Before
public void createSolrServer() {
	// zkHost:zookeeper的地址列表
	String zkHost = "192.168.8.100:2181,192.168.8.101:2181,192.168.8.102:2181/solr";
	solrServer = new CloudSolrServer(zkHost);
	//设置zookeeper超时时间
	//solrServer.setZkClientTimeout(3000);
	//连接超时时间
	//solrServer.setZkConnectTimeout(3000);
	solrServer.setDefaultCollection("core1");
	solrServer.connect();
}

 // 向solrCloud上创建索引
@Test
public void testCreateIndexToSolrCloud() throws SolrServerException, IOException {
    SolrInputDocument document = new SolrInputDocument();
    document.addField("id", "100001");
    ///document.addField("title", "李四");
    solrServer.add(document);
    solrServer.commit();
}

// 搜索索引
@Test
public void testSearchIndexFromSolrCloud() throws Exception {

    SolrQuery query = new SolrQuery();
    query.setQuery("*:*");
    try {
        QueryResponse response = solrServer.query(query);
        SolrDocumentList docs = response.getResults();
        System.out.println("文档个数:" + docs.getNumFound());
        System.out.println("查询时间:" + response.getQTime());
        for (SolrDocument doc : docs) {
            ArrayList title = (ArrayList) doc.getFieldValue("title");
            String id = (String) doc.getFieldValue("id");
            System.out.println("id: " + id);
            System.out.println("title: " + title);
            System.out.println();
        }
    } catch (SolrServerException e) {
        e.printStackTrace();
    } catch (Exception e) {
        System.out.println("Unknowned Exception!!!!");
        e.printStackTrace();
    }
}

// 删除索引
@Test
public void testDeleteIndexFromSolrCloud() throws SolrServerException, IOException {
    // 根据id删除
    UpdateResponse response = solrServer.deleteById("100001");
    // 提交
    solrServer.commit();
}
```

}

猜你喜欢

转载自blog.csdn.net/gj_user/article/details/84713508
今日推荐