Solr-SolrJ 更新和删除索引

  1.  SolrUtil
    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;
        }
    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;
        }
         
    }
  2.  TestSolr4j
    修改之前查询一次
    修改之后查询一次
    删除之后查询一次
    观察修改和删除的效果

    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();
                  
            }
        }
     
    }



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值