直接上代码:

【1】引入jar

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class Car implements Serializable {
//注意:这里的字符串id字段要在scheme.xml中有配置体现
@Field("id")
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* 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;

/**
* 创建SolrServer对象
* 该对象有两个可以使用,都是线程安全的 1、CommonsHttpSolrServer:启动web服务器使用的,通过http请求的 2、
* EmbeddedSolrServer:内嵌式的,导入solr的jar包就可以使用了 3、solr
* 4.0之后好像添加了不少东西,其中CommonsHttpSolrServer这个类改名为HttpSolrClient
*
*/
@Before
public void init() throws Exception{
//new HttpSolrClient.Builder(BASE_URL).build()
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());
// 这里也可以传递集合 List<SolrInputDocument>
server.add(doc1);
UpdateResponse rspcommit = server.commit();
// 操作完成 0
rspcommit.getStatus();
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);
// 添加集合 List<Car>
// server.addBeans(list);
server.commit();
System.out.println("操作完成!!!");
}

/**
* 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;
}
}

参考JAVA对Solr的增删查改

本文地址: https://github.com/maxzhao-it/blog/post/3276/