zoukankan      html  css  js  c++  java
  • 3.solr学习速成之索引添加 查询 删除

    solrserver.java

    public class solrServer {
        private solrServer(){};
        final static String SOLR_URL = "http://localhost:8080/solr/test";
        private static HttpSolrClient server = null;
        public static HttpSolrClient getServer(){
            if(server == null){
                server = new HttpSolrClient(SOLR_URL);
                server.setDefaultMaxConnectionsPerHost(1000);
                server.setMaxTotalConnections(10000);
                server.setConnectionTimeout(60000);
                server.setSoTimeout(60000);
                server.setFollowRedirects(false);
                server.setAllowCompression(true);
            }
            return server;
        }
    }

    solrTest.java

    public class solrTest {
        public static void addIndex(){
            HttpSolrClient server = solrServer.getServer();
            SolrInputDocument doc = new SolrInputDocument();
            doc.addField("id", "1003");
            doc.addField("name_s", "wanglc3");
            doc.addField("score_i", "883");
            try {
                server.add(doc);
                server.commit();
            } catch (SolrServerException | IOException e) {
                e.printStackTrace();
            }
        }
        public static void delete(){
            HttpSolrClient server = solrServer.getServer();
            try {
                server.deleteById("1001");
                server.commit();
            } catch (SolrServerException | IOException e) {
                e.printStackTrace();
            }
        }
        public static void search(){
            HttpSolrClient server = solrServer.getServer();
            SolrQuery query = new SolrQuery();
            query.setQuery("*:*");
            query.setStart(0);
            query.setRows(5);
            QueryResponse queryResponse;
            try {
                queryResponse = server.query(query);
                SolrDocumentList list = queryResponse.getResults();
                System.out.println("num = "+list.getNumFound());
                for(int i=0;i<list.size();i++){
                    System.out.println(list.get(i).getFieldValue("name_s"));
                }
                
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        public static void main(String[] args) {
            //addIndex();
            //delete();
            search();
        }
    }
  • 相关阅读:
    docker进入交互界面
    FCN训练注意事项
    centos7 常用命令
    vim锁定,不能动
    爬虫三之beautifulsoup
    爬虫二之Requests
    爬虫一之基本操作
    MathType的配置问题;将word中的公式转换为mathtype格式失败,缺少OMML2MML.XSL
    神经网络实现Discuz验证码识别
    修改linux环境变量配置文件
  • 原文地址:https://www.cnblogs.com/itliucheng/p/6898564.html
Copyright © 2011-2022 走看看