Solrcloud安装与使用

准备

  • JDK 1.8.0.181
  • solr 7.5.0
  • zookeeper 3.4.13

安装步骤

zookeeper集群安装

  1. 下载zookeeper http://mirror.bit.edu.cn/apache/zookeeper
  2. 解压缩zookeeper-3.4.13.tar.gz
    cd /usr/local/app
    tar -xf zookeeper-3.4.13.tar.gz
    mv zookeeper-3.4.13 zookeeper
    
  3. 修改zoo.cfg配置文件
    cd /usr/local/app/zookeeper/conf
    vi zoo.cfg
    --------------------------------
    tickTime=2000
    initLimit=10
    syncLimit=5
    dataDir=/usr/local/app/zookeeper/data
    clientPort=2181
    # server.1中的1位器群机器zookeeper服务的myid
    server.1=172.30.1.31:2888:3888
    server.2=172.30.1.32:2888:3888
    server.3=172.30.1.33:2888:3888
    --------------------------------
    
  4. data目录创建myid文件
    cd /usr/local/app/zookeeper/
    mkdir data
    vi myid
    --------------------------------
    # 第二台机器为2,依次类推
    1
    --------------------------------
    
  5. 启动服务
    ./bin/zkServer.sh start
    
  6. 集群状态检查
    # 带所有服务启动命令之后执行
    ./bin/zkServer.sh start
    

solrcloud安装

  1. 单机安装所有节点服务,见solr单机版安装
  2. 创建chroot
    # 登录任意一台solr节点服务器
    cd /opt/solr/bin/
    ./solr zk mkroot solr -z 172.30.1.31:2181,172.30.1.32:2181,172.30.1.33:2181
    
  3. solr集群配置
    # 每个节点执行
    vi /etc/default/solr.in.sh
    --------------------------------------------
    # 修改java运行环境
    SOLR_JAVA_HOME="/usr/local/jdk1.8.0_31"
    # 修改solrcloud配置
    ZK_HOST="172.30.1.32:2181,172.30.1.33:2181/test"
    # 修改内存
    SOLR_JAVA_MEM="-Xms512m -Xmx512m"
    --------------------------------------------
    
  4. 创建collection
    # 登录任意节点执行
    cd /opt/solr/bin
    # -c collection名称
    # -shards 分片数量
    # -replicationFactor 副本数量
    ./solr create_collection -c second -d _default -shards 2 -replicationFactor 3
    
  5. 修改字段配置
    1. 下载zookeeper管理的配置文件
      # -d 配置文件下载目标目录
      # -n chroot名称
      # -z zookeeper集群地址
      /opt/solr/bin/solr zk downconfig -n second -d ./
      
      
      
    2. 修改下载的配置文件
      vi managed-schema
      -----------------------------------------------------------
      <?xml version="1.0" encoding="UTF-8" ?>
      <!-- Solr managed schema - automatically generated - DO NOT EDIT -->
      <schema name="default-config" version="1.6">
        <!-- 主键定义 -->
        <uniqueKey>id</uniqueKey>
      
        <!-- 字段类型定义 -->
        <fieldType name="binary" class="solr.BinaryField"/>
        <fieldType name="boolean" class="solr.BoolField" sortMissingLast="true"/>
        <fieldType name="booleans" class="solr.BoolField" sortMissingLast="true" multiValued="true"/>
        <fieldType name="pdate" class="solr.DatePointField" docValues="true"/>
        <fieldType name="pdates" class="solr.DatePointField" docValues="true" multiValued="true"/>
        <fieldType name="pdouble" class="solr.DoublePointField" docValues="true"/>
        <fieldType name="pdoubles" class="solr.DoublePointField" docValues="true" multiValued="true"/>
        <fieldType name="pfloat" class="solr.FloatPointField" docValues="true"/>
        <fieldType name="pfloats" class="solr.FloatPointField" docValues="true" multiValued="true"/>
        <fieldType name="pint" class="solr.IntPointField" docValues="true"/>
        <fieldType name="pints" class="solr.IntPointField" docValues="true" multiValued="true"/>
        <fieldType name="plong" class="solr.LongPointField" docValues="true"/>
        <fieldType name="plongs" class="solr.LongPointField" docValues="true" multiValued="true"/>
        <fieldType name="random" class="solr.RandomSortField" indexed="true"/>
        <fieldType name="string" class="solr.StrField" sortMissingLast="true" docValues="true"/>
        <fieldType name="strings" class="solr.StrField" sortMissingLast="true" docValues="true" multiValued="true"/>
        <fieldType name="text_general" class="solr.TextField" positionIncrementGap="100" multiValued="true">
      	<analyzer type="index">
      	  <tokenizer class="solr.StandardTokenizerFactory"/>
      	  <filter class="solr.StopFilterFactory" words="stopwords.txt" ignoreCase="true"/>
      	  <filter class="solr.LowerCaseFilterFactory"/>
      	</analyzer>
      	<analyzer type="query">
      	  <tokenizer class="solr.StandardTokenizerFactory"/>
      	  <filter class="solr.StopFilterFactory" words="stopwords.txt" ignoreCase="true"/>
      	  <filter class="solr.SynonymGraphFilterFactory" expand="true" ignoreCase="true" synonyms="synonyms.txt"/>
      	  <filter class="solr.LowerCaseFilterFactory"/>
      	</analyzer>
        </fieldType>
        <fieldType name="text_ik" class="solr.TextField">
      	<analyzer type="index">
      	  <tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory" conf="ik.conf" useSmart="false"/>
      	  <filter class="solr.LowerCaseFilterFactory"/>
      	</analyzer>
      	<analyzer type="query">
      	  <tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory" conf="ik.conf" useSmart="true"/>
      	  <filter class="solr.LowerCaseFilterFactory"/>
      	</analyzer>
        </fieldType>
      
        <!-- 版本必须 -->
        <field name="_version_" type="plong" indexed="false" stored="false"/>
        <!-- id -->
        <field name="id" type="string" multiValued="false" indexed="true" required="true" stored="true"/>
        <!-- 姓名 -->
        <field name="name" type="string" indexed="true" stored="true"/>
        <!-- 简介 分词 -->
        <field name="intro" type="text_ik" indexed="true" stored="true"/>
        <!-- 爱好 多选 -->
        <field name="hobby" type="strings" multiValued="true" indexed="false" stored="true"/>
        <!-- 出生日期 -->
        <field name="birthday" type="pdate" indexed="true" stored="true"/>
      </schema>
      
    3. 上传配置文件到zookeeper
      /opt/solr/bin/solr zk upconfig -n demo -d ./
      
    4. 重启集群

客户端开始使用

  • springboot 2.1.1
  • solr 7.5

导入依赖

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

application.properties配置

spring.data.solr.zk-host=172.30.1.31:2181,172.30.1.32:2181,172.30.1.33:2181/solr
spring.data.solr.repositories.enabled=true

entity实体

/*
 * Copyright 2014-2018 nickboyer.cn.
 * All rights reserved.
 */
package cn.nickboyer.demo.solr.controller;

import org.apache.solr.client.solrj.beans.Field;

import java.util.Date;
import java.util.List;

/**
 * @author Kang.Y
 * @Date 15:09 2018/12/11
 * @since 1.8
 */
public class Person {

    @Field
    private String id;
    @Field
    private String name;
    @Field
    private String intro;
    @Field
    private List<String> hobby;
    @Field
    private Date birthday;

    @Override
    public String toString() {
        return "Person{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", intro='" + intro + '\'' +
                ", hobby=" + hobby +
                ", birthday=" + birthday +
                '}';
    }

    get/set 略...
}

service调用(增删改查、搜索)

/*
 * Copyright 2014-2018 nickboyer.cn.
 * All rights reserved.
 */
package cn.nickboyer.demo.solr.controller;

import org.apache.commons.lang3.StringUtils;
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.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @author Kang.Y
 * @Date 15:08 2018/12/11
 * @since 1.8
 */
@RestController
public class DemoController {

    @Autowired
    private SolrClient solrClient;


    @PostConstruct
    public void init() {
        if (solr instanceof CloudSolrClient) {
            ((CloudSolrClient) solr).setDefaultCollection("second");
        }
    }
    
    /**
     * 新增/修改
     *
     * @param person
     * @return
     * @throws Exception
     */
    @RequestMapping("add")
    public Object add(@RequestBody Person person) throws Exception {
        String id = UUID.randomUUID().toString().replaceAll("-", "");
        if (StringUtils.isEmpty(person.getId())) {
            person.setId(id);
        }
        UpdateResponse updateResponse = solrClient.addBean(person);
        solrClient.commit();
        return "{\"QTime\":" + updateResponse.getQTime() + "}";
    }

    /**
     * 删除
     *
     * @param person
     * @return
     * @throws Exception
     */
    @RequestMapping("delete")
    public Object delete(@RequestBody Person person) throws Exception {
        UpdateResponse updateResponse = solrClient.deleteById(person.getId());
        return "{\"QTime\":" + updateResponse.getQTime() + "}";
    }

    /**
     * 查询
     *
     * @param person
     * @return
     * @throws Exception
     */
    @RequestMapping("select")
    public Object selectById(@RequestBody Person person) throws Exception {
        SolrDocument document = solrClient.getById(person.getId());
        return document;
    }

    /**
     * 搜索
     *
     * @param person
     * @return
     * @throws Exception
     */
    @RequestMapping("search")
    public Object selects(@RequestBody Person person) throws Exception {
        SolrQuery query = new SolrQuery();

        if (StringUtils.isNotEmpty(person.getIntro())) {
            query.set("q", "intro:" + person.getIntro());
        }

        // 参数sort,设置返回结果的排序规则
        query.setSort("id", SolrQuery.ORDER.desc);

        // 设置分页参数
        query.setStart(0);
        // 每一页多少值
        query.setRows(10);

        // 获取查询结果
        QueryResponse response = solrClient.query(query);

        // 得到实体对象
        List<Person> tmpLists = response.getBeans(Person.class);
        return tmpLists;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值