海水中的泪

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
package solr_test;

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

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.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.SolrParams;

public class ServerTest {

    private HttpSolrClient solr;
    private static final String DEFAULT_URL="http://localhost:8080/solr/core_name";
    public void init() throws SolrServerException, IOException{
        //连接到solr
        solr = new HttpSolrClient(DEFAULT_URL);
        /* 以下为solr的一些配置**/
//        solr.setSoTimeout(1000);//socket read timeout
//        solr.setConnectionTimeout(100);
//        solr.setDefaultMaxConnectionsPerHost(100);
//        solr.setMaxTotalConnections(100);
//        solr.setFollowRedirects(false);//defaults to fasle
        solr.setAllowCompression(true);
        
        solr.deleteByQuery("*:*");//清空之前建立的索引数据
        
        
        SolrInputDocument doc1 = new SolrInputDocument();
        doc1.addField("id", "id1",1.0f);
        doc1.addField("name", "doc1", 1.0f);
        doc1.addField("price", 10);
        
        SolrInputDocument doc2 = new SolrInputDocument();
        doc2.addField("id", "id2",1.0f);
        doc2.addField("name", "doc2", 1.0f);
        doc2.addField("price", 20);
        
        Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();        
        docs.add(doc1);
        docs.add(doc2);
        
        solr.add(docs);
        solr.commit();
        
        
        
        
        
        
        
    }
}

这里建立两个文档,每个文档有3个field,分别是id,name,price,这里要注意一下,这3个filed其实已经在schemal中配置好了,如果你自己定义了filed一定要在schemal.xml中配置好。

posted on 2016-05-04 14:06  海水中的泪  阅读(411)  评论(0编辑  收藏  举报