how2j.cn

下载区
文件名 文件大小
solr4j.rar 10m
步骤 1 : 先运行,看到效果,再学习   
步骤 2 : 模仿和排错   
步骤 3 : SolrUtil   
步骤 4 : TestSolr4j   

步骤 1 :

先运行,看到效果,再学习

edit edit
老规矩,先下载右上角的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。
运行TestSolr4j就可以看到在 修改之前使用关键字 鞭 能查询到一条结果,修改之后,查询的结果改变了。 删除之后,查询不到结果了
先运行,看到效果,再学习
在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。
模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。
采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。

推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。
这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来
这里提供了绿色安装和使用教程:diffmerge 下载和使用教程
SolrUtil提供一个对象的增加或者更新(都是同一个方法)

public static <T> boolean saveOrUpdate(T entity) throws SolrServerException, IOException {
DocumentObjectBinder binder = new DocumentObjectBinder();
SolrInputDocument doc = binder.toSolrInputDocument(entity);
client.add(doc);
client.commit();
return true;
}

根据id删除这个索引

public static boolean deleteById(String id) {
try {
client.deleteById(id);
client.commit();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
package how2java; import java.io.IOException; import java.util.List; 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.beans.DocumentObjectBinder; 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; import org.apache.solr.common.util.NamedList; public class SolrUtil { public static SolrClient client; private static String url; static { url = "http://localhost:8983/solr/how2java"; client = new HttpSolrClient.Builder(url).build(); } public static void queryHighlight(String keywords) throws SolrServerException, IOException { SolrQuery q = new SolrQuery(); //开始页数 q.setStart(0); //每页显示条数 q.setRows(10); // 设置查询关键字 q.setQuery(keywords); // 开启高亮 q.setHighlight(true); // 高亮字段 q.addHighlightField("name"); // 高亮单词的前缀 q.setHighlightSimplePre("<span style='color:red'>"); // 高亮单词的后缀 q.setHighlightSimplePost("</span>"); //摘要最长100个字符 q.setHighlightFragsize(100); //查询 QueryResponse query = client.query(q); //获取高亮字段name相应结果 NamedList<Object> response = query.getResponse(); NamedList<?> highlighting = (NamedList<?>) response.get("highlighting"); for (int i = 0; i < highlighting.size(); i++) { System.out.println(highlighting.getName(i) + ":" + highlighting.getVal(i)); } //获取查询结果 SolrDocumentList results = query.getResults(); for (SolrDocument result : results) { System.out.println(result.toString()); } } public static <T> boolean batchSaveOrUpdate(List<T> entities) throws SolrServerException, IOException { DocumentObjectBinder binder = new DocumentObjectBinder(); int total = entities.size(); int count=0; for (T t : entities) { SolrInputDocument doc = binder.toSolrInputDocument(t); client.add(doc); System.out.printf("添加数据到索引中,总共要添加 %d 条记录,当前添加第%d条 %n",total,++count); } client.commit(); return true; } public static QueryResponse query(String keywords,int startOfPage, int numberOfPage) throws SolrServerException, IOException { SolrQuery query = new SolrQuery(); query.setStart(startOfPage); query.setRows(numberOfPage); query.setQuery(keywords); QueryResponse rsp = client.query(query); return rsp; } public static <T> boolean saveOrUpdate(T entity) throws SolrServerException, IOException { DocumentObjectBinder binder = new DocumentObjectBinder(); SolrInputDocument doc = binder.toSolrInputDocument(entity); client.add(doc); client.commit(); return true; } public static boolean deleteById(String id) { try { client.deleteById(id); client.commit(); } catch (Exception e) { e.printStackTrace(); return false; } return true; } }
修改之前查询一次
修改之后查询一次
删除之后查询一次
观察修改和删除的效果
TestSolr4j
package how2java; import java.io.IOException; import java.util.Collection; import java.util.List; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocumentList; public class TestSolr4j { public static void main(String[] args) throws SolrServerException, IOException { String keyword = "name:鞭"; System.out.println("修改之前"); query(keyword); Product p = new Product(); p.setId(51173); p.setName("修改后的神鞭"); SolrUtil.saveOrUpdate(p); System.out.println("修改之后"); query(keyword); SolrUtil.deleteById("51173"); System.out.println("删除之后"); query(keyword); } private static void query(String keyword) throws SolrServerException, IOException { QueryResponse queryResponse = SolrUtil.query(keyword,0,10); SolrDocumentList documents= queryResponse.getResults(); System.out.println("累计找到的条数:"+documents.getNumFound()); if(!documents.isEmpty()){ Collection<String> fieldNames = documents.get(0).getFieldNames(); for (String fieldName : fieldNames) { System.out.print(fieldName+"\t"); } System.out.println(); } for (SolrDocument solrDocument : documents) { Collection<String> fieldNames= solrDocument.getFieldNames(); for (String fieldName : fieldNames) { System.out.print(solrDocument.get(fieldName)+"\t"); } System.out.println(); } } }


HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。


提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
关于 工具和中间件-搜索引擎技术-更新删除索引 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 982790551
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: https://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图