Win下solr5.3使用SolrJ進行CRUD操作
阿新 • • 發佈:2019-01-25
C : 建立索引
R : 刪除索引
U : 更新索引
D : 刪除索引
本篇文章是另一種建立操作索引的方法,請勿在第二篇文章的基礎上修改。
- 新建一個Java工程,將以下三個目錄下的jar包全部匯入到工程中(可能有沒用上的jar包)
path\solr-5.3.0\dist\*.jar
path\solr-5.3.0\dist\solrj-lib\*.jar
path\solr-5.3.0\server\lib\ext\*.jar
- 參照上一篇文章,修改schema.xml檔案,將相應的欄位名、型別修改為你自己想要的型別。本文就以上一篇文章的例子為例。
- 建立一個實體類,其中的欄位和 schema.xml
import java.util.Date;
import org.apache.solr.client.solrj.beans.Field;
public class Goods {
@Field("id") // 注入屬性
private int id;
@Field("name")
private String name;
@Field("number")
private int number;
@Field("updateTime")
private Date updateTime;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
}
- 具體的操作程式碼如下,僅供參考(舉一反三吧)。程式碼中可能有小錯誤,多多包涵。
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.SolrPingResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.MapSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.junit.Test;
import com.swh.bean.Goods;
public class SolrJ {
String url = "http://localhost:8983/solr/s2";
// 測試連通性
@Test
public void createSolrClient() {
try {
SolrClient solr = new HttpSolrClient(url);
SolrPingResponse response = solr.ping();
// 列印執行時間
System.out.println(response.getElapsedTime());
solr.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 增加索引
*
* @throws Exception
*/
@Test
public void addIndex() throws Exception {
SolrClient solr = new HttpSolrClient(url);
// 新增方式1
SolrInputDocument docs = new SolrInputDocument();
docs.addField("id", "3");
docs.addField("name", "aaa");
docs.addField("number", "aaa");
docs.addField("updateTime", "2016-7-19");
UpdateResponse response = solr.add(docs);
System.out.println(response.getElapsedTime());
// 新增方式2
Goods goods = new Goods();
goods.setId(4);
goods.setName("bbb");
goods.setNumber(22);
goods.setUpdateTime(new Date());
response = solr.addBean(goods);
System.out.println(response.getElapsedTime());
solr.commit();
solr.close();
}
/**
* 批量新增索引
*
* @throws Exception
*/
@Test
public void addIndexSum() throws Exception {
SolrClient solr = new HttpSolrClient(url);
List<Goods> list = new ArrayList<Goods>();
Goods goods = null;
for (int i = 0; i < 25; i++) {
goods = new Goods();
goods.setId(i + 20);
goods.setName("hahahaha");
list.add(goods);
}
UpdateResponse response = solr.addBeans(list);
System.out.println(response.getElapsedTime());
solr.commit();
solr.close();
}
/**
* 刪除索引
*/
@Test
public void delIndex() throws Exception {
SolrClient solr = new HttpSolrClient(url);
UpdateResponse response = solr.deleteById("22");
System.out.println(response.getElapsedTime());
solr.commit();
solr.close();
}
/**
* 簡單查詢
*
* @throws Exception
*/
@Test
public void query() throws Exception {
SolrClient solr = new HttpSolrClient(url);
Map<String, String> map = new HashMap<String, String>();
map.put("q", "*:*");
SolrParams params = new MapSolrParams(map);
QueryResponse resp = solr.query(params);
// 以下是第二種方法
// String queryString="content:test";
// MultiMapSolrParams mParams =
// SolrRequestParsers.parseQueryString("queryString");
// QueryResponse resp = solr.query(mParams);
SolrDocumentList docsList = resp.getResults();
System.out.println(docsList.size());
System.out.println("---------------------");
for (SolrDocument doc : docsList) {
System.out.println(doc.get("id"));
}
solr.close();
}
@Test
public void find() throws SolrServerException, IOException {
SolrClient solr = new HttpSolrClient(url);
SolrQuery solrQuery = new SolrQuery();
// solrQuery.setQuery("*:*");
solrQuery.set("q", "*:*");
// 設定分頁的相關屬性
solrQuery.setStart(3);
solrQuery.setRows(5);
QueryResponse queryResponse = solr.query(solrQuery);
SolrDocumentList list = queryResponse.getResults();
for (SolrDocument document : list) {
Object id = document.get("id");
Object number = document.get("number");
System.out.println(id + "\t" + number);
}
}
}