使用solrJ创建索引

sorlJ官方介绍谷歌翻译

SolrJ是一个API,可以让Java应用程序轻松与Solr对话。 SolrJ隐藏了很多连接到Solr的细节,并允许您的应用程序通过简单的高级方法与Solr进行交互。 

 

package com.example.demo.solr;

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

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrInputDocument;

public class solrDemo3 {
    public static void main(String[] args) throws SolrServerException, IOException {
        String urlString = "http://localhost:8983/solr/articles";
        SolrClient solrClient = new HttpSolrClient(urlString);
        Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
        for (int i = 0; i < 9; i++) {
            SolrInputDocument doc = new SolrInputDocument();
            doc.addField("id", i * 100);
            doc.addField("name", "user" + i);
            doc.addField("price", "100");
            doc.addField("description", "新增文档" + i);
            docs.add(doc);
        }
        UpdateResponse rsp = solrClient.add(docs);
        System.out.println("Add doc size" + docs.size() + " result:" + rsp.getStatus() + " Qtime:" + rsp.getQTime());
        UpdateResponse rspcommit = solrClient.commit();
        System.out.println("commit doc to index" + " result:" + rsp.getStatus() + " Qtime:" + rsp.getQTime());

    }
}

 

查询效果

 

posted @ 2018-03-04 21:39  wdmiye  阅读(246)  评论(0编辑  收藏  举报