SolrJ是操作Solr的Java客戶端,它提供了增加、修改、刪除、查詢Solr索引的JAVA接口。SolrJ針對 Solr提供了Rest 的HTTP接口進行了封裝, SolrJ底層是通過使用httpClient中的方法來完成Solr的操作。
jar包的引用(maven pom.xml):
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
< dependency > < artifactId >solr-solrj</ artifactId > < version >5.3.1</ version > </ dependency > < dependency > < groupId >junit</ groupId > < artifactId >junit</ artifactId > < version >4.11</ version > </ dependency > < dependency > < groupId >org.slf4j</ groupId > < artifactId >slf4j-log4j12</ artifactId > < version >1.7.7</ version > </ dependency > < dependency > < groupId >commons-logging</ groupId > < artifactId >commons-logging</ artifactId > < version >1.1.3</ version > </ dependency > |
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
|
package entity; import java.io.IOException; import java.util.List; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrServerException; 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; public class SolrJTest { //指定solr服務(wù)器的地址 private final static String SOLR_URL = " http://localhost:8080/solr/ " ; /** * 創(chuàng)建SolrServer對象 * * 該對象有兩個可以使用,都是線程安全的 * 1、CommonsHttpSolrServer:啟動web服務(wù)器使用的,通過http請求的 * 2、 EmbeddedSolrServer:內(nèi)嵌式的,導入solr的jar包就可以使用了 * 3、solr 4.0之后好像添加了不少東西,其中CommonsHttpSolrServer這個類改名為HttpSolrClient * * @return */ public HttpSolrClient createSolrServer(){ HttpSolrClient solr = null ; solr = new HttpSolrClient(SOLR_URL); return solr; } /** * 往索引庫添加文檔 * @throws IOException * @throws SolrServerException */ public void addDoc() throws SolrServerException, IOException{ //構(gòu)造一篇文檔 SolrInputDocument document = new SolrInputDocument(); //往doc中添加字段,在客戶端這邊添加的字段必須在服務(wù)端中有過定義 document.addField( "id" , "8" ); document.addField( "name" , "周新星" ); document.addField( "description" , "一個灰常牛逼的軍事家" ); //獲得一個solr服務(wù)端的請求,去提交 ,選擇具體的某一個solr core HttpSolrClient solr = new HttpSolrClient(SOLR_URL + "my_core" ); solr.add(document); solr.commit(); solr.close(); } /** * 根據(jù)id從索引庫刪除文檔 */ public void deleteDocumentById() throws Exception { //選擇具體的某一個solr core HttpSolrClient server = new HttpSolrClient(SOLR_URL+ "my_core" ); //刪除文檔 server.deleteById( "8" ); //刪除所有的索引 //solr.deleteByQuery("*:*"); //提交修改 server.commit(); server.close(); } /** * 查詢 * @throws Exception */ public void querySolr() throws Exception{ HttpSolrClient solrServer = new HttpSolrClient(SOLR_URL+ "my_core/" ); SolrQuery query = new SolrQuery(); //下面設(shè)置solr查詢參數(shù) //query.set("q", "*:*");// 參數(shù)q 查詢所有 query.set( "q" , "周星馳" ); //相關(guān)查詢,比如某條數(shù)據(jù)某個字段含有周、星、馳三個字 將會查詢出來 ,這個作用適用于聯(lián)想查詢 //參數(shù)fq, 給query增加過濾查詢條件 query.addFilterQuery( "id:[0 TO 9]" ); //id為0-4 //給query增加布爾過濾條件 //query.addFilterQuery("description:演員"); //description字段中含有“演員”兩字的數(shù)據(jù) //參數(shù)df,給query設(shè)置默認搜索域 query.set( "df" , "name" ); //參數(shù)sort,設(shè)置返回結(jié)果的排序規(guī)則 query.setSort( "id" ,SolrQuery.ORDER.desc); //設(shè)置分頁參數(shù) query.setStart( 0 ); query.setRows( 10 ); //每一頁多少值 //參數(shù)hl,設(shè)置高亮 query.setHighlight( true ); //設(shè)置高亮的字段 query.addHighlightField( "name" ); //設(shè)置高亮的樣式 query.setHighlightSimplePre( "<font color='red'>" ); query.setHighlightSimplePost( "</font>" ); //獲取查詢結(jié)果 QueryResponse response = solrServer.query(query); //兩種結(jié)果獲取:得到文檔集合或者實體對象 //查詢得到文檔的集合 SolrDocumentList solrDocumentList = response.getResults(); System.out.println( "通過文檔集合獲取查詢的結(jié)果" ); System.out.println( "查詢結(jié)果的總數(shù)量:" + solrDocumentList.getNumFound()); //遍歷列表 for (SolrDocument doc : solrDocumentList) { System.out.println( "id:" +doc.get( "id" )+ " name:" +doc.get( "name" )+ " description:" +doc.get( "description" )); } //得到實體對象 List<Person> tmpLists = response.getBeans(Person. class ); if (tmpLists!= null && tmpLists.size()> 0 ){ System.out.println( "通過文檔集合獲取查詢的結(jié)果" ); for (Person per:tmpLists){ System.out.println( "id:" +per.getId()+ " name:" +per.getName()+ " description:" +per.getDescription()); } } } public static void main(String[] args) throws Exception { SolrJTest solr = new SolrJTest(); //solr.createSolrServer(); solr.addDoc(); solr.deleteDocumentById(); solr.querySolr(); } } |
參考文檔:http://www.doc88.com/p-6763747939865.html
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/u012385190/article/details/53115546