solr-7.1.0的java接口調用

 //指定solr服務器的地址  
    private final static String SOLR_URL = new String("http://localhost:8983/solr/");  

    /**
     * 創建SolrServer對象
     * 
     * 該對象有兩個可以使用,都是線程安全的  
     * 1、CommonsHttpSolrServer:啓動web服務器使用的,通過http請求的 
     * 2、 EmbeddedSolrServer:內嵌式的,導入solr的jar包就可以使用了  
     * 3、solr 4.0之後好像添加了不少東西,其中CommonsHttpSolrServer這個類改名爲HttpSolrClient
     * 
     * @return
     */
    public HttpSolrClient createSolrServer(){
        Builder builder = null;
        builder =new HttpSolrClient.Builder(SOLR_URL);
        HttpSolrClient  solr=builder.build();
        return solr;
    }

  
    /**
     * 往索引庫添加文檔
    * @throws IOException 
    * @throws SolrServerException 
     */
    public void addDoc() throws SolrServerException, IOException{
       //構造一篇文檔  
        SolrInputDocument document = new SolrInputDocument();  
        //往doc中添加字段,在客戶端這邊添加的字段必須在服務端中有過定義  
        document.addField("id", "8");  
        document.addField("name", "周新星");  
        document.addField("description", "一個灰常牛逼的軍事家");  
        //獲得一個solr服務端的請求,去提交  ,選擇具體的某一個solr core
        Builder builder = null;
        builder =new HttpSolrClient.Builder(SOLR_URL + "jcg");
        HttpSolrClient  solr=builder.build();
       solr.add(document);
       solr.commit();
       solr.close();
    }


    /** 
     * 根據id從索引庫刪除文檔 
     */
    public void deleteDocumentById() throws Exception {  
        //選擇具體的某一個solr core
    	  Builder builder = null;
          builder =new HttpSolrClient.Builder(SOLR_URL + "my_core");
          HttpSolrClient  server=builder.build();  
        //刪除文檔  
        server.deleteById("8");  
        //刪除所有的索引
        //solr.deleteByQuery("*:*");
        //提交修改  
        server.commit();  
        server.close();
    }  

    /**
     * 查詢
    * @throws Exception 
     */
    public void querySolr() throws Exception{
    	Builder builder = null;
        builder =new HttpSolrClient.Builder(SOLR_URL+"my_core/");
        HttpSolrClient  solrServer=builder.build();  
        SolrQuery query = new SolrQuery();  
        //下面設置solr查詢參數
        //query.set("q", "*:*");// 參數q  查詢所有   
        query.set("q","周星馳");//相關查詢,比如某條數據某個字段含有周、星、馳三個字  將會查詢出來 ,這個作用適用於聯想查詢

        //參數fq, 給query增加過濾查詢條件  
        query.addFilterQuery("id:[0 TO 9]");//id爲0-4  

        //給query增加布爾過濾條件  
        //query.addFilterQuery("description:演員");  //description字段中含有“演員”兩字的數據

        //參數df,給query設置默認搜索域  
        query.set("df", "name");  

        //參數sort,設置返回結果的排序規則  
        query.setSort("id",SolrQuery.ORDER.desc);

        //設置分頁參數  
        query.setStart(0);  
        query.setRows(10);//每一頁多少值  

        //參數hl,設置高亮  
        query.setHighlight(true);  
        //設置高亮的字段  
        query.addHighlightField("name");  
        //設置高亮的樣式  
        query.setHighlightSimplePre("<font color='red'>");  
        query.setHighlightSimplePost("</font>"); 

        //獲取查詢結果
        QueryResponse response = solrServer.query(query);  
        //兩種結果獲取:得到文檔集合或者實體對象

        //查詢得到文檔的集合  
        SolrDocumentList solrDocumentList = response.getResults();  
        System.out.println("通過文檔集合獲取查詢的結果"); 
        System.out.println("查詢結果的總數量:" + solrDocumentList.getNumFound());  
        //遍歷列表  
        for (SolrDocument doc : solrDocumentList) {
            System.out.println("id:"+doc.get("id")+"   name:"+doc.get("name")+"    description:"+doc.get("description"));
        } 

        //得到實體對象
        List<Person> tmpLists = response.getBeans(Person.class);
        if(tmpLists!=null && tmpLists.size()>0){
            System.out.println("通過文檔集合獲取查詢的結果"); 
            for(Person per:tmpLists){
                System.out.println("id:"+per.getId()+"   name:"+per.getName()+"    description:"+per.getDescription());
            }
        }
    }

    public static void main(String[] args) throws Exception {
//        solrtest solr = new solrtest();
//        solr.createSolrServer();
//        solr.addDoc();
//        solr.deleteDocumentById();
//        solr.querySolr();
   }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章