1, 下载solr 3.0/4.0,本文以3.0为例(参考附件的说明)
2, 创建一个抽象的SolrEntity,在solr的schema文件中,定义这几个字段
public abstract class SolrEntity implements Serializable {
@Field("id")
protected String solrjId;
@Field("entity_author")
protected String entityAuthor;
@Field("entity_content")
protected String entityContent;
@Field("entity_timestamp")
protected String entityTimeStamp;
//All the Pojo must override this method to generate the index
public abstract void generateSolrIndex();
public static SolrEntity generateSolrEntity(SolrDocument document) {
SolrEntity entity = new SolrEntity() {
@Override
public void generateSolrIndex() {
}
};
entity.setSolrjId(String.valueOf(document.getFieldValue("id")));
entity.setEntityAuthor(String.valueOf(document.getFieldValue("entity_author")));
entity.setEntityContent(String.valueOf(document.getFieldValue("entity_content")));
entity.setEntityTimeStamp(String.valueOf(document.getFieldValue("entity_timestamp")));
return entity;
}
public void highlight(String keywords) {
//TODO:here we can make the search string high light
}
public String getSolrjId() {
return solrjId;
}
public void setSolrjId(String solrjId) {
this.solrjId = solrjId;
}
public String getEntityContent() {
return entityContent;
}
public void setEntityContent(String entityContent) {
this.entityContent = entityContent;
}
public String getEntityAuthor() {
return entityAuthor;
}
public void setEntityAuthor(String entityAuthor) {
this.entityAuthor = entityAuthor;
}
public String getEntityTimeStamp() {
return entityTimeStamp;
}
public void setEntityTimeStamp(String entityTimeStamp) {
this.entityTimeStamp = entityTimeStamp;
}
}
3, 集成这个SolrEntity的需要实现下面的这个方法
public void generateSolrIndex() {
this.solrjId = this.getClass().getSimpleName() + ":" + id;
this.entityAuthor = getCreatorName();
this.entityContent = getContext();
this.entityTimeStamp = DateUtil.formatDateToString(getCreateTime());
}
4, 创建SolrService和SolrServiceImpl
public interface SolrService {
//最好通过事件来通知更新索引
void index(SolrEntity entity);
void delete(SolrEntity entity);
int queryForNumber(String searchCondition) throws Exception;
List<SolrEntity> queryForResult(String searchCondition, int start) throws Exception;
}
ublic class SolrServiceImpl implements SolrService {
public void index(SolrEntity entity) {
try {
SolrServerService solrServerService = new SolrServerService();
CommonsHttpSolrServer solrServer = solrServerService.getServer();
entity.generateSolrIndex();
solrServer.addBean(entity);
solrServer.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
public void delete(SolrEntity entity) {
try {
SolrServerService solrServerService = new SolrServerService();
CommonsHttpSolrServer solrServer = solrServerService.getServer();
entity.generateSolrIndex();
solrServer.deleteByQuery("id:" + entity.getSolrjId());
solrServer.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
public int queryForNumber(String searchCondition) throws Exception {
SolrServerService solrServerService = new SolrServerService();
CommonsHttpSolrServer solrServer = solrServerService.getServer();
SolrQuery query = new SolrQuery();
query.setFields("id");
query.setRows(10000);
query.setQuery(searchCondition);
QueryResponse response = solrServer.query(query);
return response.getResults().size();
}
public List<SolrEntity> queryForResult(String searchCondition, int start) throws Exception {
List<SolrEntity> entities = new ArrayList<SolrEntity>();
SolrServerService solrServerService = new SolrServerService();
CommonsHttpSolrServer solrServer = solrServerService.getServer();
SolrQuery query = new SolrQuery();
query.setRows(PagingUtil.DEFAULT_OVERVIEW_MAX_ITEMS);
query.setStart(start);
query.setQuery(searchCondition);
QueryResponse response = solrServer.query(query);
SolrDocumentList solrDocumentList = response.getResults();
for (SolrDocument document : solrDocumentList.subList(0, solrDocumentList.size())) {
SolrEntity entity = SolrEntity.generateSolrEntity(document);
entities.add(entity);
}
return entities;
//这个就是你得到的结果
}
}
5, 上面用到的SolrServerService可以通过很多种方式来获取(bean factory, pojo)
public class SolrServerService {
private static CommonsHttpSolrServer server;
public CommonsHttpSolrServer getServer() {
if (server == null) {
try {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("Solr.properties");
Properties p = new Properties();
try {
p.load(inputStream);
} catch (IOException e1) {
e1.printStackTrace();
}
String serverUrl = p.getProperty("solr.serverUrl");
String connectionTimeout = p.getProperty("solr.connectionTimeout");
String defaultMaxConnectionsPerHost = p.getProperty("solr.connectionTimeout");
String maxTotalConnections = p.getProperty("solr.maxTotalConnections");
String followRedirects = p.getProperty("solr.followRedirects");
String allowCompression = p.getProperty("solr.allowCompression");
String maxRetries = p.getProperty("solr.maxRetries");
server = new CommonsHttpSolrServer(serverUrl);
server.setConnectionTimeout(Integer.valueOf(connectionTimeout));
server.setDefaultMaxConnectionsPerHost(Integer.valueOf(defaultMaxConnectionsPerHost));
server.setMaxTotalConnections(Integer.valueOf(maxTotalConnections));
server.setFollowRedirects(Boolean.valueOf(followRedirects));
server.setAllowCompression(Boolean.valueOf(allowCompression));
server.setMaxRetries(Integer.valueOf(maxRetries));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("solr init error");
}
}
return server;
}
}
分享到:
相关推荐
SOLR搭建企业级搜索引擎 很详细 很不错哦
资源名称:相关性搜索 利用Solr与Elasticsearch创建智能应用内容简介:《相关性搜索:利用Solr与Elasticsearch创建智能应用》揭开了相关性搜索的神秘面纱,告诉大家如何将 Elasticsearch与 Solr这样的搜索引擎作为可...
SOLR搭建企业搜索平台。solr是基于Lucene Java搜索库的企业级全文搜索引擎,目前是apache的一个项目。
solr配置搜索引擎平台 一、 SOLR搭建企业搜索平台 二.... 三,,,,
《解密搜索引擎技术实战》第八章代码,用solr实现的搜索引擎。
tomcat7下实现solr4.3的部署与配置,配置中文服务器。通过solrj实现索引的创建,修改,删除,查询。并实现httpclienct创建、修改索引,检索信息等。
使用Solr为大数据库搭建搜索引擎,内容很好的,不忽悠
基于开源搜索引擎工具(如Heritrix +Lucence,或Nutch+Solr),搭建独立完整的搜索引擎测试平台。 2)垂直搜索行业信息:自主选择某一感兴趣行业,抓取相关行业内容。以抓取结果作为数据库,建立垂直搜索引擎,实现...
solr构建景点搜索引擎
Solr 5.3.1搜索引擎搭建详细教程(包括集成mysql、添加中文分词、添加同义词、查询语法等)
使用Solr为大数据库搭建搜索引擎,霍庆,刘培植,如今信息快速发展,数据库信息不断增多,尤其是对于中文信息,传统的数据库搜索方式(like%%)不仅效率低下,而且搜索速度极慢,此
lucene的应用.pdf 01solr企业级搜索引擎准备阶段.pdf 02solr企业级搜索引擎实战演练.pdf 适合新手搭建solr使用
基于solr的中文搜索引擎框架
solr搜索引擎,solr介绍什么是solr搜索引擎,操作实例
基于Solr的搜索引擎的设计与实现
高效的企业级搜索引擎Solr简介,快速学习Solr
更重要的是,Solr 创建的索引与 Lucene 搜索引擎库完全兼容。 通过对 Solr 进行适当的配置, 某些情况下可能需要进行编码,Solr 可以阅读和使用构建到其他 Lucene 应用程序中的索引。此 外,很多 Lucene 工具(如 Nutch、...