SOLR 公共查询笔记

原创
09/29 15:34
阅读数 8
package com.qianlima.solr.qy.service.indexer.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.request.AbstractUpdateRequest;
import org.apache.solr.client.solrj.request.UpdateRequest;
import org.apache.solr.client.solrj.response.FieldStatsInfo;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

/**
 * @author liweihai
 * @date 2020/8/18 16:53
 * @desc
 */
@Slf4j
@Service
public class SolrCommonService {

    @Autowired
    protected SolrClient solrClient; // solr server

    public <T> List<T> queryList(String queryStr, String fields, int start, int rows, String sort, Class<T> tClass) {
        SolrQuery query = new SolrQuery();
        query.setQuery(queryStr);
        query.setFields(fields);
        query.setStart(start);
        query.setRows(rows);
        query.setSort(sort, SolrQuery.ORDER.asc);
        try {
            QueryResponse response = solrClient.query(query);
            List<T> stats = response.getBeans(tClass);
            return stats;
        } catch (SolrServerException e) {

            log.error("SolrServerException:{}", e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            log.error("Solr IOException:{}", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    public <T> List<T> queryList(String queryStr, String fields, int start, int rows, String sort, Class<T> tClass, SolrClient solrClient) {
        SolrQuery query = new SolrQuery();
        query.setQuery(queryStr);
        query.setFields(fields);
        query.setStart(start);
        query.setRows(rows);
        query.setSort(sort, SolrQuery.ORDER.asc);
        try {
            QueryResponse response = solrClient.query(query);
            List<T> stats = response.getBeans(tClass);
            return stats;
        } catch (SolrServerException e) {

            log.error("SolrServerException:{}", e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            log.error("Solr IOException:{}", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    public Long queryMaxId(String filed) {
        SolrQuery query = new SolrQuery();
        query.setQuery("*:*");
        query.setGetFieldStatistics(true);
        query.setRows(0);
        query.setGetFieldStatistics(filed);
        try {
            QueryResponse response = solrClient.query(query);
            Map<String, FieldStatsInfo> stats = response.getFieldStatsInfo();
            FieldStatsInfo info = stats.get(filed);
            long max = info.getMax() != null ? Double.valueOf(info.getMax().toString()).longValue() : 0L;
            return max;
        } catch (SolrServerException e) {

            log.error("SolrServerException:{}", e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            log.error("Solr IOException:{}", e.getMessage());
            e.printStackTrace();
        }
        return 0L;
    }

    public int addListDocument(List<SolrInputDocument> docs) {
        try {
            UpdateResponse res = solrClient.add(docs);
            solrClient.commit(true, false);
            return res.getStatus();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public int addListDocument(List<SolrInputDocument> docs, SolrClient solrClient) {
        try {
            UpdateResponse res = solrClient.add(docs);
            solrClient.commit(true, false);
            return res.getStatus();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public int addDocument(SolrInputDocument doc) {
        try {
            UpdateResponse res = solrClient.add(doc);
            solrClient.commit(true, false);
            return res.getStatus();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public int addBean(Object doc) {
        try {
            UpdateResponse res = solrClient.addBean(doc);
            solrClient.commit(true, false);
            return res.getStatus();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public int addListBeans(Collection<?> docs) {
        try {
            UpdateResponse res = solrClient.addBeans(docs);
            solrClient.commit(true, false);
            return res.getStatus();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public int deleteByIds(List<String> ids) {
        try {
            UpdateResponse res = solrClient.deleteById(ids);
            solrClient.commit(true, false);
            return res.getStatus();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public int deleteByIds(List<String> ids, SolrClient solrClient) {
        try {
            UpdateResponse res = solrClient.deleteById(ids);
            solrClient.commit(true, false);
            return res.getStatus();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public Long queryTotalCount(String query, SolrClient client) {

        SolrQuery solrQuery = new SolrQuery();
        solrQuery.setQuery(query);
        solrQuery.set("indent", "on");
        solrQuery.set("wt", "json");
        solrQuery.setRows(0);
        solrQuery.setStart(0);
        solrQuery.setParam("group", false);
        try {
            QueryResponse queryResponse = client.query(solrQuery, SolrRequest.METHOD.GET);
            return queryResponse.getResults().getNumFound();
        } catch (Exception e) {
            log.error("queryTotalCount--" + e.getStackTrace());
            e.printStackTrace();
        }
        return 0L;
    }

    public int updateDocument(SolrInputDocument document) {
        try {
            UpdateRequest updateRequest = new UpdateRequest();
            updateRequest.setAction(AbstractUpdateRequest.ACTION.COMMIT, false, false);
            updateRequest.add(document);
            UpdateResponse result = updateRequest.process(solrClient);
            return result.getStatus();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public int updateDocument(SolrInputDocument document, SolrClient solrClient) {
        if (document == null) {
            log.error("document ===null");
            return 1;
        }
        try {
            UpdateRequest updateRequest = new UpdateRequest();
            updateRequest.setAction(AbstractUpdateRequest.ACTION.COMMIT, false, false);
            updateRequest.add(document);
            UpdateResponse result = updateRequest.process(solrClient);
            return result.getStatus();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return -1;
    }




}
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部