码迷,mamicode.com
首页 > 其他好文 > 详细

3.solr学习速成之索引添加 查询 删除

时间:2017-05-24 14:38:03      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:tac   dir   val   nts   tps   cti   gets   string   tar   

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();
    }
}

 

3.solr学习速成之索引添加 查询 删除

标签:tac   dir   val   nts   tps   cti   gets   string   tar   

原文地址:http://www.cnblogs.com/itliucheng/p/6898564.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!