我与solr--solrJ
Posted DASOU
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我与solr--solrJ相关的知识,希望对你有一定的参考价值。
SolrJ索引库:
solr提供的一个客户端操作框架,在文件/solr6.2/dist下面可以找到该jar包solrj.jar以及相关jar包,可以使用maven添加。
java使用solrJ如下:
@Service public class IntelligenceWordSolrDAOImpl implements IntelligenceWordSolrDAO { private static final String URL = Config.getString("config.solr.url.mycore"); /** * 获取solrService对象 * * @return */ private SolrClient getSolrService() { String urlString = "http://192.168.1.12:8080/solr/mycore"; SolrClient solr = new HttpSolrClient.Builder(urlString).build(); return solr; } /** * 在搜索器引擎中创建索引 * * @param intelligenceList */ public void add(List<Intelligence> intelligenceList) throws Exception { SolrClient solr = getSolrService(); List<SolrInputDocument> SolrInputDocumentList = Lists.newArrayList(); intelligenceList.forEach(intelligence -> SolrInputDocumentList.add(initProperty(intelligence))); solr.add(SolrInputDocumentList); solr.commit(); } /** * 查询数据 * * @param param 匹配的参数集合 * @return 文档的数量 * @throws Exception */ public Long query(String[] param, Integer limit) throws Exception { Integer branchId = LoginContext.getBranchId(); SolrClient solr = getSolrService(); SolrQuery query = new SolrQuery(); StringBuffer buffer = new StringBuffer(); for (int i = 0; i < param.length; i++) { if (i + 1 == param.length) { buffer.append("\\"" + param[i] + "\\""); } else { buffer.append("\\"" + param[i] + "\\"" + " OR "); } } //根据时间限制设置选定条件 DateTime dateTime = new DateTime(); dateTime.minusDays(limit); Date queryTime = dateTime.toDate(); String queryStr = "text:(" + buffer.toString() + ")"; query.add(queryStr); //时间限定 String limitStr = "collectTime:[" + queryTime + " TO *]"; String branchStr = "branchId:("+branchId+")"; query.add(limitStr); query.add(branchStr); // String allQuery = queryStr+" AND "+limitStr+" AND "+branchStr; //query.setQuery(allQuery); QueryResponse response = solr.query(query); //获取返回的数据 SolrDocumentList solrDocumentList = response.getResults(); return solrDocumentList.getNumFound(); } /** * 初始化 * * @param param * @return */ public SolrInputDocument initProperty(Intelligence param) { Assert.notNull(param, "param not be null"); SolrInputDocument document = new SolrInputDocument(); document.addField("id", param.getIntelligenceId()); document.addField("intelligenceId", param.getIntelligenceId()); document.addField("title", param.getTitle()); document.addField("content", param.getContent()); document.addField("collectTime", param.getCollectTime().getTime()); document.addField("branchId", param.getBranchId()); return document; } }
以上是关于我与solr--solrJ的主要内容,如果未能解决你的问题,请参考以下文章