java集成solr

一、安装solr

https://lucene.apache.org/solr/

下载之后解压,cmd访问bin路径 输入 solr start启动solr服务,访问:http://127.0.0.1:8983

创建core

创建core前,要事先再solr目录下创建一个core的文件夹,定义为自己想要的命名,定义路径:solr\server\solr

将solr\server\solr\configsets\basic_configs下的内容复制到已创建的目录下,修改managed-schema,增加想存入solr库中的字段

创建完以上文件及文件夹之后就可以在solr后台创建core了,core的名称与你创建的文件夹名称要一致。

二、java项目集成

pom依赖

<dependency>
  <groupId>org.apache.solr</groupId>
  <artifactId>solr-solrj</artifactId>
  <version>6.6.0</version>
</dependency>

创建solr管理类

public class SolrManager {
   
   private String solorUrl;
   
   public SolrManager(){
      this.solorUrl = Global.getConfig("solr.url");
   }
   
   public SolrManager(String solorUrl){
      this.solorUrl = solorUrl;
   }
   
   public String getSolorUrl() {
      return solorUrl;
   }

   public void setSolorUrl(String solorUrl) {
      this.solorUrl = solorUrl;
   }

   /**
    * 根据id删除文档 从索引库删除文档
    */

   public void deleteDocument(List<String> idList) throws Exception {
   
      SolrClient solrClient = new HttpSolrClient.Builder(solorUrl).build();
      // 删除文档
      solrClient.deleteById(idList);
      // 提交修改
      solrClient.commit();
      release(solrClient);
   }

   public void deleteDocById(String id) {
      SolrClient solrClient = new HttpSolrClient.Builder(solorUrl).build();
      if(solrClient != null){
         try {
            UpdateResponse response = solrClient.deleteById(id);
            solrClient.commit();
         } catch (Exception e) {
            e.printStackTrace();
         }
      }
      release(solrClient);
   }

   public void release(SolrClient solrClient){
      try {
         solrClient.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

}

添加

public class SolrDocumentAdder extends SolrManager {

   private SolrClient solrClient;

   public void add(List<Map> rows) throws SolrServerException, IOException {
      if(rows == null || rows.size() == 0)
         return;
      ArrayList<SolrInputDocument> docList = new ArrayList<SolrInputDocument>();
      for(Map row :rows){
         Set<Entry> set = row.entrySet();
         SolrInputDocument document = new SolrInputDocument();
         for(Entry e : set){
            if(e.getValue() != null){
               document.addField((String) e.getKey(), e.getValue());
            }
         }
         docList.add(document);
      }
      
      solrClient = getSolrClient();
      if(solrClient != null){
         UpdateResponse response = solrClient.add(docList);
         
         solrClient.commit();
      }
   }

   protected SolrClient getSolrClient(){
      if(solrClient == null)
         solrClient = new HttpSolrClient.Builder(this.getSolorUrl()).build();
      return solrClient;
   }
   
   public void release(){
      if(solrClient != null){
         release(solrClient);
         solrClient=null;
      }
   }
}

查询

public class SolrDocumentQueryer extends SolrManager {

   static final String QUERY_FIELD_PART = "tmpl_data_field";
   
   public SolrDocumentQueryer(){
      super();
   }
   
   public SolrDocumentQueryer(String solrUrl){
      super(solrUrl);
   }
   
   /**
    * 对索引库中的文档进行更新
    * 
    * @throws SolrServerException
    */
   public QueryResponse query(String inputWord, int start, int size)   {
      
      SolrClient solrClient = new HttpSolrClient.Builder(this.getSolorUrl()).build();
      SolrQuery query = new SolrQuery();
      // 给query设置一个主查询条件
      // query.set("q", "*:*");//查询所有
      query.set("q", inputWord);

      // 给query增加过滤查询条件
//    query.addFilterQuery("product_price:[0 TO 200]");
      // 给query增加布尔过滤条件
      // query.addFilterQuery("-product_name:台灯");
      // 给query设置默认搜索域
      query.set("df", QUERY_FIELD_PART); 
      
      // 设置分页参数
      query.setStart(start);
      query.setRows(size);// 每一页多少值

      QueryResponse response = null;
      try {
         response = solrClient.query(query);

      } catch (Exception e) {
         e.printStackTrace();
      }finally{
         release(solrClient);
      }
      return response;
   }
   
   public QueryResponse query(String inputWord, List<String> queryFilters, int start, int size, Map<String, String> rawQueryParameters)   {
      
      SolrClient solrClient = new HttpSolrClient.Builder(this.getSolorUrl()).build();
      SolrQuery query = new SolrQuery();
      // 给query设置一个主查询条件
      query.set("q", inputWord);

      // 给query增加过滤查询条件
      if(queryFilters != null && queryFilters.size()>0){
         for(String qf: queryFilters){
            query.addFilterQuery(qf);
         }
      }
      query.set("df", QUERY_FIELD_PART); 
      query.setStart(start*size);
      query.setRows(size);
      if(rawQueryParameters != null && rawQueryParameters.size() > 0){
         Set<String> rqps = rawQueryParameters.keySet();
         for(String k : rqps){
            query.set(k, rawQueryParameters.get(k));
         }
      }
      QueryResponse response = null;
      try {
         response = solrClient.query(query);
      } catch (Exception e) {
         e.printStackTrace();
      }finally{
         release(solrClient);
      }
      return response;
   }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值