package com.baidu.test;
import java.io.IOException;
import javax.management.Query;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
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 Solr {
/**
* 功能:添加功能
* 作者:wangwang
* 时间:2016-10-19 09:54:30
* @throws IOException
* @throws SolrServerException
*
*
*/
@Test
public void add() throws SolrServerException, IOException{
//1.获取solr连接
String urlString="http://localhost:8983/solr";
SolrServer solrServer = new HttpSolrServer(urlString);
SolrInputDocument solrInputDocument = new SolrInputDocument();
solrInputDocument.addField("id", "4");
solrInputDocument.addField("name", "柳杨");
solrInputDocument.addField("description", "女的 ,活的");
solrServer.add(solrInputDocument);
solrServer.commit();
}
/**
* 功能:删除功能
* 作者:wangwang
* 时间:2016-10-19 10:02:16
* @throws IOException
* @throws SolrServerException
*
*/
@Test
public void delete() throws SolrServerException, IOException{
//1.获取solr的连接
String urlString ="http://localhost:8983/solr";
SolrServer solrServer = new HttpSolrServer(urlString);
solrServer.deleteById("1");
solrServer.commit();
}
/**
* 功能:修改功能
* 作者:wangwang
* 时间:2016-10-19 10:06:34
* @throws IOException
* @throws
* SolrServerException
*/
@Test
public void update() throws SolrServerException, IOException{
//1.获取solr连接
String urlString="http://localhost:8983/solr";
SolrServer solrServer = new HttpSolrServer(urlString);
SolrInputDocument solrInputDocument = new SolrInputDocument();
solrInputDocument.addField("id", "2");
solrInputDocument.addField("name", "小倩");
solrInputDocument.addField("description", "是一个漂亮的女鬼!!");
solrServer.add(solrInputDocument);
solrServer.commit();
}
/**
* 功能:分页+列表
* 作者:wangwang
* 时间:2016-10-19 10:47:25
* @throws SolrServerException
*
*
*/
@Test
public void list() throws SolrServerException{
//获取solr连接
String urlString ="http://localhost:8983/solr";
SolrServer solrServer = new HttpSolrServer(urlString);
SolrQuery solrQuery = new SolrQuery();
solrQuery.setStart(0);
solrQuery.setRows(5);
solrQuery.setQuery("*:*");
QueryResponse queryResponse = solrServer.query(solrQuery);
SolrDocumentList results = queryResponse.getResults();
for (SolrDocument solrDocument : results) {
System.out.println(solrDocument);
}
}
}
import java.io.IOException;
import javax.management.Query;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
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 Solr {
/**
* 功能:添加功能
* 作者:wangwang
* 时间:2016-10-19 09:54:30
* @throws IOException
* @throws SolrServerException
*
*
*/
@Test
public void add() throws SolrServerException, IOException{
//1.获取solr连接
String urlString="http://localhost:8983/solr";
SolrServer solrServer = new HttpSolrServer(urlString);
SolrInputDocument solrInputDocument = new SolrInputDocument();
solrInputDocument.addField("id", "4");
solrInputDocument.addField("name", "柳杨");
solrInputDocument.addField("description", "女的 ,活的");
solrServer.add(solrInputDocument);
solrServer.commit();
}
/**
* 功能:删除功能
* 作者:wangwang
* 时间:2016-10-19 10:02:16
* @throws IOException
* @throws SolrServerException
*
*/
@Test
public void delete() throws SolrServerException, IOException{
//1.获取solr的连接
String urlString ="http://localhost:8983/solr";
SolrServer solrServer = new HttpSolrServer(urlString);
solrServer.deleteById("1");
solrServer.commit();
}
/**
* 功能:修改功能
* 作者:wangwang
* 时间:2016-10-19 10:06:34
* @throws IOException
* @throws
* SolrServerException
*/
@Test
public void update() throws SolrServerException, IOException{
//1.获取solr连接
String urlString="http://localhost:8983/solr";
SolrServer solrServer = new HttpSolrServer(urlString);
SolrInputDocument solrInputDocument = new SolrInputDocument();
solrInputDocument.addField("id", "2");
solrInputDocument.addField("name", "小倩");
solrInputDocument.addField("description", "是一个漂亮的女鬼!!");
solrServer.add(solrInputDocument);
solrServer.commit();
}
/**
* 功能:分页+列表
* 作者:wangwang
* 时间:2016-10-19 10:47:25
* @throws SolrServerException
*
*
*/
@Test
public void list() throws SolrServerException{
//获取solr连接
String urlString ="http://localhost:8983/solr";
SolrServer solrServer = new HttpSolrServer(urlString);
SolrQuery solrQuery = new SolrQuery();
solrQuery.setStart(0);
solrQuery.setRows(5);
solrQuery.setQuery("*:*");
QueryResponse queryResponse = solrServer.query(solrQuery);
SolrDocumentList results = queryResponse.getResults();
for (SolrDocument solrDocument : results) {
System.out.println(solrDocument);
}
}
}