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());
  • 相关阅读:
    火狐常用的插件
    sourceinsight技巧
    为sourceinsight添加makefile、kconfig、*.S文件支持
    如何在shell中打印出带颜色的字符?
    Linux shell tee指令学习
    【转载】dirs、pushd、popd指令
    【转载】SHELL字符串处理技巧(${}、##、%%)
    【转载】利用shell脚本获取一个文件的绝对路径readlink
    如何查看智能手机的IP地址
    SDK Manager中勾选项
  • 原文地址:https://www.cnblogs.com/james-roger/p/11059034.html
Copyright © 2011-2022 走看看