Solr中初学Demo

时间:2023-03-08 15:58:44
 import java.util.Collection;
import java.util.Date; import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
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 TestSolr { String baseURL = "http://192.168.1.99:8983/solr";
HttpSolrServer httpSolrServer = new HttpSolrServer(baseURL); /**
* 获取操作solr的客户端对象
* @throws Exception
*/
@Test
public void test1() {
//指定solr的连接地址,注意:默认情况下连接的是collection1这个索引库
//下面的两个baseurl效果一样
//String baseURL = "http://192.168.1.171:8983/solr/collection1";
String baseURL = "http://192.168.1.99:8983/solr";
HttpSolrServer httpSolrServer = new HttpSolrServer(baseURL);
System.out.println(httpSolrServer.toString());
} /**
* 建立索引-1
*
* add(HttpSolrServer server,SolrInputDocument doc){
* server.add(doc);
* server.commit();
* }
* @throws Exception
*/
@Test
public void test2() throws Exception {
//把数据封装为一个document
SolrInputDocument doc = new SolrInputDocument();
doc.setField("id", "1");
doc.setField("name", "crxy1");//这个字段必须在schema.xml文件中定义了,否则会设置失败.这里id和name都已经在schema.xml文件中定义了.
doc.setField("last_modified", new Date());
//把这个文档添加到solr中
httpSolrServer.add(doc);//也会把数据添加到内存中,但是查询不到,因为没有在内存中生成segment,执行软提交的时候才会生成
//把这个添加操作提交
httpSolrServer.commit();//(硬提交)这个提交其实是表示把索引数据直接提交到硬盘中,并且可以保证数据能够查询到
//httpSolrServer.commit(true, true, true);//软提交,数据保存在内存中,并且保证数据可以查询
/**
* 在工作中,不建议没add一条数据,就硬提交一次,这样太消耗性能
* 建议,为了保证实时读取到新增的数据,可以,每add一条数据,就调用一次软提交
* 如果对数据的实时查询要求不是很高,建议在批量添加数据的时候,可以每添加1000条左右调用一次硬提交。
*/
} /**
* 建立索引-2
* 这种工作中用的比较多
* test2中需要自己去封装set...使用比较少...
* @throws Exception
*/
@Test
public void test3() throws Exception {
Person person = new Person();
person.setId("22");
person.setName("heeh22"); httpSolrServer.addBean(person);
httpSolrServer.commit();
} /**
* 删除
* @throws Exception
*/
@Test
public void test4() throws Exception {
//httpSolrServer.deleteById("1");//根据id删除
httpSolrServer.deleteByQuery("id:22");//根据查询条件删除
httpSolrServer.commit(); } /**
*
* @throws Exception
*/
@Test
public void test5() throws Exception {
//组装查询条件
SolrQuery params = new SolrQuery(); //具体查询条件就要拼字符串
params.setQuery("id:1");
//params.setQuery("name:samsung");//params.setQuery()和params.set()是一样的...建议使用setQuery
//params.set("q", "*:*"); //执行查询请求
QueryResponse response = httpSolrServer.query(params);
//从response中获取返回的结果
SolrDocumentList results = response.getResults();
//获取满足条件的数据总条数
long numFound = results.getNumFound();
System.out.println("总数:"+numFound); //当前查询返回document文档的总数,默认最多返回10条,通过rows控制的
//这个获取的总数是有问题的,如果要做分页,获取总页面数,要用results.getNumFound()来获取
System.out.println(results.size());//如果上面params对象使用params.set("q", "*:*"); 打印的是10 for (SolrDocument solrDocument : results) {
//获取文档中的所有字段
Collection<String> fieldNames = solrDocument.getFieldNames();
for (String field : fieldNames) {
//打印字段和对应字段的值
System.out.println(field+":"+solrDocument.get(field));
}
}
} }