Solr JAVA客户端SolrJ 4.9使用示例教程

简介

SolrJ是操作Solr的JAVA客户端,它提供了增加、修改、删除、查询Solr索引的JAVA接口。SolrJ针对 Solr提供了Rest 的HTTP接口进行了封装, SolrJ底层是通过使用httpClient中的方法来完成Solr的操作。 

SolrJ/Solr 跨版本兼容性

SolrJ通常向后保持兼容性,可以使用新版本的SolrJ访问较旧的Solr,反之亦然。建议使用同Solr server同版本的SolrJ,

详见 http://wiki.apache.org/solr/Solrj 中关于兼容性的描述。

示例代码
•    采用 SolrInputDocument对象 增加、删除索引
• import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;

public class AddDocsDemo {
  public static final String SOLR_URL = "http://172.168.63.233:8983/solr";

  public static void main(String[] args) {
    //通过浏览器查看结果
    //http://172.168.63.233:8983/solr/collection1/select?q=name%3A%E6%94%B9%E9%9D%A9&wt=json&indent=true
    //AddDocs();
    delDocs();
  }

  public static void AddDocs() {
    String[] words = { "中央全面深化改革领导小组", "第四次会议", "审议了国企薪酬制度改革", "考试招生制度改革",
        "传统媒体与新媒体融合等", "相关内容文件", "xxx强调要", "逐步规范国有企业收入分配秩序",
        "实现薪酬水平适当", "结构合理、管理规范、监督有效", "对不合理的偏高", "过高收入进行调整",
        "深化考试招生制度改革", "总的目标是形成分类考试", "综合评价", "多元录取的考试招生模式", "健全促进公平",
        "科学选才", "监督有力的体制机制", "着力打造一批形态多样", "手段先进", "具有竞争力的新型主流媒体",
        "建成几家拥有强大实力和传播力", "公信力", "影响力的新型媒体集团" };
    long start = System.currentTimeMillis();
    Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
    for (int i = 1; i < 300; i++) {
      SolrInputDocument doc1 = new SolrInputDocument();
      doc1.addField("id", "id" + i, 1.0f);
      doc1.addField("name", words[i % 21], 1.0f);
      doc1.addField("price", 10 * i);
      docs.add(doc1);
    }
    try {
      HttpSolrServer server = new HttpSolrServer(SOLR_URL);
      // 可以通过三种方式增加docs,其中server.add(docs.iterator())效率最高
      // 增加后通过执行commit函数commit (936ms)
//    server.add(docs);
//    server.commit();

      // 增加doc后立即commit (946ms)
//    UpdateRequest req = new UpdateRequest();
//    req.setAction(ACTION.COMMIT, false, false);
//    req.add(docs);
//    UpdateResponse rsp = req.process(server);

      // the most optimal way of updating all your docs
      // in one http request(432ms)
      server.add(docs.iterator());
    } catch (Exception e) {
      System.out.println(e);
    }
    System.out.println("time elapsed(ms):"
        + (System.currentTimeMillis() - start));
  }

  public static void delDocs() {
    long start = System.currentTimeMillis();
    try {
      HttpSolrServer server = new HttpSolrServer(SOLR_URL);
      List<String> ids = new ArrayList<String>();
      for (int i = 1; i < 300; i++) {
        ids.add("id" + i);
      }
      server.deleteById(ids);
      server.commit();
    } catch (Exception e) {
      System.out.println(e);
    }
    System.out.println("time elapsed(ms):"
        + (System.currentTimeMillis() - start));
  }
}
 
•     采用POJOs增加、删除索引
• import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Random;

import org.apache.solr.client.solrj.beans.DocumentObjectBinder;
import org.apache.solr.client.solrj.impl.BinaryRequestWriter;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;

public class AddBeansDemo {
  public static final String SOLR_URL = "http://172.168.63.233:8983/solr";

  public static void main(String[] args) {
    // 通过浏览器查看结果
    // 要保证bean中各属性的名称在conf/schema.xml中存在,如果查询,要保存被索引
    // http://172.168.63.233:8983/solr/collection1/select?q=description%3A%E6%94%B9%E9%9D%A9&wt=json&indent=true
//  delBeans();
    AddBeans();
  }

  public static Random rand = new Random(47);
  public static String[] authors = { "张三", "李四", "王五", "赵六", "张飞", "刘备",
      "关云长" };
  public static String[] links = {
      "http://repository.sonatype.org/content/sites/forge-sites/m2e/",
      "http://news.ifeng.com/a/20140818/41626965_0.shtml",
      "http://news.ifeng.com/a/20140819/41631363_0.shtml?wratingModule_1_9_1",
      "http://news.ifeng.com/topic/19382/",
      "http://news.ifeng.com/topic/19644/" };

  public static String genAuthors() {
    List<String> list = Arrays.asList(authors).subList(0, rand.nextInt(7));
    String str = "";
    for (String tmp : list) {
      str += " " + tmp;
    }
    return str;
  }

  public static List<String> genLinks() {
    return Arrays.asList(links).subList(0, rand.nextInt(5));
  }

  public static void AddBeans() {
    String[] words = { "中央全面深化改革领导小组", "第四次会议", "审议了国企薪酬制度改革", "考试招生制度改革",
        "传统媒体与新媒体融合等", "相关内容文件", "xxx强调要", "逐步规范国有企业收入分配秩序",
        "实现薪酬水平适当", "结构合理、管理规范、监督有效", "对不合理的偏高", "过高收入进行调整",
        "深化考试招生制度改革", "总的目标是形成分类考试", "综合评价", "多元录取的考试招生模式", "健全促进公平",
        "科学选才", "监督有力的体制机制", "着力打造一批形态多样", "手段先进", "具有竞争力的新型主流媒体",
        "建成几家拥有强大实力和传播力", "公信力", "影响力的新型媒体集团" };

    long start = System.currentTimeMillis();
    Collection<NewsBean> docs = new ArrayList<NewsBean>();
//  DocumentObjectBinder binder = new DocumentObjectBinder();
    for (int i = 1; i < 300; i++) {
      NewsBean news = new NewsBean();
      news.setId("id" + i);
      news.setName("news" + i);
      news.setAuthor(genAuthors());
      news.setDescription(words[i % 21]);
      news.setRelatedLinks(genLinks());
//   SolrInputDocument doc1 = binder.toSolrInputDocument(news);
      docs.add(news);
    }
    try {
      HttpSolrServer server = new HttpSolrServer(SOLR_URL);
      server.setRequestWriter(new BinaryRequestWriter());
      // 可以通过二种方式增加docs,其中server.add(docs.iterator())效率最高
      // 增加后通过执行commit函数commit (981ms)
      // server.addBeans(docs);
      // server.commit();

      // the most optimal way of updating all your docs
      // in one http request(481ms)
      server.addBeans(docs.iterator());
      server.optimize(); //time elasped 1176ms
    } catch (Exception e) {
      System.out.println(e);
    }
    System.out.println("time elapsed(ms):"
        + (System.currentTimeMillis() - start));
  }

  public static void delBeans() {
    long start = System.currentTimeMillis();
    try {
      HttpSolrServer server = new HttpSolrServer(SOLR_URL);
      List<String> ids = new ArrayList<String>();
      for (int i = 1; i < 300; i++) {
        ids.add("id" + i);
      }
      server.deleteById(ids);
      server.commit();
    } catch (Exception e) {
      System.out.println(e);
    }
    System.out.println("time elapsed(ms):"
        + (System.currentTimeMillis() - start));
  }
}
import java.util.List;

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

class NewsBean {
  @Field
  private String id;

  @Field
  private String name;

  @Field
  private String author;

  @Field
  private String description;

  @Field("links")
  private List<String> relatedLinks;
 
  public NewsBean(){
   
  }

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

 
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getAuthor() {
    return author;
  }

  public void setAuthor(String author) {
    this.author = author;
  }

  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  public List<String> getRelatedLinks() {
    return relatedLinks;
  }

  public void setRelatedLinks(List<String> relatedLinks) {
    this.relatedLinks = relatedLinks;
  }
}
 
•     普通方式处理查询结果
• import java.io.IOException;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.BinaryRequestWriter;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;

public class QueryDocsDemo {
// public static final String SOLR_URL = "http://192.168.230.128:8983/solr";
  public static final String SOLR_URL = "http://172.168.63.233:8983/solr";

  public static void main(String[] args) throws SolrServerException, IOException {
    HttpSolrServer server = new HttpSolrServer(SOLR_URL);
    server.setMaxRetries(1);
    server.setMaxRetries(1); // defaults to 0. > 1 not recommended.
    server.setConnectionTimeout(5000); // 5 seconds to establish TCP
    //正常情况下,以下参数无须设置
    //使用老版本solrj操作新版本的solr时,因为两个版本的javabin incompatible,所以需要设置Parser
    server.setParser(new XMLResponseParser());
    server.setSoTimeout(1000); // socket read timeout
    server.setDefaultMaxConnectionsPerHost(100);
    server.setMaxTotalConnections(100);
    server.setFollowRedirects(false); // defaults to false
    // allowCompression defaults to false.
    // Server side must support gzip or deflate for this to have any effect.
    server.setAllowCompression(true);

    //使用ModifiableSolrParams传递参数
//  ModifiableSolrParams params = new ModifiableSolrParams();
//  // 192.168.230.128:8983/solr/select?q=video&fl=id,name,price&sort=price asc&start=0&rows=2&wt=json
//  // 设置参数,实现上面URL中的参数配置
//  // 查询关键词
//  params.set("q", "video");
//  // 返回信息
//  params.set("fl", "id,name,price,score");
//  // 排序
//  params.set("sort", "price asc");
//  // 分页,start=0就是从0开始,rows=5当前返回5条记录,第二页就是变化start这个值为5就可以了
//  params.set("start", 2);
//  params.set("rows", 2);
//  // 返回格式
//  params.set("wt", "javabin");
//  QueryResponse response = server.query(params);

    //使用SolrQuery传递参数,SolrQuery的封装性更好
    server.setRequestWriter(new BinaryRequestWriter());
    SolrQuery query = new SolrQuery();
    query.setQuery("video");
    query.setFields("id","name","price","score");
    query.setSort("price", ORDER.asc);
    query.setStart(0);
    query.setRows(2);
//  query.setRequestHandler("/select");
    QueryResponse response = server.query( query );
   
   
   
    // 搜索得到的结果数
    System.out.println("Find:" + response.getResults().getNumFound());
    // 输出结果
    int iRow = 1;
    for (SolrDocument doc : response.getResults()) {
      System.out.println("----------" + iRow + "------------");
      System.out.println("id: " + doc.getFieldValue("id").toString());
      System.out.println("name: " + doc.getFieldValue("name").toString());
      System.out.println("price: "
          + doc.getFieldValue("price").toString());
      System.out.println("score: " + doc.getFieldValue("score"));
      iRow++;
    }
  }
}
 
•     采用POJOs方式处理查询结果
• import java.io.IOException;
import java.util.List;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.beans.DocumentObjectBinder;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.FacetField;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;

public class QueryBeanDemo {
  public static final String SOLR_URL = "http://172.168.63.233:8983/solr";

  public static void main(String[] args) throws SolrServerException,
      IOException {
    // http://172.168.63.233:8983/solr/collection1/select?q=description%3A%E6%80%BB%E7%9B%AE%E6%A0%87&facet=true&facet.field=author_s
    HttpSolrServer server = new HttpSolrServer(SOLR_URL);
    server.setMaxRetries(1);
    server.setMaxRetries(1); // defaults to 0. > 1 not recommended.
    server.setConnectionTimeout(5000); // 5 seconds to establish TCP
    // server.setRequestWriter(new BinaryRequestWriter());

    SolrQuery query = new SolrQuery();
    query.setQuery("description:改革");
    query.setStart(0);
    query.setRows(2);
    query.setFacet(true);
    query.addFacetField("author_s");

    QueryResponse response = server.query(query);
    // 搜索得到的结果数
    System.out.println("Find:" + response.getResults().getNumFound());
    // 输出结果
    int iRow = 1;
   
    //response.getBeans存在BUG,将DocumentObjectBinder引用的Field应该为 org.apache.solr.client.solrj.beans.Field
    SolrDocumentList list = response.getResults();
    DocumentObjectBinderL binder = new DocumentObjectBinderL();
    List<NewsBean> beanList=binder.getBeans(NewsBean.class, list);
    for(NewsBean news:beanList){
      System.out.println(news.getId());
    }

    for (SolrDocument doc : response.getResults()) {
      System.out.println("----------" + iRow + "------------");
      System.out.println("id: " + doc.getFieldValue("id").toString());
      System.out.println("name: " + doc.getFieldValue("name").toString());
      iRow++;
    }
    for (FacetField ff : response.getFacetFields()) {
      System.out.println(ff.getName() + "," + ff.getValueCount() + ","
          + ff.getValues());
    }
  }
}

相关推荐