SolrJ8.0使用教程 SolrJ8.0操作Solr的写法(索引的CRUD)

  • 1
  • 浏览量:734 次
  • A+
所属分类:全文检索

现在网上大部分的SolrJ使用教程都是低版本的,用的都是SolrServer这个对象,但是在SolrJ8.0.0版本里已经移除了这个类,网上资料很少,基本上找不到什么8.0版本的写法,但我的solr服务是用8.0启的,所以我还是想用solrJ8.0来操作。

打开jar包看到源码里虽然没有SolrServer类了,但有一个类叫SolrClient,所以应该是用这个类来操作了,其实按照逻辑来说,叫SolrServer比叫SolrServer更确切,因为solr是服务端,solrJ是客户端。

接下来就是new这个对象了,但是SolrClient是个抽象类,不能直接new,在Inellij idea里按ctrl+H可以看到有一个子类叫HttpSolrClient。所以应该用这个来实例化。构造函数的参数里有一个Builder。所以我们还得传一个builder进去。拿到solrClient对象后CRUD的操作跟低版本的SolrJ就完全一样了。

废话不多说,直接上完整代码。

package com.example.solrj;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
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;
import org.junit.Test;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Author zp
 * @create 2019/4/30 8:26
 */
public class SolrJManager {
//    @Test
    /**
     * solrJ 4.10.3版本写法
     */
//    public void testSolrJ4Add() throws IOException, SolrServerException {
//        String baseUrl = "http://localhost:8983/solr/new_core";
//        SolrServer solrServer = new HttpSolrServer(baseUrl);
//        SolrInputDocument doc = new SolrInputDocument();
//        doc.setField("id", "haha");
//        doc.setField("name","我多我");
//
//        solrServer.add(doc);
//        solrServer.commit();
//    }

    @Test
    /**
     * solrJ 8.0.0版本写法
     * add
     */
    public void testSolrJ8Add() throws IOException, SolrServerException {
        String baseUrl = "http://localhost:8983/solr/new_core";
        // 8.0版本里移除了SolrServer类,用SolrClient取代
        // 其实按正常逻辑来说,用SolrClient更确切。
        SolrClient solrClient = new HttpSolrClient.Builder(baseUrl).build();
        SolrInputDocument doc = new SolrInputDocument();
        doc.setField("id", "haha2");
        doc.setField("name","solrJ8.0");

        solrClient.add(doc);
        solrClient.commit();
    }

    @Test
    /**
     * solrJ 8.0.0版本写法
     * delete
     */
    public void testSolrJ8Delete() throws IOException, SolrServerException {
        String baseUrl = "http://localhost:8983/solr/new_core";
        // 8.0版本里移除了SolrServer类,用SolrClient取代
        // 其实按正常逻辑来说,用SolrClient更确切。
        SolrClient solrClient = new HttpSolrClient.Builder(baseUrl).build();

        solrClient.deleteByQuery("id:haha2",1000);

    }

    @Test
    /**
     * solrJ 8.0.0版本写法
     * update
     */
    public void testSolrJ8Update() throws IOException, SolrServerException {
        String baseUrl = "http://localhost:8983/solr/new_core";
        // 8.0版本里移除了SolrServer类,用SolrClient取代
        // 其实按正常逻辑来说,用SolrClient更确切。
        SolrClient solrClient = new HttpSolrClient.Builder(baseUrl).build();

        // update 与add一致 id相同为更新
        SolrInputDocument doc = new SolrInputDocument();
        doc.setField("id", "haha2");
        doc.setField("name","solrJ8.0222222222222");

        solrClient.add(doc);
        solrClient.commit();

    }

    @Test
    /**
     * solrJ 8.0.0版本写法
     * query
     */
    public void testSolrJ8Query() throws IOException, SolrServerException {
        String baseUrl = "http://localhost:8983/solr/new_core";
        // 8.0版本里移除了SolrServer类,用SolrClient取代
        // 其实按正常逻辑来说,用SolrClient更确切。
        SolrClient solrClient = new HttpSolrClient.Builder(baseUrl).build();
        SolrQuery solrParam = new SolrQuery();

        // 关键词 第一个参数就是query
        solrParam.set("q","*:*");
        // 上面的另一种写法
        solrParam.setQuery("book_name:作文");
        // 过滤条件 注意放的顺序会影响最后的结果
        solrParam.set("fq","id:[* TO 559]");
//        solrParam.set("fq","author_name:满分");
        // 排序
        solrParam.addSort("id", SolrQuery.ORDER.asc);
        // 分页
        solrParam.setStart(0);
        solrParam.setRows(5);

        // 默认域
        solrParam.set("df", "book_name");
        // 只查询指定域
        solrParam.set("fl", "id,book_name");
        // 高亮
        solrParam.setHighlight(true);
        solrParam.addHighlightField("book_name");
        solrParam.setHighlightSimplePre("<span style='color:red'>");
        solrParam.setHighlightSimplePost("</span>");

        // 执行查询
        QueryResponse queryResponse = solrClient.query(solrParam);
        SolrDocumentList results = queryResponse.getResults();
        // 获取高亮结果
        Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
        for (SolrDocument doc: results) {
            System.out.println(doc.get("id"));
            System.out.println(doc.get("author_name"));
            System.out.println(doc.get("book_name"));
            System.out.println(doc.get("publish_date"));
            Map<String, List<String>> map = highlighting.get(doc.get("id"));
            List<String> book_name = map.get("book_name");
            System.out.println(book_name.get(0));
            System.out.println("--------------------");
        }



    }
}

 

ZPY

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

目前评论:1   其中:访客  0   博主  0   引用   1

    来自外部的引用: 1

    • SOLR8.0 query查询用法详解 | ZPY博客