Solr java客户端solrJ的CRUD操作
来源:互联网 发布:淘宝审核原图的标准 编辑:程序博客网 时间:2024/05/15 04:40
需要的jar包
代码
public class SolrTest { @After public void tearDown() throws Exception { } //添加索引 @Test public void testAdd() throws SolrServerException, IOException { //创建和solr服务器的连接 String url="http://localhost:8080/solr"; SolrServer solrServer=new HttpSolrServer(url); //创建一个文档对象 SolrInputDocument document = new SolrInputDocument(); document.addField("id","solr001"); document.addField("product_name", "魅族手机"); document.addField("product_price", 3000); solrServer.add(document); solrServer.commit(); } //根据id删除 @Test public void deleteDocumentById() throws Exception{ //创建和solr服务器的连接 String url="http://localhost:8080/solr"; SolrServer solrServer=new HttpSolrServer(url); solrServer.deleteById("solr001"); solrServer.commit(); } //删除全部 @Test public void deleteAll()throws Exception{ //创建和solr服务器的连接 String url="http://localhost:8080/solr"; SolrServer solrServer=new HttpSolrServer(url); solrServer.deleteByQuery("*:*"); solrServer.commit(); } //更新和添加一样 略 @Test public void testSearch() throws Exception{ //创建和solr服务器的连接 String url="http://localhost:8080/solr"; SolrServer solrServer=new HttpSolrServer(url); //创建查询对象 SolrQuery是SolrParams 的子类 SolrQuery solrQuery = new SolrQuery(); //solrQuery.set("q", "*:*"); //solrQuery.setQuery("*:*"); //设置默认搜索域 solrQuery.set("df", "product_name"); //设置查询条件 solrQuery.setQuery("手机"); //设置过滤条件 solrQuery.addFilterQuery("product_price:[100 TO 6600]"); //设置排序条件 solrQuery.setSort("product_price", ORDER.asc); //设置分页条件 solrQuery.setStart(0); solrQuery.setRows(10); //设置显示的域 solrQuery.setFields("id","product_name","product_price"); solrQuery.setHighlight(true); solrQuery.addHighlightField("product_name"); solrQuery.setHighlightSimplePre("<span style='color:red'>"); solrQuery.setHighlightSimplePost("</span>"); //执行查询 QueryResponse queryResponse = solrServer.query(solrQuery); //取文档列表 SolrDocumentList results = queryResponse.getResults(); System.out.println("查询结果总量:"+results.getNumFound()); //遍历查询结果 for (SolrDocument solrDocument : results) { System.out.println(solrDocument.get("id")); System.out.println(solrDocument.get("product_name")); System.out.println(solrDocument.get("product_price")); System.out.println(solrDocument.get("product_catalog_name")); System.out.println(solrDocument.get("product_picture")); Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting(); //key 域名 Map<String, List<String>> map = highlighting.get(solrDocument.get("id")); List<String> list = map.get("product_name"); System.out.println(list.get(0)); System.out.println("--------------"); } }}
阅读全文
0 0
- Solr java客户端solrJ的CRUD操作
- SolrJ是操作Solr的JAVA客户端
- Solr JAVA客户端SolrJ的使用
- Solr Java客户端(SolrJ)
- solr的客户端调用solrj
- Solr客户端 SolrJ单机 添加查询操作
- solrj 对solr 的操作
- Solr之客户端SolrJ
- Solr java客户端的操作
- 利用solrj操作solr的API
- 利用solrj操作solr的API
- 使用solrj操作solr
- solrj操作solr
- 用solrj操作solr
- 使用solrJ操作solr
- Solr JAVA客户端SolrJ 4.9使用示例教程
- Solr JAVA客户端SolrJ 4.9使用示例教程
- 搜索引擎-基于solrj客户端的solr增删改查
- Java设计模式(三)--单例模式
- 事件模型--冒泡与捕获
- 设计模式(22)--模板方法模式
- Servlet地址书学
- 集合的排序,COmparable和Comparator的区别
- Solr java客户端solrJ的CRUD操作
- 浅谈Android事件分发机制
- SpringMVC学习系列(4) 之 数据绑定-1
- Java 多线程-java.util.concurrent锁对象
- D.6三角公式求值
- 8.6 A
- leetcode(121). Best Time to Buy and Sell Stock
- mxgraph 搭建简单应用 主线描述、属性与方法的意思
- python 生成200个激活码并存进数据库