自己封装的solr方法

package com.employment.common.utils;


import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.MapSolrParams;
import org.apache.solr.common.params.SolrParams;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SolrClientUtils {
    private final static  String url = "http://localhost:8080/solr";

    /**
     * 获取SolrClient
     * solrj 6.5及以后版本获取方式
     * @return
     */
    public static HttpSolrClient getSolrClient(){
        /*
         * 设置超时时间
         * .withConnectionTimeout(10000)
         * .withSocketTimeout(60000)
         */
        return new HttpSolrClient.Builder(url)
                .withConnectionTimeout(10000)
                .withSocketTimeout(60000)
                .build();
    }

    /**
     *
     * @param core  solr中core名字
     * @param query  条件  字段名:搜索内容 AND 段名:搜索内容  多个字段用AND连接
     * @return
     * @throws IOException
     * @throws SolrServerException
     */
    public static SolrDocumentList testQuery(String core, String query) throws IOException, SolrServerException {
        HttpSolrClient solrClient = getSolrClient();
        // 定义查询条件
        Map<String, String> params = new HashMap<String, String>();
        params.put("q", query);
        SolrParams mapSolrParams = new MapSolrParams(params);
        //执行查询 第一个参数是collection,就是我们在solr中创建的core
        QueryResponse response = solrClient.query(core, mapSolrParams);
        // 获取结果集
        SolrDocumentList results = response.getResults();
        for (SolrDocument result : results) {
            // SolrDocument 数据结构为Map
            System.out.println(result);
        }
        return results;
    }

    /**
     * 根据id删除数据
     * @param core solr中core名字
     * @param id   数据id
     * @throws IOException
     * @throws SolrServerException
     */
    public static void deleteById(String core, String id) throws IOException, SolrServerException {
        HttpSolrClient solrClient = getSolrClient();
        UpdateResponse updateResponse = solrClient.deleteById(core, id);
        solrClient.commit(core);
    }

    /**
     * 单个数据 实体类添加
     * @param core solr中core名字
     * @param object 实体类对象
     * @throws IOException
     * @throws SolrServerException
     */
    public static void addObject(String core, Object object) throws IOException, SolrServerException {
        HttpSolrClient solrClient = getSolrClient();
        UpdateResponse updateResponse = solrClient.addBean(core,object);
        solrClient.commit(core);
    }


    /**
     * 单个数据 map类型添加
     * @param core  solr中core名字
     * @param map   要添加的map对象
     * @throws IOException
     * @throws SolrServerException
     */
    public static void addMap(String core, Map<String, Object> map) throws IOException, SolrServerException {
        HttpSolrClient solrClient = getSolrClient();
        SolrInputDocument solrInputFields = new SolrInputDocument();
        for (Map.Entry<String, Object> entry : map.entrySet()){
            solrInputFields.addField(entry.getKey(),entry.getValue());
        }
        UpdateResponse add = solrClient.add(core, solrInputFields);
        solrClient.commit(core);
    }

    /**
     * 根据id批量删除
     * @param core solr中core名字
     * @param ids id集合
     * @throws IOException
     * @throws SolrServerException
     */
    public static void  deleteList(String core, List<String> ids) throws IOException, SolrServerException {

        HttpSolrClient solrClient = getSolrClient();
        UpdateResponse updateResponse = solrClient.deleteById(core, ids);
        solrClient.commit(core);
        System.out.println(updateResponse);
    }

}

pom文件

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值