八、SOLRJ 更新和删除索引
SolrUtil
SolrUtil提供一个对象的增加或者更新(都是同一个方法)
根据id删除这个索引
public static <T> boolean saveOrUpdate(T entity) throws SolrServerException, IOException {
DocumentObjectBinder binder = new DocumentObjectBinder();
SolrInputDocument doc = binder.toSolrInputDocument(entity);
client.add(doc);
client.commit();
return true;
}
根据id删除这个索引
public static boolean deleteById(String id) {
try {
client.deleteById(id);
client.commit();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
|
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
|
package how2java;import java.io.IOException;import java.util.List;import org.apache.solr.client.solrj.SolrClient;import org.apache.solr.client.solrj.SolrQuery;import org.apache.solr.client.solrj.SolrServerException;import org.apache.solr.client.solrj.beans.DocumentObjectBinder;import org.apache.solr.client.solrj.impl.HttpSolrClient;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.apache.solr.common.util.NamedList;public class SolrUtil { public static SolrClient client; private static String url; static { url = "http://localhost:8983/solr/how2java"; client = new HttpSolrClient.Builder(url).build(); } public static void queryHighlight(String keywords) throws SolrServerException, IOException { SolrQuery q = new SolrQuery(); //开始页数 q.setStart(0); //每页显示条数 q.setRows(10); // 设置查询关键字 q.setQuery(keywords); // 开启高亮 q.setHighlight(true); // 高亮字段 q.addHighlightField("name"); // 高亮单词的前缀 q.setHighlightSimplePre("<span style='color:red'>"); // 高亮单词的后缀 q.setHighlightSimplePost("</span>"); //摘要最长100个字符 q.setHighlightFragsize(100); //查询 QueryResponse query = client.query(q); //获取高亮字段name相应结果 NamedList<Object> response = query.getResponse(); NamedList<?> highlighting = (NamedList<?>) response.get("highlighting"); for (int i = 0; i < highlighting.size(); i++) { System.out.println(highlighting.getName(i) + ":" + highlighting.getVal(i)); } //获取查询结果 SolrDocumentList results = query.getResults(); for (SolrDocument result : results) { System.out.println(result.toString()); } } public static <T> boolean batchSaveOrUpdate(List<T> entities) throws SolrServerException, IOException { DocumentObjectBinder binder = new DocumentObjectBinder(); int total = entities.size(); int count=0; for (T t : entities) { SolrInputDocument doc = binder.toSolrInputDocument(t); client.add(doc); System.out.printf("添加数据到索引中,总共要添加 %d 条记录,当前添加第%d条 %n",total,++count); } client.commit(); return true; } public static QueryResponse query(String keywords,int startOfPage, int numberOfPage) throws SolrServerException, IOException { SolrQuery query = new SolrQuery(); query.setStart(startOfPage); query.setRows(numberOfPage); query.setQuery(keywords); QueryResponse rsp = client.query(query); return rsp; } public static <T> boolean saveOrUpdate(T entity) throws SolrServerException, IOException { DocumentObjectBinder binder = new DocumentObjectBinder(); SolrInputDocument doc = binder.toSolrInputDocument(entity); client.add(doc); client.commit(); return true; } public static boolean deleteById(String id) { try { client.deleteById(id); client.commit(); } catch (Exception e) { e.printStackTrace(); return false; } return true; } } |
TestSolr4j
修改之前查询一次
修改之后查询一次
删除之后查询一次
观察修改和删除的效果
修改之后查询一次
删除之后查询一次
观察修改和删除的效果
|
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
|
package how2java;import java.io.IOException;import java.util.Collection;import java.util.List;import org.apache.solr.client.solrj.SolrServerException;import org.apache.solr.client.solrj.response.QueryResponse;import org.apache.solr.common.SolrDocument;import org.apache.solr.common.SolrDocumentList;public class TestSolr4j { public static void main(String[] args) throws SolrServerException, IOException { String keyword = "name:鞭"; System.out.println("修改之前"); query(keyword); Product p = new Product(); p.setId(51173); p.setName("修改后的神鞭"); SolrUtil.saveOrUpdate(p); System.out.println("修改之后"); query(keyword); SolrUtil.deleteById("51173"); System.out.println("删除之后"); query(keyword); } private static void query(String keyword) throws SolrServerException, IOException { QueryResponse queryResponse = SolrUtil.query(keyword,0,10); SolrDocumentList documents= queryResponse.getResults(); System.out.println("累计找到的条数:"+documents.getNumFound()); if(!documents.isEmpty()){ Collection<String> fieldNames = documents.get(0).getFieldNames(); for (String fieldName : fieldNames) { System.out.print(fieldName+"\t"); } System.out.println(); } for (SolrDocument solrDocument : documents) { Collection<String> fieldNames= solrDocument.getFieldNames(); for (String fieldName : fieldNames) { System.out.print(solrDocument.get(fieldName)+"\t"); } System.out.println(); } }} |
浙公网安备 33010602011771号