使用solrj连接solr集群进行操作

1.solr配置文件

solr.url=http://192.168.0.5:8983/solr/collection1
solr.timeout=10000
solr.maxconnection=100
solr.queuesize=20
solr.zookeeper.url=192.168.0.5:2181

2.solr配置

@Configuration(value = "solrIndexFactory")
public class SolrIndexFactory {
    private static String url;
    private static String zookeeperUrl;
    private static String timeout;
    private static String maxConnection;
    private static String queueSize;
    private ConcurrentUpdateSolrClient concurrentUpdateSolrClient;
    private static final String DEFAULT_COLLECTION = "collection1";

    static {
        ResourceBundle resource = ResourceBundle.getBundle("solrindex");
        url = resource.getString("solr.url");
        timeout = resource.getString("solr.timeout");
        maxConnection = resource.getString("solr.maxconnection");
        queueSize = resource.getString("solr.queuesize");
        zookeeperUrl = resource.getString("solr.zookeeper.url");
    }

    /**
     * @return
     * @Title: getConcurrentUpdateSolrClient
     * @Description:
     * @return: ConcurrentUpdateSolrClient
     */
    @Scope
    @Bean
    public ConcurrentUpdateSolrClient getConcurrentUpdateSolrClient() {
        concurrentUpdateSolrClient = new ConcurrentUpdateSolrClient.Builder(url)
                .withQueueSize(Integer.parseInt(queueSize)).build();
        concurrentUpdateSolrClient.setParser(new XMLResponseParser());
        concurrentUpdateSolrClient.setConnectionTimeout(Integer.parseInt(timeout));
        concurrentUpdateSolrClient.setRequestWriter(new BinaryRequestWriter());
        return concurrentUpdateSolrClient;
    }

    @Scope
    @Bean
    public CloudSolrClient getCloudSolrClient() {
        CloudSolrClient client = new CloudSolrClient.Builder()
                .withZkHost(zookeeperUrl).build();
        client.setDefaultCollection(DEFAULT_COLLECTION);
        client.setParser(new XMLResponseParser());
        client.setRequestWriter(new BinaryRequestWriter());
        return client;
    }

3.solr查询接口--做两个接口

public interface IndexSearch {
    
    /**
     * 根据id查询内容
     * 
     * @Title: search
     * @Description:
     * @param id
     * @return
     * @throws Exception
     * @return: Object
     */
    Object search(String id) throws Exception;
}
public interface ISolrIndexSearch extends IndexSearch {

    SolrQuery getQuery() throws Exception;
    Map search(Page page, SolrQuery query, boolean isHight) throws Exception;
    /**
     * 统计
     * @Title: statisticData
     * @Description: 
     * @return
     * @throws Exception
     * @return: Map
     */
    Map statisticData(SolrQuery query) throws Exception;
    List statisticData(PageData pd, SolrQuery query) throws Exception;
}

3.使用solr查询数据

@Component(value = "solrIndexSearch")
public class SolrIndexSearch implements ISolrIndexSearch {
    private static final Logger LOGGER = Logger.getLogger(SolrIndexSearch.class);

    @Resource
    private CloudSolrClient solr;

    private SolrQuery setHighlightHandle(SolrQuery query) {
        query.setHighlight(true);
        query.addHighlightField("title");
        query.addHighlightField("text");
        query.setHighlightSimplePre("");
        query.setHighlightSimplePost("");
        return query;
    }

    private List gethighlighDataList(QueryResponse response) {
        List dataList = Lists.newArrayList();
        // 获取高亮字段
        Map>> hightMap = response.getHighlighting();
        SolrDocumentList results = response.getResults();
        Iterator it = results.iterator();
        while (it.hasNext()) {
            SolrDocument document = it.next();
            String id = document.getFieldValue("id").toString();
            String title = document.getFieldValue("title").toString();
            List titleHight = hightMap.get(id).get("title");
            List textHight = hightMap.get(id).get("text");
            if (titleHight != null && titleHight.size() != 0) {
                document.setField("title", titleHight.get(0));
            }
            if (textHight != null && textHight.size() != 0) {
                document.setField("text", textHight.get(0));
            }

            PageData pds = solrDocumentToPageData(document);
            dataList.add(pds);
        }
        return dataList;
    }

    /**
     * 逻辑层传入查询条件
     *
     * @param search
     * @param query
     * @param isHight
     * @return
     * @throws Exception
     * @Title: search
     * @Description:
     * @see com.jianong.util.indexes.ISolrIndexSearch#search(com.jianong.entity.SolrDocumentParameter,
     * org.apache.solr.client.solrj.SolrQuery, boolean)
     */
    @Override
    public Map search(Page page, SolrQuery query, boolean isHight) throws Exception {
        Map dataMap = new HashMap<>();
        PageData pd = page.getPd();
        int showCount = 0;
        int currentPage = 0;
        int totalResult = 0;

        if (null != pd.getString("currentPage")) {
            currentPage = Integer.parseInt(pd.getString("currentPage"));
        }
        if (currentPage == 0) {
            currentPage = 1;
        }
        if (null != pd.getString("showCount")) {
            showCount = Integer.parseInt(pd.getString("showCount"));
        } else {
            showCount = Const.SHOW_COUNT;
        }
        query.setStart(showCount * (currentPage - 1)).setRows(showCount);
        List dataList = Lists.newArrayList();

        if (isHight) {
            query = setHighlightHandle(query);
            QueryResponse response = solr.query(query);
            dataList = gethighlighDataList(response);
            totalResult = (int) response.getResults().getNumFound();
            LOGGER.info("查询参数:" + query + "共查询到文档:" + totalResult + "个" + "用时:" + response.getQTime());
        } else {
            QueryResponse response = solr.query(query);
            SolrDocumentList results = response.getResults();
            dataList = solrDocumentToList(results);
            totalResult = (int) results.getNumFound();
            LOGGER.info("查询参数:" + query + "共查询到文档:" + totalResult + "个" + "用时:" + response.getQTime());
        }

        dataMap.put("dataList", dataList);
        // 构造page
        page.setCurrentPage(currentPage);
        page.setShowCount(showCount);
        page.setTotalResult(totalResult);
        dataMap.put("page", makePage(page));
        return dataMap;
    }

    private Page makePage(Page page) {
        page.getTotalPage();
        page.setEntityOrField(true);
        page.getPageStr();
        return page;
    }

    private PageData solrDocumentToPageData(SolrDocument document) {
        PageData pd = new PageData();
        Iterator> it = document.iterator();
        while (it.hasNext()) {
            Entry entry = it.next();
            // 时间
            if (entry.getKey().equals("releasedate")) {
                pd.put(entry.getKey(), DateUtil.getDateTimeFromTimeStrap(Long.parseLong(entry.getValue().toString())));
            } else {
                pd.put(entry.getKey(), entry.getValue());
            }
        }
        return pd;
    }

    /**
     * 根据id查询数据
     *
     * @param id
     * @return
     * @throws Exception
     * @Title: search
     * @Description:
     * @see com.jianong.util.indexes.ISolrIndexSearch#search(java.lang.String)
     */
    @Override
    public PageData search(String id) throws Exception {
        PageData pd = new PageData();
        SolrQuery query = new SolrQuery();
        query.setQuery(SolrStatementUtils.generateBaseMatchStatement("id", id));
        query = setHighlightHandle(query);
        QueryResponse response = solr.query(query);
        List datas = gethighlighDataList(response);
        LOGGER.info("查询参数:" + query + "用时:" + response.getQTime());
        return datas.get(0);
    }

    /**
     * solrdocuemnt转list
     *
     * @param result
     * @return
     * @Title: solrDocumentToList
     * @Description:
     * @return: List
     */
    private List solrDocumentToList(SolrDocumentList result) {
        List dataList = new ArrayList<>();
        Iterator it = result.iterator();
        while (it.hasNext()) {
            SolrDocument solrDocument = (SolrDocument) it.next();
            PageData pd = new PageData();
            for (String key : solrDocument.keySet()) {
                if (key.equals("releasedate")) {
                    pd.put(key, DateUtil.getDateTimeFromTimeStrap(Long.parseLong(solrDocument.get(key).toString())));
                } else {
                    pd.put(key, solrDocument.get(key));
                }
            }
            dataList.add(pd);
        }
        return dataList;
    }

    @Override
    public SolrQuery getQuery() throws Exception {
        return new SolrQuery();
    }

    /**
     * 统计
     *
     * @param query
     * @return
     * @throws Exception
     * @Title: statisticData
     * @Description:
     * @see com.jianong.util.indexes.ISolrIndexSearch#statisticData(org.apache.solr.client.solrj.SolrQuery)
     */
    @Override
    public Map statisticData(SolrQuery query) throws Exception {
        Map dataMap = Maps.newHashMap();
        QueryResponse response = solr.query(query);
        List facetFields = response.getFacetFields();
        for (FacetField face : facetFields) {
            List counts = face.getValues();
            for (Count count : counts) {
                dataMap.put(count.getName(), count.getCount());
            }
        }
        return dataMap;
    }

    @Override
    public List statisticData(PageData pd, SolrQuery query) throws Exception {
        List dataList = Lists.newArrayList();
        QueryResponse response = solr.query(query);
        List facetFields = response.getFacetFields();
        for (FacetField face : facetFields) {
            List counts = face.getValues();
            for (Count count : counts) {
                PageData pds = new PageData();
                pds.put("name", count.getName());
                pds.put("value", count.getCount());
                dataList.add(pds);
            }
        }
        return dataList;
    }
}

4.封装solrj集群方式查询索引

@Component(value = "solrClusterIndexWriter")
public class SolrClusterIndexWriter implements IndexeWriter {

    @Resource(name = "solrIndexWriter")
    private IndexeWriter indexeWriter;

    @Override
    public void open() {
        indexeWriter.open();
    }

    @Override
    public void delete(List ids) throws Exception {
        indexeWriter.delete(ids);
    }

    @Override
    public void delete(String id) throws Exception {
        indexeWriter.delete(id);
    }

    @Override
    public void deleteAll() throws Exception {
        indexeWriter.deleteAll();
    }

    @Override
    public void update(PageData document) throws Exception {
        indexeWriter.update(document);
    }

    @Override
    public void write(List document) throws Exception {
        indexeWriter.write(document);
    }

    @Override
    public void write(PageData pd) throws Exception {
        indexeWriter.write(pd);
    }

    @Override
    public void createIndex(String beginTime) throws Exception {
    }

    @Override
    public void commit() {
        indexeWriter.commit();
    }

    @Override
    public void close() {
        indexeWriter.close();
    }
}

5.创建索引增删改的接口

public interface IndexeWriter {

    void delete(List ids) throws Exception;

    void delete(String id) throws Exception;

    void deleteAll() throws Exception;

    void update(PageData document) throws Exception;

    void write(List document) throws Exception;

    void write(PageData pd) throws Exception;

    /**
     * 创建索引
     * 
     * @Title: createIndex
     * @Description:
     * @throws Exception
     * @return: void
     */
    void createIndex(String beginTime) throws Exception;

    void commit();

    void close();

    /**
     * 提交操作释放链接
     */
    void detory();
}

6.索引增删改实现类

@Component(value = "solrIndexWriter")
public class SolrIndexWriter implements IndexeWriter {
    private static final Logger LOGGER = Logger.getLogger(SolrIndexWriter.class);

    @Resource
    private ConcurrentUpdateSolrClient solr;

    @Override
    public void delete(List ids) throws Exception {
        solr.deleteById(ids);
        solr.commit();
    }

    @Override
    public void delete(String id) throws Exception {
        UpdateResponse response = solr.deleteById(id);
        solr.commit();
        LOGGER.info("删除文档id:" + id + "用时:" + response.getQTime() + "状态:" + response.getStatus());
    }

    public void write(List document) throws Exception {
        UpdateResponse response = null;
        for (PageData pd : document) {
            SolrInputDocument d = new SolrInputDocument();
            Iterator it = pd.keySet().iterator();
            while (it.hasNext()) {
                String key = (String) it.next();
                if (!Objects.equals(key, "_version_")) {
                    d.setField(key, pd.get(key));
                }
            }
            response = solr.add(d);
        }
        commit();
        LOGGER.info("写入文档个数:" + document.size() + "用时:" + response.getQTime() + "状态:" + response.getStatus());
    }

    @Override
    public void write(PageData pd) throws Exception {
        SolrInputDocument d = new SolrInputDocument();
        Iterator it = pd.keySet().iterator();
        while (it.hasNext()) {
            String key = (String) it.next();
            if (!Objects.equals(key, "_version_")) {
                d.setField(key, pd.get(key));
            }
        }
        UpdateRequest request = new UpdateRequest();
        request.setAction(ACTION.COMMIT, false, false);
        request.add(d);
        UpdateResponse response = request.process(solr);
    }

    @Override
    public void commit() {
        try {
            solr.commit();
        } catch (SolrServerException | IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void close() {
        try {
            solr.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void update(PageData document) throws Exception {
        List pds = new ArrayList<>(1);
        pds.add(document);
        write(pds);
    }

    @Override
    public void deleteAll() throws Exception {
        UpdateResponse response = solr.deleteByQuery("*:*");
        solr.commit();
        LOGGER.info("删除全部文档" + response.getStatus() + "用时:" + response.getQTime());
    }

    /**
     * 传入开始时间创建索引
     *
     * @param beginTime
     * @throws Exception
     * @Title: createIndex
     * @Description:
     * @see com.jianong.util.indexes.IndexeWriter#createIndex(java.lang.String)
     */
    @Override
    public void createIndex(String beginTime) throws Exception {

    }

    @Override
    public void detory() {
        commit();
        close();
    }
}

7.增删改的solr集群实现类

@Component(value = "solrClusterIndexWriter")
public class SolrClusterIndexWriter implements IndexeWriter {

    @Resource(name = "solrIndexWriter")
    private IndexeWriter indexeWriter;

    @Override
    public void delete(List ids) throws Exception {
        indexeWriter.delete(ids);
    }

    @Override
    public void delete(String id) throws Exception {
        indexeWriter.delete(id);
    }

    @Override
    public void deleteAll() throws Exception {
        indexeWriter.deleteAll();
    }

    @Override
    public void update(PageData document) throws Exception {
        indexeWriter.update(document);
    }

    @Override
    public void write(List document) throws Exception {
        indexeWriter.write(document);
    }

    @Override
    public void write(PageData pd) throws Exception {
        indexeWriter.write(pd);
    }

    @Override
    public void createIndex(String beginTime) throws Exception {

    }

    @Override
    public void commit() {
        indexeWriter.commit();
    }

    @Override
    public void close() {
        indexeWriter.close();
    }

    @Override
    public void detory() {
        indexeWriter.detory();
    }
}

8.外部操作接口--用于外部调用

@Component(value = "solrManager")
public class SolrManager {

    @Resource(name = "solrClusterSearch")
    private SolrClusterSearch solrClusterSearch;

    @Resource(name = "solrIndexWriter")
    private IndexeWriter indexeWriter;

    /**
     * 删除索引
     *
     * @param ids
     * @throws Exception
     */
    public void delete(List ids) throws Exception {
        indexeWriter.delete(ids);
    }

    public void delete(String id) throws Exception {
        indexeWriter.delete(id);
    }

    public void deleteAll() throws Exception {
        indexeWriter.deleteAll();
    }

    public void update(PageData document) throws Exception {
        indexeWriter.update(document);
    }

    public void write(List document) throws Exception {
        indexeWriter.write(document);
    }

    public void write(PageData pd) throws Exception {
        indexeWriter.write(pd);
    }

    /**
     * 创建索引
     *
     * @throws Exception
     * @Title: createIndex
     * @Description:
     * @return: void
     */
    public void createIndex(String beginTime) throws Exception {

    }

    /**
     * 查询
     *
     * @param page
     * @param query
     * @param isHight
     * @return
     * @throws Exception
     */
    public Map search(Page page, SolrQuery query, boolean isHight) throws Exception {
        return solrClusterSearch.search(page, query, isHight);
    }

    public PageData search(String id) throws Exception {
        return solrClusterSearch.search(id);
    }

    public Map statisticData(SolrQuery query) throws Exception {
        return solrClusterSearch.statisticData(query);
    }

    public List statisticData(PageData pd, SolrQuery query) throws Exception {
        return solrClusterSearch.statisticData(pd, query);
    }
}

你可能感兴趣的:(使用solrj连接solr集群进行操作)