Solr简介
solr是基于Lucene,采用纯java开发的企业级检索工具,提供了比Lucene更为丰富的查询语言,配置更为灵活,扩展性更强
Solr有一个完善的检索介面,如下图所示:
Solr的运行方式
下载apache-solr-4.10.4.zip压缩吧,解压到C盘,进入C:\solr\apache-solr-4.10.4\example,执行java -jar start.jar,启动solr,输入网址http://localhost:8983/solr/#/~cores/collection1进行访问,如下图所示:
Solr和JavaWeb项目的整合和使用
solrj是solr的jar,用于访问solr类库。它提供了批次添加、新增、删除、查询等功能。
java创建solr的连接:
private Logger logger = Logger.getLogger(SolrWebServiceImpl.class);
private CommonsHttpSolrServer solrServer = null;
public SolrWebServiceImpl() {
try {
Properties pro = new Properties();
pro.load(SolrWebServiceImpl.class.getResourceAsStream("/application.properties"));
String SOLR_URL = "http://192.168.20.39:9090/solr/core0/";
solrServer = new CommonsHttpSolrServer(SOLR_URL);
solrServer.setMaxTotalConnections(100);
// socket read time
solrServer.setSoTimeout(50000);
solrServer.setConnectionTimeout(5000);
} catch (Exception e) {
logger.error("初始化方法报错:", e);
}
}
solr的批次汇入方法:
/**
* solr数据重整方法
*/
@Transactional
public void solrIndex() {
try {
Connection conn = DBConnection.getConnection();
if (conn != null) {
solrServer.deleteByQuery("*:*");
solrServer.commit();
ResultSet rs = null;
PreparedStatement ps = null;
String sql = "select titleid,title,author,ISBN,baseCallNumber,publisherName,yearOfPublication,"
+ "libraryWithAvailableCopies,materialType,All_book_Number,newbookdate from bib where titleid != '' and title != ''";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
Integer num = 0;
while (rs.next()) {
num++;
String titleid = rs.getString("titleid");
String title = rs.getString("title");
String author = rs.getString("author");
String ISBN = rs.getString("ISBN");
String baseCallNumber = rs.getString("baseCallNumber");
String publisherName = rs.getString("publisherName");
String yearOfPublication = rs.getString("yearOfPublication");
String libraryWithAvailableCopies = rs.getString("libraryWithAvailableCopies");
String materialType = rs.getString("materialType");
String All_book_Number = rs.getString("All_book_Number");
Date newbookdate = null;
SolrInputDocument doc = new SolrInputDocument();
doc.addField("titleid", titleid);
if(StringUtils.isNotBlank(title)){
doc.addField("title", title.trim());
doc.addField("title_sort", title.trim());
}
doc.addField("author", author);
doc.addField("ISBN", ISBN);
doc.addField("baseCallNumber", baseCallNumber);
if(StringUtils.isNotBlank(publisherName)){
doc.addField("publisherName", publisherName.trim());
}
if(isNumeric(yearOfPublication) && StringUtils.isNotBlank(yearOfPublication)){
doc.addField("yearOfPublication", Integer.parseInt(yearOfPublication));
}
doc.addField("libraryWithAvailableCopies", libraryWithAvailableCopies);
doc.addField("materialType", materialType);
doc.addField("All_book_Number", All_book_Number);
doc.addField("newbookdate", newbookdate);
docs.add(doc);
if (num % 1000 == 0) {
solrServer.add(docs);
solrServer.commit();
docs = new ArrayList<SolrInputDocument>();
}
}
solrServer.add(docs);
solrServer.commit();
DBConnection.closeDB(rs, ps, conn);
}
} catch (Exception e) {
logger.error("solrIndex方法报错:", e);
}
}
solr的单笔新增方法:
/**
* solr更新和新增方法如果titleid是已存在,那么就是更新动作,如果不存在,那么就是新增动作
*/
public String solrUpdateOrSave(String titleid, String title, String author, String isbn, String baseCallNumber, String publisherName, String yearOfPublication,
String libraryWithAvailableCopies, String materialType, String all_book_Number, String newbookdate) {
String returnValue = "false";
try {
ModifiableSolrParams params = new ModifiableSolrParams();
params.set("q", "titleid:" + titleid);
QueryResponse response = solrServer.query(params);
SolrDocumentList results = response.getResults();
// 输出结果
if (results.size() > 0) {
SolrDocument doc = results.get(0);
String titleTmp = doc.getFieldValue("title") == null ? null : doc.getFieldValue("title").toString();
String authorTmp = doc.getFieldValue("author") == null ? null : doc.getFieldValue("author").toString();
String ISBNTmp = doc.getFieldValue("ISBN") == null ? null : doc.getFieldValue("ISBN").toString();
String baseCallNumberTmp = doc.getFieldValue("baseCallNumber") == null ? null : doc.getFieldValue("baseCallNumber").toString();
String publisherNameTmp = doc.getFieldValue("publisherName") == null ? null : doc.getFieldValue("publisherName").toString();
String yearOfPublicationTmp = doc.getFieldValue("yearOfPublication") == null ? null : doc.getFieldValue("yearOfPublication").toString();
String libraryWithAvailableCopiesTmp = doc.getFieldValue("libraryWithAvailableCopies") == null ? null : doc.getFieldValue("libraryWithAvailableCopies").toString();
String materialTypeTmp = doc.getFieldValue("materialType") == null ? null : doc.getFieldValue("materialType").toString();
String All_book_NumberTmp = doc.getFieldValue("All_book_Number") == null ? null : doc.getFieldValue("All_book_Number").toString();
Date newbookdateTmp = null;
if (doc.getFieldValue("newbookdate") != null) {
newbookdateTmp = (Date) doc.getFieldValue("newbookdate");
}
SolrInputDocument docTmp = new SolrInputDocument();
if (StringUtils.isNotBlank(titleid)) {
docTmp.addField("titleid", titleid);
if (StringUtils.isNotBlank(title)) {
docTmp.addField("title", title);
docTmp.addField("title_sort", title);
} else {
docTmp.addField("title", titleTmp);
docTmp.addField("title_sort", titleTmp);
}
if (StringUtils.isNotBlank(author)) {
docTmp.addField("author", author);
} else {
docTmp.addField("author", authorTmp);
}
if (StringUtils.isNotBlank(isbn)) {
docTmp.addField("ISBN", isbn);
} else {
docTmp.addField("ISBN", ISBNTmp);
}
if (StringUtils.isNotBlank(baseCallNumber)) {
docTmp.addField("baseCallNumber", baseCallNumber);
} else {
docTmp.addField("baseCallNumber", baseCallNumberTmp);
}
if (StringUtils.isNotBlank(publisherName)) {
docTmp.addField("publisherName", publisherName);
} else {
docTmp.addField("publisherName", publisherNameTmp);
}
if (StringUtils.isNotBlank(yearOfPublication)) {
docTmp.addField("yearOfPublication", Integer.parseInt(yearOfPublication));
} else {
docTmp.addField("yearOfPublication", Integer.parseInt(yearOfPublicationTmp));
}
if (StringUtils.isNotBlank(libraryWithAvailableCopies)) {
docTmp.addField("libraryWithAvailableCopies", libraryWithAvailableCopies);
} else {
docTmp.addField("libraryWithAvailableCopies", libraryWithAvailableCopiesTmp);
}
if (StringUtils.isNotBlank(materialType)) {
docTmp.addField("materialType", materialType);
} else {
docTmp.addField("materialType", materialTypeTmp);
}
if (StringUtils.isNotBlank(all_book_Number)) {
docTmp.addField("All_book_Number", all_book_Number);
} else {
docTmp.addField("All_book_Number", All_book_NumberTmp);
}
if (newbookdate != null) {
docTmp.addField("newbookdate", new SimpleDateFormat("yyyy-MM-dd").parse(newbookdate));
} else {
docTmp.addField("newbookdate", newbookdateTmp);
}
solrServer.add(docTmp);
solrServer.commit();
}
}
returnValue = "true";
return returnValue;
} catch (Exception e) {
logger.error("solrUpdateOrSave方法报错:", e);
return returnValue;
}
}
solr的删除方法:
/**
* solr数据删除方法,根据titleid进行删除
*/
public String solrDelete(String titleid) {
String returnValue = "false";
try {
solrServer.deleteById(titleid);
solrServer.commit();
returnValue = "true";
return returnValue;
} catch (Exception e) {
logger.error("solrDelete方法报错:", e);
return returnValue;
}
}
solr的查询方法:
/**
* 根据条件进行查询
*/
public String solrQuerySearch(String searchType, String searchValue, String libraryWithAvailableCopiesTmp, String titleidTmp, String sortColumn, String sort, String pageSize,
String nowPage) {
List<Bib> bibList = new ArrayList<Bib>();
String searchStr = "";
try {
ModifiableSolrParams params = new ModifiableSolrParams();
if (StringUtils.isNotBlank(searchType) && searchType.equals("title") && StringUtils.isNotBlank(searchValue)) {
searchValue = escapeQueryChars(searchValue);
searchStr += " (title:" + searchValue.trim().toLowerCase() + ") ";
}
if (StringUtils.isNotBlank(searchType) && searchType.equals("author") && StringUtils.isNotBlank(searchValue)) {
searchStr += " (author:" + searchValue.trim().toLowerCase() + " OR author:*" + searchValue.trim().toLowerCase() + "*) ";
}
if (StringUtils.isNotBlank(searchType) && searchType.equals("isbn") && StringUtils.isNotBlank(searchValue)) {
searchStr += " ISBN:" + searchValue + " ";
}
if (StringUtils.isNotBlank(searchType) && searchType.equals("callNumber") && StringUtils.isNotBlank(searchValue)) {
searchStr += " baseCallNumber:" + searchValue + " ";
}
if (StringUtils.isNotBlank(libraryWithAvailableCopiesTmp) && !libraryWithAvailableCopiesTmp.toLowerCase().equals("all")) {
searchStr += " (libraryWithAvailableCopies:" + libraryWithAvailableCopiesTmp + " OR libraryWithAvailableCopies:*" + libraryWithAvailableCopiesTmp + "*) ";
}
if (StringUtils.isNotBlank(titleidTmp)) {
searchStr += " (titleid:" + titleidTmp + ") ";
}
// 如果所有查询条件爲空,查询全部
if (StringUtils.isBlank(searchStr)) {
searchStr += "*:*";
}
params.set("q", searchStr);
if (StringUtils.isNotBlank(nowPage) && StringUtils.isNotBlank(pageSize)) {
int start = (Integer.parseInt(nowPage) - 1) * Integer.parseInt(pageSize);
params.set("start", start);
params.set("rows", pageSize);
}
if (StringUtils.isNotBlank(sortColumn) && StringUtils.isNotBlank(sort)) {
if(sortColumn.equals("title")){
sortColumn = "title_sort";
}
String sortVal = sortColumn + " " + sort;
params.set("sort", sortVal);
}
QueryResponse response = solrServer.query(params);
SolrDocumentList results = response.getResults();
// 输出结果
for (SolrDocument doc : results) {
Bib bibTmp = new Bib();
String titleid = doc.getFieldValue("titleid") == null ? null : doc.getFieldValue("titleid").toString();
String title = doc.getFieldValue("title") == null ? null : doc.getFieldValue("title").toString();
String author = doc.getFieldValue("author") == null ? null : doc.getFieldValue("author").toString();
String ISBN = doc.getFieldValue("ISBN") == null ? null : doc.getFieldValue("ISBN").toString();
String baseCallNumber = doc.getFieldValue("baseCallNumber") == null ? null : doc.getFieldValue("baseCallNumber").toString();
String publisherName = doc.getFieldValue("publisherName") == null ? null : doc.getFieldValue("publisherName").toString();
String yearOfPublication = doc.getFieldValue("yearOfPublication") == null ? null : doc.getFieldValue("yearOfPublication").toString();
String libraryWithAvailableCopies = doc.getFieldValue("libraryWithAvailableCopies") == null ? null : doc.getFieldValue("libraryWithAvailableCopies").toString();
String materialType = doc.getFieldValue("materialType") == null ? null : doc.getFieldValue("materialType").toString();
String All_book_Number = doc.getFieldValue("All_book_Number") == null ? null : doc.getFieldValue("All_book_Number").toString();
Date newbookdate = null;
if (doc.getFieldValue("newbookdate") != null) {
newbookdate = (Date) doc.getFieldValue("newbookdate");
}
bibTmp.setTitleid(titleid);
bibTmp.setTitle(title);
bibTmp.setAuthor(author);
bibTmp.setIsbn(ISBN);
bibTmp.setBaseCallNumber(baseCallNumber);
bibTmp.setPublisherName(publisherName);
bibTmp.setYearOfPublication(yearOfPublication);
bibTmp.setLibraryWithAvailableCopies(libraryWithAvailableCopies);
bibTmp.setMaterialType(materialType);
bibTmp.setAll_book_Number(All_book_Number);
if (newbookdate != null) {
bibTmp.setNewbookdate(new SimpleDateFormat("yyyy-MM-dd").format(newbookdate));
}
bibList.add(bibTmp);
}
} catch (Exception e) {
logger.error("solrQuerySearch方法报错:", e);
}
if (bibList != null && bibList.size() > 0) {
String jsonStr = JSONArray.fromObject(bibList).toString();
return jsonStr;
} else {
return null;
}
}
请大家多多关注我的头条号,谢谢大家!