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());
  • 相关阅读:
    Socket 编程(二)
    【ASP.NET】:Ckeditor+Fckeditor的使用
    Socket编程(一):建立与客户端的连接并接受数据
    Linux基础系列:常用命令(3)
    linux 命令汇总
    Linux基础系列:常用命令(2)
    Linux基础系列:常用命令(1)
    计算机基础系列三:网络基础
    计算机基础系列二:计算机操作系统
    计算机基础系列一:计算机硬件
  • 原文地址:https://www.cnblogs.com/james-roger/p/11059034.html
Copyright © 2011-2022 走看看