solrJ的增删改查,solrDocument与Bean的映射,直接增删该查bean完成对索引的操作

 

package com.he.mysolr;

import org.apache.solr.client.solrj.impl.HttpSolrClient;

/**
 * Hello world!
 *
 */
public class App {
    private static HttpSolrClient server = null;
    private static String url ="http://localhost:8080/solr/new_core";
    
    public static HttpSolrClient getServer()
    {
        if(server == null){
            //new HttpSolrClient(url)这种搞法已经被废弃了
            server = new HttpSolrClient.Builder(url).build();
            server.setDefaultMaxConnectionsPerHost(1000); 
            server.setMaxTotalConnections(10000);
            server.setConnectionTimeout(60000);//设置连接超时时间(单位毫秒) 1000
            server.setSoTimeout(60000);//// 设置读数据超时时间(单位毫秒) 1000
            server.setFollowRedirects(false);//遵循从定向
            server.setAllowCompression(true);//允许压缩
        }
        return server;
    }
    
    public static void main( String[] args ){
        System.out.println( getServer() );
    }
}
package com.he.mysolr;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
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;

public class SolrTest {
    
    public static void addIndex() {
        HttpSolrClient client = App.getServer();
        Collection<SolrInputDocument> c = new ArrayList<SolrInputDocument>();
        
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("id", "1001");
        doc.addField("name_s", "liming");
        doc.addField("score_i", "98");
        c.add(doc);
        SolrInputDocument doc1 = new SolrInputDocument();
        doc1.addField("id", "1002");
        doc1.addField("name_s", "wangpeng");
        doc1.addField("score_i", "34");
        c.add(doc1);
        SolrInputDocument doc3 = new SolrInputDocument();
        doc3.addField("id", "1003");
        doc3.addField("name_s", "dongdong");
        doc3.addField("score_i", "67");
        c.add(doc3);
        try {
            client.add(c);
            client.commit();
        } catch (SolrServerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    
    public static  void  searchIndex() {
        HttpSolrClient client = App.getServer();
        SolrQuery sQuery = new SolrQuery();
        sQuery.setQuery("*:*");
        sQuery.setStart(0);
        sQuery.setRows(5);
        QueryResponse queryResponse=null;
        try {
            queryResponse = client.query(sQuery);
            SolrDocumentList  list = queryResponse.getResults();
            for (SolrDocument solrDocument : list) {
                printSolrDOC(solrDocument);
            }
        } catch (SolrServerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    
    public static void  printSolrDOC(SolrDocument solrDocument){
        Map<String, Object> fieldValueMap = solrDocument.getFieldValueMap();
        Set<String> set = fieldValueMap.keySet();
        for (String string : set) {
            System.out.println(string+":"+fieldValueMap.get(string));
        }
    }
    
    public static void main(String[] args) {
        searchIndex();
    }

}
package com.he.mysolr;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;

public class SolrBean {
    
    public static void addIndex() {
        HttpSolrClient client = App.getServer();
        List list = new ArrayList();
        TestBO bean = new TestBO();
        bean.setId("12131232213");
        bean.setName_s("hzzhzz");
        bean.setScore_i(100);
        list.add(bean);
        try {
            client.addBeans(list);
            client.commit();
        } catch (SolrServerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public static void delete(String condition) {
        HttpSolrClient client = App.getServer();
        try {
            client.deleteByQuery(condition);
            client.commit();
        } catch (SolrServerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    
    public static void query() {
        HttpSolrClient client = App.getServer();
        SolrQuery sQuery = new SolrQuery();
        sQuery.setQuery("id:1*");
        QueryResponse queryResponse =null;
        try {
            queryResponse = client.query(sQuery);
            List<TestBO> beans = queryResponse.getBeans(TestBO.class);
            for (TestBO testBO : beans) {
                System.out.println(testBO);
            }
        } catch (SolrServerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        //addIndex();
        //delete("id:12131232*");
        query();
        
    }

}
package com.he.mysolr;

import java.io.Serializable;

import org.apache.solr.client.solrj.beans.Field;

public class TestBO implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Field
    public String id;
    @Field
    public String name_s;
    @Field
    public int score_i;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName_s() {
        return name_s;
    }
    public void setName_s(String name_s) {
        this.name_s = name_s;
    }
    public int getScore_i() {
        return score_i;
    }
    public void setScore_i(int score_i) {
        this.score_i = score_i;
    }
    @Override
    public String toString() {
        return "TestBO [id=" + id + ", name_s=" + name_s + ", score_i=" + score_i + "]";
    }
    

}

 

posted on 2017-09-21 20:58  编世界  阅读(317)  评论(0编辑  收藏  举报