一、Solrj的使用

  1.什么是Solrj

  solrj是访问Solr服务的java客户端(就像通过jedis操作redis一样),提供索引和搜索的请求方法,SolrJ通常在嵌入在业务系统中,通过SolrJ的API接口操作Solr服务,如下图:

  Solr第二讲——SolrJ客户端的使用与案例

  2.如何使用

     需要的是solrj的包与拓展服务包

    Solr第二讲——SolrJ客户端的使用与案例

    使用solrj完成索引的维护:

    在solr中,索引库中都会存在一个唯一键,如果一个Document的id存在,则执行修改操作,如果不存在,则执行添加操作。

      添加/修改索引:   

    1、 创建HttpSolrServer对象,通过它和Solr服务器建立连接。

    2、 创建SolrInputDocument对象,然后通过它来添加域。

    3、 通过HttpSolrServer对象将SolrInputDocument添加到索引库。

    4、 提交。

@Test
    public void addDocument() throws Exception {

        // 1、 创建HttpSolrServer对象,通过它和Solr服务器建立连接。
        // 参数:solr服务器的访问地址
        HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/");
        // 2、 创建SolrInputDocument对象,然后通过它来添加域。
        SolrInputDocument document = new SolrInputDocument();
        // 第一个参数:域的名称,域的名称必须是在schema.xml中定义的
        // 第二个参数:域的值
        // 注意:id的域不能少
        document.addField("id", "c0001");
        document.addField("title_ik", "使用solrJ添加的文档");
        document.addField("content_ik", "文档的内容");
        document.addField("product_name", "商品名称");
        // 3、 通过HttpSolrServer对象将SolrInputDocument添加到索引库。
        server.add(document);
        // 4、 提交。
        server.commit();
    }
View Code

相关文章:

  • 2021-06-08
  • 2022-12-23
  • 2021-11-05
  • 2021-04-07
  • 2022-12-23
  • 2022-12-23
  • 2021-12-01
  • 2022-01-15
猜你喜欢
  • 2022-12-23
  • 2021-07-16
  • 2021-12-14
  • 2022-12-23
  • 2022-12-23
  • 2021-10-12
  • 2021-08-30
相关资源
相似解决方案