solr增删改查,java工程

package com.baidu.test;

import java.io.IOException;

import javax.management.Query;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;

public class Solr {
    
    
    /**
     * 功能:添加功能
     * 作者:wangwang
     * 时间:2016-10-19 09:54:30
     * @throws IOException
     * @throws SolrServerException
     *
     *
     */
    @Test
    public void add() throws SolrServerException, IOException{
        
        //1.获取solr连接
        String urlString="http://localhost:8983/solr";
        SolrServer solrServer = new HttpSolrServer(urlString);
        
        SolrInputDocument solrInputDocument = new SolrInputDocument();
        solrInputDocument.addField("id", "4");
        solrInputDocument.addField("name", "柳杨");
        solrInputDocument.addField("description", "女的 ,活的");
        
        solrServer.add(solrInputDocument);
        solrServer.commit();
    }
    
    /**
     * 功能:删除功能
     * 作者:wangwang
     * 时间:2016-10-19 10:02:16
     * @throws IOException
     * @throws SolrServerException
     *
     */
    @Test
    public  void  delete() throws SolrServerException, IOException{
        
        //1.获取solr的连接
        String urlString ="http://localhost:8983/solr";
        SolrServer solrServer = new HttpSolrServer(urlString);
        
        solrServer.deleteById("1");
        
        solrServer.commit();
    }
    
    
   
    

    
    /**
     * 功能:修改功能
     * 作者:wangwang
     * 时间:2016-10-19 10:06:34
     * @throws IOException
     * @throws
     *  SolrServerException
     */
    
      @Test
      public void update() throws SolrServerException, IOException{
          
          //1.获取solr连接
          String urlString="http://localhost:8983/solr";
          SolrServer solrServer = new HttpSolrServer(urlString);
          SolrInputDocument solrInputDocument = new SolrInputDocument();
          solrInputDocument.addField("id", "2");
          solrInputDocument.addField("name", "小倩");
          solrInputDocument.addField("description", "是一个漂亮的女鬼!!");
          
          solrServer.add(solrInputDocument);
          
          solrServer.commit();
          
          
          
      }
    /**
     * 功能:分页+列表
     * 作者:wangwang
     * 时间:2016-10-19 10:47:25
     * @throws SolrServerException
     *
     *
     */
      @Test
      public void list() throws SolrServerException{
          
          //获取solr连接
          String urlString ="http://localhost:8983/solr";
          SolrServer solrServer = new HttpSolrServer(urlString);
          
          SolrQuery solrQuery = new SolrQuery();
          solrQuery.setStart(0);
          solrQuery.setRows(5);
          
          solrQuery.setQuery("*:*");
          
          QueryResponse queryResponse = solrServer.query(solrQuery);
          SolrDocumentList results = queryResponse.getResults();
          for (SolrDocument solrDocument : results) {
            System.out.println(solrDocument);
        }
          
        
            
        }
          
      }
      
      
       
      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值