使用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<String, Object> search(Page page, SolrQuery query, boolean isHight) throws Exception;
    /**
     * 统计
     * @Title: statisticData
     * @Description: 
     * @return
     * @throws Exception
     * @return: Map<String,Object>
     */
    Map<String, Object> statisticData(SolrQuery query) throws Exception;
    List<PageData> 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("<span class=\"" + Const.HIGHLIGHT_CLASS + "\">");
        query.setHighlightSimplePost("</span>");
        return query;
    }

    private List<PageData> gethighlighDataList(QueryResponse response) {
        List<PageData> dataList = Lists.newArrayList();
        // 获取高亮字段
        Map<String, Map<String, List<String>>> hightMap = response.getHighlighting();
        SolrDocumentList results = response.getResults();
        Iterator<SolrDocument> it = results.iterator();
        while (it.hasNext()) {
            SolrDocument document = it.next();
            String id = document.getFieldValue("id").toString();
            String title = document.getFieldValue("title").toString();
            List<String> titleHight = hightMap.get(id).get("title");
            List<String> 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<String, Object> search(Page page, SolrQuery query, boolean isHight) throws Exception {
        Map<String, Object> 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<PageData> 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<Entry<String, Object>> it = document.iterator();
        while (it.hasNext()) {
            Entry<String, Object> 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<PageData> datas = gethighlighDataList(response);
        LOGGER.info("查询参数:" + query + "用时:" + response.getQTime());
        return datas.get(0);
    }

    /**
     * solrdocuemnt转list
     *
     * @param result
     * @return
     * @Title: solrDocumentToList
     * @Description:
     * @return: List<PageData>
     */
    private List<PageData> solrDocumentToList(SolrDocumentList result) {
        List<PageData> dataList = new ArrayList<>();
        Iterator<SolrDocument> 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<String, Object> statisticData(SolrQuery query) throws Exception {
        Map<String, Object> dataMap = Maps.newHashMap();
        QueryResponse response = solr.query(query);
        List<FacetField> facetFields = response.getFacetFields();
        for (FacetField face : facetFields) {
            List<Count> counts = face.getValues();
            for (Count count : counts) {
                dataMap.put(count.getName(), count.getCount());
            }
        }
        return dataMap;
    }

    @Override
    public List<PageData> statisticData(PageData pd, SolrQuery query) throws Exception {
        List<PageData> dataList = Lists.newArrayList();
        QueryResponse response = solr.query(query);
        List<FacetField> facetFields = response.getFacetFields();
        for (FacetField face : facetFields) {
            List<Count> 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<String> 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<PageData> 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<String> ids) throws Exception;

    void delete(String id) throws Exception;

    void deleteAll() throws Exception;

    void update(PageData document) throws Exception;

    void write(List<PageData> 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<String> 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<PageData> 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<PageData> 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<String> 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<PageData> 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<String> 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<PageData> 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<String, Object> 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<String, Object> statisticData(SolrQuery query) throws Exception {
        return solrClusterSearch.statisticData(query);
    }

    public List<PageData> statisticData(PageData pd, SolrQuery query) throws Exception {
        return solrClusterSearch.statisticData(pd, query);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,830评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,992评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,875评论 0 331
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,837评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,734评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,091评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,550评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,217评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,368评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,298评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,350评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,027评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,623评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,706评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,940评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,349评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,936评论 2 341

推荐阅读更多精彩内容