zoukankan      html  css  js  c++  java
  • SolrJ总结

    1.solrJ概念

    solrJ是Java连接solr进行查询检索和索引更新维护的jar包。

    2.项目引入solrJ相关jar包

    对于maven工程,直接将下面内容加入到pom文件中即可。

    1. <dependency>
    2. <groupId>org.apache.solr</groupId>
    3. <artifactId>solr-solrj</artifactId>
    4. <version>5.3.1</version>
    5. </dependency>

    注意solrj编译依赖下面jar包

    非maven工程,可在solr安装目录下找到所有需要的jar包

    3.主要用到的类接口简介


    如上图SolrClient是所有类基类,里面定义了更新维护索引、搜索相关的接口;LBHttpSolrClient用于有多个solr服务器时实现负载均衡的情况;ConcurrentUpdateSolrClient类是线程安全类,推荐在更新维护索引时用;HttpSolrClient用于独立工作模式的solr的查询;CloudSorlClient用于solrCould模式。以HttpSolrClient为例说明主要接口的使用。

    • 初始化SolrClient对象
    1. //直接指定solr的URL和core1,只能查询或更新core1内容
    2. SolrClient client = new HttpSolrClient("http://my-solr-server:8983/solr/core1");
    3. QueryResponse resp = client.query(new SolrQuery("*:*"));
    4. //指定solr的URL,查询或更新时要指定core
    5. SolrClient client = new HttpSolrClient("http://my-solr-server:8983/solr");
    6. QueryResponse resp = client.query("core1", new SolrQuery("*:*"));
    • 更新维护索引的主要接口是

      1. addBean(Object obj)
      2. addBean(Object obj, int commitWithinMs)
      3. addBean(String collection, Object obj, int commitWithinMs)
      4. add(String collection, Collection<SolrInputDocument> docs, int commitWithinMs)
      5. add(String collection, SolrInputDocument doc, int commitWithinMs)

      该函数有多个重载形式,obj是要加入索引的实体对象,collection指定要操作的core,commitWithinMs要提交的毫秒数,默认为-1,add后不会更新,要调用
      commit(String collection)提交后才能更新查询到。

    • SolrInputDocument和Object之间转换

    1. doc = getBinder().toSolrInputDocument(obj);
    2. objList =solr.getBinder().getBeans(CaseEntity.class, resp.getResults());




  • 相关阅读:
    hdu 1455 N个短木棒 拼成长度相等的几根长木棒 (DFS)
    hdu 1181 以b开头m结尾的咒语 (DFS)
    hdu 1258 从n个数中找和为t的组合 (DFS)
    hdu 4707 仓鼠 记录深度 (BFS)
    LightOJ 1140 How Many Zeroes? (数位DP)
    HDU 3709 Balanced Number (数位DP)
    HDU 3652 B-number (数位DP)
    HDU 5900 QSC and Master (区间DP)
    HDU 5901 Count primes (模板题)
    CodeForces 712C Memory and De-Evolution (贪心+暴力)
  • 原文地址:https://www.cnblogs.com/doit8791/p/4902571.html
Copyright © 2011-2022 走看看