【Solr in Action】eclipse+java+solrj环境下实现solr客户端数据上传

-------------------------------------------------说明------------------------------------------------

solr服务器版本为7.1,程序引用的jar包是4.7版本。

-------------------------------------------------图示------------------------------------------------

运行程序上传数据后,在solr服务端查询数据的图示如下:


-------------------------------------------------程序------------------------------------------------

import java.io.PrintStream;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


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;




/**
 * Minimal example client to index documents using SolrJ version 4.x+
 */
public class ExampleSolrJClient {


public static void main(String[] args) throws Exception{
String serverUrl= "http://localhost:8983/solr/newcore1";


        // Get a connection to Solr
        SolrServer solr = new HttpSolrServer(serverUrl);


        // Add some example docs
        SolrInputDocument doc1 = new SolrInputDocument();
        doc1.setField("id", "1");
        doc1.setField("screen_name_s", "@thelabdude");
        doc1.setField("type_s", "post");
        doc1.setField("lang_s", "en");
        doc1.setField("timestamp_tdt", "2012-05-22T09:30:22Z/HOUR");
        doc1.setField("favourites_count_ti", "10");
        doc1.setField("text_t", "#Yummm :) Drinking a latte at Caffe Grecco in SF's" +
          " historic North Beach... Learning text analysis with #SolrInAction by @Manning on my i-Pad");


        solr.add(doc1);


        SolrInputDocument doc2 = new SolrInputDocument();
        doc2.setField("id", "2");
        doc2.setField("screen_name_s", "@thelabdude");
        doc2.setField("type_s", "post");
        doc2.setField("lang_s", "en");
        doc2.setField("timestamp_tdt", "2012-05-22T09:30:22Z/HOUR");
        doc2.setField("favourites_count_ti", "10");
        doc2.setField("text_t", "Just downloaded the MEAP for #SolrInAction from" +
          " @Manning http://bit.ly/15tzw to learn more about #Solr http://bit.ly/3ynriE");
        doc2.addField("link_ss", "http://manning.com/");
        doc2.addField("link_ss", "http://lucene.apache.org/solr/");


        solr.add(doc2);


        // Make the docs we just added searchable using a "hard" commit
        solr.commit(true, true);


        // Send and process the first 10 rows of the match all docs query
        for (SolrDocument next : simpleSolrQuery(solr, "*:*", 10)) {
            prettyPrint(System.out, next);
        }        
    }


    static SolrDocumentList simpleSolrQuery(SolrServer solr, String query, int rows) throws SolrServerException {
        SolrQuery solrQuery = new SolrQuery(query);
        solrQuery.setRows(rows);
        QueryResponse resp = solr.query(solrQuery);
        SolrDocumentList hits = resp.getResults();
        return hits;
    }


    public String getDescription() {
        return "Use SolrJ client API to index some example documents and then query Solr.";
    }
    
  static  void prettyPrint(PrintStream out, SolrDocument doc) {
        List<String> sortedFieldNames = new ArrayList<String>(doc.getFieldNames());
        Collections.sort(sortedFieldNames);
        out.println();
        for (String field : sortedFieldNames) {
            out.println(String.format("\t%s: %s", field, doc.getFieldValue(field)));
        }
        out.println();
    }
    

}

-------------------------------------------------结束------------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值