zoukankan      html  css  js  c++  java
  • java操作solr

    <dependency>
                <groupId>org.apache.solr</groupId>
                <artifactId>solr-solrj</artifactId>
                <version>8.1.0</version>
            </dependency>
    import org.apache.solr.client.solrj.SolrClient;
    import org.apache.solr.client.solrj.impl.HttpSolrClient;
    
    public class SolrClient {
    
        private volatile static SolrClient solrClient;
        private SolrClient(){}
        public static SolrClient getSolrClient(String url) {
            if (solrClient == null) {
                synchronized (MySolrClient.class) {
                    if (solrClient == null) {
                        solrClient = new HttpSolrClient.Builder(url)
                                .withConnectionTimeout(10*1000)
                                .withSocketTimeout(30*1000)
                                .build();
                    }
                }
            }
            return solrClient;
        }
    }
    /**
         * 添加或者更新solr库
         * 单条数据
         * @param document
         * @return
         */
        public static String addByDoc(SolrInputDocument document, String url) {
            SolrClient solrClient = MySolrClient.getSolrClient(url);
            String result = "success";
            try {
                solrClient.add(document);
                solrClient.commit();
                log.info("insert doc to solr success!");
            } catch (Exception e) {
                result = "failed";
                loggerErrToSolr(e);
            }
            return result;
        }
    
        /**
         * 添加或者更新solr库
         * 多条数据
         * @param docs
         * @return
         */
        public static String addByList(List<SolrInputDocument> docs, String url) {
            SolrClient solrClient = MySolrClient.getSolrClient(url);
            String result = "success";
            try {
                solrClient.add(docs);
                solrClient.commit();
                log.info("insert list to solr success!");
            } catch (Exception e) {
                result = "failed";
                loggerErrToSolr(e);
            }
            return result;
        }

    如果是更新已经存在的记录里面的某个字段,可以这样实现:

     SolrInputDocument updateDocument = new SolrInputDocument();
                updateDocument.addField("FEATUREID", requestAddLayerElementDisable.getLayerName() + "_" + requestAddLayerElementDisable.getElement());//类似于id
                Map<String, Object> operationMap = new HashMap<String, Object>();
                operationMap.put("set", false);//要设置的值
                updateDocument.addField("VISIBLE", operationMap);//要更新的字段
                SolrUtil.addByDoc(updateDocument, jmsConfig.getSolrUrl());
  • 相关阅读:
    NetworkX-根据权重画图
    Matplotlib 画廊
    NetworkX-画图
    NetworkX-simple graph
    python+networkx
    AttributeError: 'module' object has no attribute 'X509_up_ref'
    python Flask post 数据 输出
    windows环境下批处理实现守护进程
    supervisor自启动
    支持高并发的IIS Web服务器常用设置
  • 原文地址:https://www.cnblogs.com/james-roger/p/11059034.html
Copyright © 2011-2022 走看看