solr增删改查

  1. 打开solr-7.7.3里面的bin文件夹,cmd输入solr start启动,默认端口8983
  2. localhost:8983/solr/
  3. 新建一个java项目
  4. pom.xml:
<!-- springboot整合mybatis -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.0</version>
</dependency>

<!-- 引入solr依赖 -->
<dependency>
    <groupId>org.apache.solr</groupId>
    <artifactId>solr-solrj</artifactId>
    <version>5.1.0</version>
</dependency>

<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc6</artifactId>
    <version>11.2.0.3</version>
</dependency>

  1. application.yml:
spring:
  data:
    solr:
      host: http://localhost:8983/solr/an-core #这里面的an-core

在这里插入图片描述
6.Test:

public class SolrUtilTest {

    public static void main(String[] args) throws SolrServerException, IOException {
//         addDocument();
         // deteleDocument();
         // updateDocument();
        //queryDocument();
//        updateSolrData();
        queryDim();
    }

    /**
     * 添加文档内容
     *
     * @throws SolrServerException
     * @throws IOException
     */
    public static void addDocument() throws SolrServerException, IOException {
        // 连接solr服务器
        HttpSolrClient httpSolrServer = new HttpSolrClient("http://localhost:8983/solr/testcore");
        // 创建一个文档对象, 向文档中添加域,必须有id域,域的名称必须在scheme.xml中定义
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());
        System.out.println(formatter.format(date));
        SolrInputDocument document = new SolrInputDocument();
        document.addField("id", 2);
        document.addField("seqNo", "2");
        document.addField("area", "2");
        document.addField("local", "2");
        document.addField("plateTime", formatter.format(date));
        // 把文档对象写入索引库
        httpSolrServer.add(document);
        // 提交
        httpSolrServer.commit();
    }

    /**
     * 更新文档内容,跟添加的区别是:id不能变,其他的可以变
     *
     * @throws SolrServerException
     * @throws IOException
     */
    public static void updateDocument() throws SolrServerException, IOException {
        // 连接solr服务器
        HttpSolrClient httpSolrServer = new HttpSolrClient("http://localhost:8983/solr/testcore");
        // 创建一个文档对象, 向文档中添加域,必须有id域,域的名称必须在scheme.xml中定义
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());
        System.out.println(formatter.format(date));
        SolrInputDocument document = new SolrInputDocument();
        document.addField("id", 1);
        document.addField("seqNo", "222");
        document.addField("area", "222");
        document.addField("local", "222");
        document.addField("plateTime", formatter.format(date));
        // 把文档对象写入索引库
        httpSolrServer.add(document);
        // 提交
        httpSolrServer.commit();
    }

    /**
     * java操作Solr的全量或增量更新,可以结合定时任务做定时全量或增量更新
     * @throws SolrServerException
     * @throws IOException
     */
    public static void updateSolrData() throws SolrServerException, IOException {
        // 连接solr服务器
        HttpSolrServer httpSolrServer = new HttpSolrServer("http://localhost:8983/solr/testcore");
        //创建一个查询对象
        SolrQuery solrQuery = new SolrQuery();

        //增量更新全部完成;注意这里entity的值为solr-data-config.xml里entity标签里的name值
        final String SOLR_DELTA_PARAM = "/dataimport?command=delta-import&entity=order_info&clean=false&commit=true";
        //全量更新全部完成
        final String SOLR_FULL_PARAM = "/dataimport?command=full-import&entity=order_info&clean=true&commit=true";
        //设置更新方式
        solrQuery.setRequestHandler(SOLR_DELTA_PARAM);

        // 执行查询
        QueryResponse query = httpSolrServer.query(solrQuery);

        //提交
        httpSolrServer.commit();

    }

    /**
     * 查询文档内容
     *
     * @throws SolrServerException
     * @throws IOException
     */
    public static void queryDocument() throws SolrServerException, IOException {
        // 连接solr服务器
        HttpSolrServer httpSolrServer = new HttpSolrServer("http://localhost:8983/solr/testcore");
        // 创建一个查询条件
        SolrQuery solrQuery = new SolrQuery();
        // 设置查询条件
        solrQuery.setQuery("*:*");
        // 设置分页
        solrQuery.setStart(1);
        solrQuery.setRows(10);
        // 执行查询
        QueryResponse query = httpSolrServer.query(solrQuery);
        // 取查询结果
        SolrDocumentList solrDocumentList = query.getResults();

        System.out.println("总记录数:" + solrDocumentList.getNumFound());

        for (SolrDocument sd : solrDocumentList) {
            System.out.println(sd.get("id"));
            System.out.println(sd.get("seqNo"));
            System.out.println(sd.get("area"));
            System.out.println(sd.get("local"));
            System.out.println(sd.get("plateTime"));
        }

    }


    /**
     * 模糊查询
     *
     * @throws SolrServerException
     * @throws IOException
     */
    public static void queryDim() throws SolrServerException, IOException {
        // 连接solr服务器
        HttpSolrServer httpSolrServer = new HttpSolrServer("http://localhost:8983/solr/testcore");
        // 创建一个查询条件
        SolrQuery solrQuery = new SolrQuery();
        // 设置查询条件
        solrQuery.setQuery("*"+ 2 +"*");
        // 设置分页
        solrQuery.setStart(1);
        solrQuery.setRows(10);
        // 执行查询
        QueryResponse query = httpSolrServer.query(solrQuery);
        // 取查询结果
        SolrDocumentList solrDocumentList = query.getResults();

        System.out.println("总记录数:" + solrDocumentList.getNumFound());

        for (SolrDocument sd : solrDocumentList) {
            System.out.println(sd.get("id"));
            System.out.println(sd.get("seqNo"));
            System.out.println(sd.get("area"));
            System.out.println(sd.get("local"));
            System.out.println(sd.get("plateTime"));
        }

    }

    /**
     * 删除文档
     *
     * @throws SolrServerException
     * @throws IOException
     */
    public static void deteleDocument() throws SolrServerException, IOException {
        // 连接solr服务器
        HttpSolrClient httpSolrServer = new HttpSolrClient("http://localhost:8983/solr/testcore");
        // 根据id删除
        httpSolrServer.deleteById("1");
        // 根据条件删除
        // httpSolrServer.deleteByQuery("");
        // 提交
        httpSolrServer.commit();
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值