solr两种安装、ik分词器设置、DIH导入mysql数据

solr 安装(内置方式)

  1. 安装包

    • 官网下载安装包,官网地址:https://lucene.apache.org/solr/downloads.html

    • 使用命令获取:

      [root@10 ss]# wget https://mirror.bit.edu.cn/apache/lucene/solr/7.7.3/solr-7.7.3.tgz
      
  2. 解压

    [root@10 solr]# tar xvf solr-7.7.3.tgz
    
  3. 启动

    [root@10 bin]# pwd
    /root/solr/solr-7.7.3/bin
    [root@10 bin]# ./solr start -force
    *** [WARN] *** Your open file limit is currently 1024.  
     It should be set to 65000 to avoid operational disruption. 
     If you no longer wish to see this warning, set SOLR_ULIMIT_CHECKS to false in your profile or solr.in.sh
    *** [WARN] ***  Your Max Processes Limit is currently 7283. 
     It should be set to 65000 to avoid operational disruption. 
     If you no longer wish to see this warning, set SOLR_ULIMIT_CHECKS to false in your profile or solr.in.sh
    Waiting up to 180 seconds to see Solr running on port 8983 [\]  
    [root@10 bin]# 
    
  4. 浏览器访问

    http://192.168.0.101:8983
    
  5. 配置solr_core

    • 在/server/solr目录下建立一个文件夹,此处叫laosan_core

    • 复制文件到该目录下

      [root@10 solr]# pwd
      /root/solr/solr-7.7.3/server/solr
      [root@10 solr]# cp /root/solr/solr-7.7.3/server/solr/configsets/_default/conf -rf laosan_core
      
    • 修改managed-schema为schema.xml

      [root@10 conf]# pwd
      /root/solr/solr-7.7.3/server/solr/laosan_core/conf
      [root@10 conf]# ll
      总用量 132
      drwxr-xr-x. 2 root root  4096 9月  11 22:19 lang
      -rw-r--r--. 1 root root 54513 9月  11 22:19 managed-schema
      -rw-r--r--. 1 root root   308 9月  11 22:19 params.json
      -rw-r--r--. 1 root root   873 9月  11 22:19 protwords.txt
      -rw-r--r--. 1 root root 53346 9月  11 22:19 solrconfig.xml
      -rw-r--r--. 1 root root   781 9月  11 22:19 stopwords.txt
      -rw-r--r--. 1 root root  1124 9月  11 22:19 synonyms.txt
      [root@10 conf]# mv managed-schema schema.xml
      [root@10 conf]# ll
      总用量 132
      drwxr-xr-x. 2 root root  4096 9月  11 22:19 lang
      -rw-r--r--. 1 root root   308 9月  11 22:19 params.json
      -rw-r--r--. 1 root root   873 9月  11 22:19 protwords.txt
      -rw-r--r--. 1 root root 54513 9月  11 22:19 schema.xml
      -rw-r--r--. 1 root root 53346 9月  11 22:19 solrconfig.xml
      -rw-r--r--. 1 root root   781 9月  11 22:19 stopwords.txt
      -rw-r--r--. 1 root root  1124 9月  11 22:19 synonyms.txt
      
  6. 页面Add Core
    在这里插入图片描述
    添加成功后显示
    在这里插入图片描述

solr 安装(tomcat方式)

  1. 解压tomcat

    [root@10 solr]# unzip apache-tomcat-8.0.52-windows-x64.zip 
    Archive:  apache-tomcat-8.0.52-windows-x64.zip
       creating: apache-tomcat-8.0.52/
    
  2. 移动solr中webapp到tomcat中webapp下

    [root@10 solr-webapp]# pwd
    /root/solr/solr-7.7.3/server/solr-webapp
    [root@10 solr-webapp]# cp -r webapp/ /root/solr/apache-tomcat-8.0.52/webapps
    
  3. 将应用名改为solr

    [root@10 webapps]# pwd
    /root/solr/apache-tomcat-8.0.52/webapps
    [root@10 webapps]# mv webapp/ solr
    [root@10 webapps]# ll
    总用量 12
    drwxr-xr-x. 14 root root 4096 4月  28 2018 docs
    drwxr-xr-x.  6 root root   78 4月  28 2018 examples
    drwxr-xr-x.  5 root root   82 4月  28 2018 host-manager
    drwxr-xr-x.  5 root root   97 4月  28 2018 manager
    drwxr-xr-x.  3 root root 4096 4月  28 2018 ROOT
    drwxr-xr-x.  8 root root 4096 9月  11 22:44 solr
    
  4. 复制所需依赖jar包

    • 复制ext目录下jar包
    [root@10 lib]# pwd
    /root/solr/solr-7.7.3/server/lib
    [root@10 lib]# cp ext/* /root/solr/apache-tomcat-8.0.52/webapps/solr/WEB-INF/lib/
    cp:是否覆盖"/root/solr/apache-tomcat-8.0.52/webapps/solr/WEB-INF/lib/disruptor-3.4.0.jar"? y
    [root@10 lib]#
    
    • 复制有metrics-开头的jar包
    [root@10 lib]# cp metrics-* /root/solr/apache-tomcat-8.0.52/webapps/solr/WEB-INF/lib/
    
  5. 配置solrhome

    • 用于存储Solr Core的数据及配置文件

    • 创建solrhome存储Solr索引文件,创建哪里都可以

      [root@10 lib]# mkdir -p /usr/local/solr/solrhome
      
    • 复制server/solr目录下所有内容到solrhome

      [root@10 solr]# pwd
      /root/solr/solr-7.7.3/server/solr
      [root@10 solr]# cp -r * /usr/local/solr/solrhome/
      
  6. 配置tomcat

    • 修改web.xml,修改solr_home路径指向我们刚刚创建的 solrhome

      [root@10 WEB-INF]# pwd
      /root/solr/apache-tomcat-8.0.52/webapps/solr/WEB-INF
      [root@10 WEB-INF]# vim web.xml 
      
      <!-- 修改solrhome路径 -->
      <env-entry>
              <env-entry-name>solr/home</env-entry-name>
              <env-entry-value>/usr/local/solr/solrhome</env-entry-value>
              <env-entry-type>java.lang.String</env-entry-type>
      </env-entry>
      
    • 去掉安全认证

        <!-- Get rid of error message 
        <security-constraint>
          <web-resource-collection>
            <web-resource-name>Disable TRACE</web-resource-name>
            <url-pattern>/</url-pattern>
            <http-method>TRACE</http-method>
          </web-resource-collection>
          <auth-constraint/>
        </security-constraint>
        <security-constraint>
          <web-resource-collection>
            <web-resource-name>Enable everything but TRACE</web-resource-name>
            <url-pattern>/</url-pattern>
            <http-method-omission>TRACE</http-method-omission>
          </web-resource-collection>
        </security-constraint>
      -->
      
  7. 修改tomcat中bin目录权限

    [root@10 apache-tomcat-8.0.52]# pwd
    /root/solr/apache-tomcat-8.0.52
    [root@10 apache-tomcat-8.0.52]# chmod 777 -R bin/
    
  8. 配置solr_core

    • 在/server/solr目录下建立一个文件夹,此处叫laosan_core

    • 复制文件到该solrhome目录下

      [root@10 solr]# pwd
      /usr/local/solr/solrhome
      [root@10 solr]# cp /root/solr/solr-7.7.3/server/solr/configsets/_default/conf -rf laosan_core
      
    • 修改managed-schema为schema.xml

      [root@10 conf]# pwd
      /usr/local/solr/solrhome/laosan_core/conf
      [root@10 conf]# ll
      总用量 132
      drwxr-xr-x. 2 root root  4096 9月  11 22:19 lang
      -rw-r--r--. 1 root root 54513 9月  11 22:19 managed-schema
      -rw-r--r--. 1 root root   308 9月  11 22:19 params.json
      -rw-r--r--. 1 root root   873 9月  11 22:19 protwords.txt
      -rw-r--r--. 1 root root 53346 9月  11 22:19 solrconfig.xml
      -rw-r--r--. 1 root root   781 9月  11 22:19 stopwords.txt
      -rw-r--r--. 1 root root  1124 9月  11 22:19 synonyms.txt
      [root@10 conf]# mv managed-schema schema.xml
      [root@10 conf]# ll
      总用量 132
      drwxr-xr-x. 2 root root  4096 9月  11 22:19 lang
      -rw-r--r--. 1 root root   308 9月  11 22:19 params.json
      -rw-r--r--. 1 root root   873 9月  11 22:19 protwords.txt
      -rw-r--r--. 1 root root 54513 9月  11 22:19 schema.xml
      -rw-r--r--. 1 root root 53346 9月  11 22:19 solrconfig.xml
      -rw-r--r--. 1 root root   781 9月  11 22:19 stopwords.txt
      -rw-r--r--. 1 root root  1124 9月  11 22:19 synonyms.txt
      
  9. 启动tomcat后add core

    http://192.168.0.101:8080/solr/index.html#/
    

在这里插入图片描述

IK中文分词器设置

  1. 下载solr7版本的ik分词器

    • 下载地址:http://search.maven.org/#search%7Cga%7C1%7Ccom.github.magese
    • GitHub源码地址:https://github.com/magese/ik-analyzer-solr7
  2. 将jar包拷贝到tomcat中lib目录下:

    [root@10 lib]# pwd
    /root/solr/apache-tomcat-8.0.52/webapps/solr/WEB-INF/lib
    [root@10 lib]# rz -E
    rz waiting to receive.
    
  3. 修改managed-schema文件,增加ik分词器

    <!-- ik分词器 -->
    <fieldType name="text_ik" class="solr.TextField"> 
      <analyzer type="index"> 
        <tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory" useSmart="false" conf="ik.conf"/>  
        <filter class="solr.LowerCaseFilterFactory"/> 
      </analyzer>  
      <analyzer type="query"> 
        <tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory" useSmart="true" conf="ik.conf"/>  
        <filter class="solr.LowerCaseFilterFactory"/> 
      </analyzer> 
    </fieldType>
    
  4. 重启tomcat

  5. 页面测试
    在这里插入图片描述

solr数据操作

使用Solr的DIH将mysql中的数据导入到Solr中

使用dataimport导入数据库数据
  1. 复制3个jar包到tomcat/webapp/solr/WEB-INF/lib/下

    • solr-dataimporthandler-extras-7.7.3.jar
    • solr-dataimporthandler-7.7.3.jar
    • mysql-connector-java-5.1.47.jar
  2. 上传sql脚本到服务器并在机器执行

    • 上传sql脚本到此目录

    • 当前目录进入mysql,执行脚本

      [root@10 mysql]# mysql -u root -p
      Enter password: 
      Welcome to the MySQL monitor.  Commands end with ; or \g.
      Your MySQL connection id is 6
      Server version: 5.7.28 MySQL Community Server (GPL)
      Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
      Oracle is a registered trademark of Oracle Corporation and/or its
      affiliates. Other names may be trademarks of their respective
      owners.
      Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
      
      mysql> source create_table.sql
      Query OK, 0 rows affected (0.00 sec)
      
      
  3. solrhome/laosan_core/conf/下创建文件data-config.xml

    [root@10 conf]# pwd
    /usr/local/solr/solrhome/laosan_core/conf
    
    <dataConfig> 
      <!-- 这是mysql的配置 -->  
      <dataSource driver="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.0.112:3306/laosan" user="root" password="root"/>  
      <document> 
        <!-- query是一条sql,代表在数据库查找出来的数据 -->  
        <entity name="position" query="select * from position"> 
          <!-- 每一个field映射着数据库中列与文档中的域,column是数据库列,name是 solr的域(必须是在managed-schema文件中配置过的域才行) -->  
          <field column="companyName" name="companyName"/>  
          <field column="id" name="id"/>  
          <field column="type" name="type"/>  
          <field column="prolong_offline_time"/> 
        </entity> 
      </document> 
    </dataConfig>
    
  4. 修改solrconfig.xml配置文件增加以下内容(678行)

    [root@10 conf]# pwd
    /usr/local/solr/solrhome/laosan_core/conf
    [root@10 laosan_core]# vim solrconfig.xml
    
    <requestHandler name="/dataimport" class="solr.DataImportHandler"> 
      <lst name="defaults"> 
        <str name="config">data-config.xml</str> 
      </lst> 
    </requestHandler>
    
  5. 在managed-schema 配置对应的域(453行)

    [root@10 conf]# pwd
    /usr/local/solr/solrhome/laosan_core/conf
    
    <field name="companyName" type="text_ik" indexed="true" stored="true"/> 
    <field name="positionName" type="text_ik" indexed="true" stored="true"/> 
    <field name="positionAdvantage" type="text_ik" indexed="true" stored="true"/>
    <field name="salary" type="text_ik" indexed="true" stored="true"/>
    <field name="workYear" type="text_ik" indexed="true" stored="true"/>
    
  6. 页面导入数据
    在这里插入图片描述

  7. 查询数据
    在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值