04SolrCloud的Collection管理
2017-12-01 本文已影响0人
Explorer_Mi
启动tomcat


创建一个索引库(在浏览器中输入下面的命令)
http://192.168.25.110:8180/solr/admin/collections?action=CREATE&name=collection2&numShards=2&replicationFactor=2


删除多余的索引库
http://192.168.25.110:8180/solr/admin/collections?action=DELETE&name=collection1
启动集群版只需要启动下面两个服务就可以了

测试方法
package cn.e3mall.solrj.test;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.CloudSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;
public class TestSolrCloud {
@Test
public void testAddDocument() throws Exception {
//创建一个集群的连接,应该使用CloudSolrServer创建。
CloudSolrServer solrServer = new CloudSolrServer("192.168.25.110:2181,192.168.25.110:2182,192.168.25.110:2183");
//zkHost:zookeeper的地址列表
//设置一个defaultCollection属性。
solrServer.setDefaultCollection("collection2");
//创建一个文档对象
SolrInputDocument document = new SolrInputDocument();
//向文档中添加域
document.setField("id", "solrcloud01");
document.setField("item_title", "测试商品01");
document.setField("item_price", 123);
//把文件写入索引库
solrServer.add(document);
//提交
solrServer.commit();
}
@Test
public void testQueryDocument() throws Exception {
//创建一个CloudSolrServer对象
CloudSolrServer cloudSolrServer = new CloudSolrServer("192.168.25.110:2181,192.168.25.110:2182,192.168.25.110:2183");
//设置默认的Collection
cloudSolrServer.setDefaultCollection("collection2");
//创建一个查询对象
SolrQuery query = new SolrQuery();
//设置查询条件
query.setQuery("*:*");
//执行查询
QueryResponse queryResponse = cloudSolrServer.query(query);
//取查询结果
SolrDocumentList solrDocumentList = queryResponse.getResults();
System.out.println("总记录数:" + solrDocumentList.getNumFound());
//打印
for (SolrDocument solrDocument : solrDocumentList) {
System.out.println(solrDocument.get("id"));
System.out.println(solrDocument.get("title"));
System.out.println(solrDocument.get("item_title"));
System.out.println(solrDocument.get("item_price"));
}
}
}