springboot 整合 solr

 solr服务器搭起来, 数据导入之后, 就该应用到项目中去了. 那在项目中, 该怎么整合和应用solr呢? 

  接下来, 就来整合和应用solr

一. 整合

1. 引入jar包

复制代码

    <properties>
        <spring.data.solr.version>2.1.1.RELEASE</spring.data.solr.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-solr</artifactId>
                <version>${spring.data.solr.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>  
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-solr</artifactId>
        </dependency>

        <!-- 默认 starter 会加载 solrj 进来, 下面这个可不引-->
        <dependency>
            <groupId>org.apache.solr</groupId>
            <artifactId>solr-solrj</artifactId>
            <version>6.6.2</version>
        </dependency>
    </dependencies>

复制代码

2. 配置文件

spring:
  data:
    solr:
      host: http://127.0.0.1:8081/solr

host 也可以写成  http://127.0.0.1:8081/solr/collection1.

这里的collection1是 core 的名字, 可以在 core admin 选项中进行配置. 

collection1 这里可以理解为数据库的概念. 在操作的时, 如果有多个core, 可以切换数据库. 也就是切换 core

二. 索引和查询

复制代码

package org.elvin.mysolr.controller;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("solr")
public class SolrController {

    @Autowired
    private SolrClient client;

    /**
     * 新增/修改 索引
     * 当 id 存在的时候, 此方法是修改(当然, 我这里用的 uuid, 不会存在的), 如果 id 不存在, 则是新增
     * @return
     */
    @RequestMapping("add")
    public String add() {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        try {
            SolrInputDocument doc = new SolrInputDocument();
            doc.setField("id", uuid);
            doc.setField("content_ik", "我是中国人, 我爱中国");

            /* 如果spring.data.solr.host 里面配置到 core了, 那么这里就不需要传 collection1 这个参数
             * 下面都是一样的
             */

            client.add("collection1", doc);
            //client.commit();
            client.commit("collection1");
            return uuid;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "error";
    }

    /**
     * 根据id删除索引
     * @param id
     * @return
     */
    @RequestMapping("delete")
    public String delete(String id)  {
        try {
            client.deleteById("collection1",id);
            client.commit("collection1");

            return id;
        } catch (Exception e) {
            e.printStackTrace();
        }


        return "error";
    }

    /**
     * 删除所有的索引
     * @return
     */
    @RequestMapping("deleteAll")
    public String deleteAll(){
        try {

            client.deleteByQuery("collection1","*:*");
            client.commit("collection1");

            return "success";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error";
    }

    /**
     * 根据id查询索引
     * @return
     * @throws Exception
     */
    @RequestMapping("getById")
    public String getById() throws Exception {
        SolrDocument document = client.getById("collection1", "536563");
        System.out.println(document);
        return document.toString();
    }

   
}

复制代码

三. 补充

这里特别需要补充一点的是, 在页面上执行增改查, 都很方便, 感觉是在做填空题. 

但是删除的时候, 貌似不能直接填空了. 有点特别

这里需要使用 xml 的方式操作, 且必须加上 commit, 下面的1000ms自动提交, 不适用于delete

复制代码

<!--删除所有索引-->
<delete>
  <query>*:*</query>
</delete>
<commit />

<!-- 根据 id 进行删除-->
<delete>
  <id>1</id>
</delete>
<commit />

猜你喜欢

转载自blog.csdn.net/weixin_33207551/article/details/84835884
今日推荐