SolrJ 操作索引文档
来源:互联网 发布:h5投票系统源码 编辑:程序博客网 时间:2024/05/21 11:03
这是我在使用solr单机版服务器进行查询
服务器操作截图
java测试代码片段:
(添加索引文档)
public void testSoloJ() throws Exception{//得到SolrServer 通过HttpSolrServerSolrServer solrServer = new HttpSolrServer("http://10.117.17.110:8080/solr/collection1");//创建一个SolrDocumentSolrInputDocument document = new SolrInputDocument();document.addField("id", "test3");document.addField("item_title", "手机贴膜");document.addField("item_sell_point", "送钢化膜");//添加到服务器solrServer.add(document);//提交solrServer.commit();}
(删除索引文档)
public void testDeleteDocumentById() throws Exception{//创建solrserver对象SolrServer server = new HttpSolrServer("http://10.117.17.110:8080/solr/collection1");//通过id删除documentserver.deleteById("test1");//提交server.commit();}@Testpublic void testDeleteDocumentByQuery() throws Exception{//创建solrserver对象SolrServer server = new HttpSolrServer("http://10.117.17.110:8080/solr/collection1");//通过条件删除server.deleteByQuery("*:*");//提交server.commit();}
(查询索引文档高亮显示)
public void testQuery() throws Exception{// 1. 创建solrserver对象SolrServer solrServer = new HttpSolrServer("http://10.117.17.110:8080/solr/collection1");// 2. 创建solrQuery对象SolrQuery query = new SolrQuery();// 3. 设置查询条件query.setQuery("手机");// 4. 设置默认查询字段query.set("df", "item_keywords");// 5. 设置分页条件query.setStart(0);query.setRows(10);// 6. 设置高亮显示query.setHighlight(true);query.addHighlightField("item_title");query.setHighlightSimplePre("<div>");query.setHighlightSimplePost("</div");// 7. 执行查询,返回查询结果QueryResponse queryResponse = solrServer.query(query);// 8. 得到结果集SolrDocumentList solrDocumentList = queryResponse.getResults();// 9. 得到结果总记录数long numFound = solrDocumentList.getNumFound();System.out.println("总共查询到"+numFound+"条记录");// 10. 显示查询结果for (SolrDocument solrDocument : solrDocumentList) {System.out.println("id: "+solrDocument.getFieldValue("id"));// 11. 高亮显示标题Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();List<String> list = highlighting.get(solrDocument.getFieldValue("id")).get("item_title");// 12. 如果list不为空则标题中有查询的内容,否则在其他地方有查询的内容String title = "";if( list!=null && list.size()>0){title = list.get(0);} else {title = (String) solrDocument.getFieldValue("item_title");}System.out.println("item_title: " + title);System.out.println("item_sell_point: "+solrDocument.getFieldValue("item_sell_point"));System.out.println("item_price: "+solrDocument.getFieldValue("item_price"));System.out.println("item_image: "+solrDocument.getFieldValue("item_image"));System.out.println("item_category_name: "+solrDocument.getFieldValue("item_category_name"));System.out.println("分割线===================================");}}
阅读全文
1 0
- SolrJ 操作索引文档
- 使用solrj操作solr索引库
- solrJ对索引的删除操作
- 使用SolrJ管理索引库,并添加文档
- SolrJ提交索引
- solrj实现增量索引
- solrj 文件索引问题
- 使用SolrJ创建索引
- SolrJ 管理索引库
- 使用solrj操作solr
- solrj操作solr
- 用solrj操作solr
- solrj操作core
- 使用solrJ操作solr
- SolrJ的基本操作
- SolrJ的基本操作
- 使用Solrj管理Solr索引
- 使用Solrj管理Solr索引
- 数组中的逆序对Java实现
- NYOJ289
- 把python基本功搞扎实(3)
- Web应用程序的最佳实践
- 剑指Offer------从尾到头打印链表
- SolrJ 操作索引文档
- poj2155
- GAP
- UVA 532 Dungeon Master
- C++服务器开发资源收集
- 字符串p型编码
- okHttp基本使用
- Spring Batch 之 Spring Batch 简介
- 【HDU