直接上代码:
【1】引入jar
<dependencies>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>4.10.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
【测试代码】
javabean:
/**
* Created by mis on 2017/5/24.
*/
public class Car implements Serializable {
@Field("id")//注意:这里的字符串id字段要在scheme.xml中有配置体现
private String id;
@Field("name")
private String name;
@Field("price")
private double price;
@Field("url")
private String url;
public Car(){}
public Car(String id, String name, double price,String url){
this.id = id;
this.name = name;
this.price = price;
this.url = url;
}
//getter setter方法
@Override
public String toString() {
return "Car{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", price=" + price +
", url='" + url + '\'' +
'}';
}
testSolr.java
/**
* Author:lvfang
*
* Created by mis on 2017/5/24.
*/
public class SolrTest {
public final String baseURL = "http://192.168.22.128:8081/solr";
public HttpSolrServer server=null;
@Before
public void init() throws Exception{
server=new HttpSolrServer(baseURL);
}
/**
* 基于索引名添加
* @throws Exception
*/
@Test
public void testAdd() throws Exception{
Car car1 = new Car("Audi000A4001","奥迪A4L",295000,"/images/001.jpg");
//Car car1 = new Car("BMW0000B5001","宝马5系",436800,"/images/002.jpg");
SolrInputDocument doc1 = new SolrInputDocument();
doc1.addField("id",car1.getId());
doc1.addField("name",car1.getName());
doc1.addField("price",car1.getPrice());
doc1.addField("url",car1.getUrl());
server.add(doc1);
server.commit();
System.out.println("操作完成!!!");
}
/**
* 基于bean添加(注意,这里bean的属性要添加solr的注解@Field)
* @throws Exception
*/
@Test
public void testAddBean() throws Exception{
Car car1 = new Car("BMW0000B5001","宝马5系",436800,"/images/002.jpg");
server.addBean(car1);
server.commit();
System.out.println("操作完成!!!");
}
/**
* 批量添加
* @throws Exception
*/
@Test
public void testAddMulti() throws Exception {
List<Car> list = new ArrayList<Car>();
Car car = null;
for(int i = 0; i < 40; i++){
car = new Car(UUID.randomUUID()+"","宝马"+i+"系",4368*i,"/images/bmw00"+i+".jpg");
list.add(car);
}
server.addBeans(list);
server.commit();
}
/**
* Document结果转换bean
* @throws Exception
*/
@Test
public void change() throws Exception {
SolrDocument doc1 = new SolrDocument();
doc1.addField("id","Siju0032SJ0C8001");
doc1.addField("name","世爵C8");
doc1.addField("price",1370000);
doc1.addField("url","/images/005.jps");
Car car = server.getBinder().getBean(Car.class,doc1);
System.out.println(car);
System.out.println("操作完成!!!");
}
/**
* 删除
* @throws Exception
*/
@Test
public void testDel() throws Exception{
//server.deleteById("1");
server.deleteByQuery("*:*");
server.commit();
System.out.println("操作完成!!!");
}
/**
* 修改(可以理解为重新添加,)
* @throws Exception
*/
@Test
public void testUpdate() throws Exception {
SolrInputDocument doc1 = new SolrInputDocument();
doc1.addField("id","1");
doc1.addField("title","lisi");
server.add(doc1);
server.commit();
System.out.println("操作完成!!!");
}
/**
* 查询
* @throws Exception
*/
@Test
public void testQuery() throws Exception {
SolrQuery query = new SolrQuery("name:*");
//SolrQuery query = new SolrQuery("*:*");
query.setStart(0);//起始页
query.setRows(3);//每页显示数量
QueryResponse rsp = server.query( query );
SolrDocumentList results = rsp.getResults();
System.out.println("总记录数为:" + results.getNumFound());//查询总条数
for(SolrDocument document : results){
System.out.println(document.get("id") + " " + document.get("name") + " " + document.get("price"));
}
}
/**
* 多条件查询
* @throws Exception
*/
@Test
public void testQueryMulti() throws Exception {
ModifiableSolrParams params = new ModifiableSolrParams();
//组织查询条件
params.set("q","*:*");
params.set("start",0);
params.set("rows",10);
params.set("sort","price desc");
QueryResponse response = server.query(params);
SolrDocumentList list = response.getResults();
for(SolrDocument doc : list){
System.out.println(server.getBinder().getBean(Car.class,doc));
}
}
/**
*
* @throws Exception
*/
@Test
public void testQueryCase() throws Exception{
SolrQuery params = new SolrQuery();
//AND OR NOT条件
// params.set("q","name:35系 AND price:152880.0");
// params.set("q","name:5系 OR price:152880.0");
// params.set("q","name:5系 NOT price:152880.0");
//To 条件 min <= price <= max
// params.set("q","price:[130000 TO 160000]");
//To 条件 min < price < max
// params.set("q","price:{130000 TO 160000}");
// params.addFacetQuery("name:宝马");
//显示设置
// params.setHighlight(true);
// params.addHighlightField("name");
// params.setHighlightSimplePre("<font color = 'red'>");
// params.setHighlightSimplePost("</font>");
// params.setHighlightSnippets(1);
// params.setHighlightFragsize(100);
// params.set("start",0);
// params.set("rows",10);
params.set("sort","price desc");
QueryResponse response = server.query(params);
SolrDocumentList list = response.getResults();
for(SolrDocument doc : list){
System.out.println(server.getBinder().getBean(Car.class,doc));
}
}
@After
public void destroy(){
server.shutdown();
server = null;
}
}