linux 安装 solr 7.5

2018-11-26  本文已影响0人  塞外务农
第一步:安装启动
cd /usr/local
wget http://mirrors.shuosc.org/apache/lucene/solr/7.5.0/solr-7.5.0.tgz
tar -zxvf solr-7.5.0.tgz
mv solr-7.5.0 solr
vim /etc/profile
--------------------------------------------------------------------------
    export SOLR_INSTALL_HOME=/usr/local/solr
    export PATH=$SOLR_INSTALL_HOME/bin:$PATH
--------------------------------------------------------------------------
source /etc/profile
solr start -force
第二步:开启防火墙

浏览器访问xxx.xxxx.xxx.xxxx:8983/solr

第三步:新建实例

solr的web界面的 "Add Core" 添加一个 "test" 实例
然后去test中添加配置文件

cp -r /usr/local/solr/server/solr/configsets/_default/conf /usr/local/solr/server/solr/configsets/test

再次Add Core

第四步:配置文件

在test/conf目录下新建data-config.xml数据库配置文件

<?xml version="1.0" encoding="UTF-8"?>
<dataConfig>
    <dataSource name="source" type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1:3306/databasename" user="xxxx" password="xxxx" />
    <document>
        <entity name="your_db_table_name"
            pk="id"
            dataSource="source"
            query="select * from your_db_table_name"
            deltaImportQuery="select * from your_db_table_name id = '${dih.delta.id}'"
            deltaQuery="select id from your_db_table_name update_date > '${dataimporter.last_index_time}' and status_flag = 'success'">
            <field column="id" name="id"/>
            <field column="update_date" name="update_date"/>
        </entity>
    </document>
</dataConfig>

query:是获取全部数据的SQL
deltaImportQuery:是获取增量数据时使用的SQL
deltaQuery:是获取pk的SQL
field:column数据库字段名,name为schema.xml中字段名

然后在test的solrconfig.xml配置文件中添加一行

<requestHandler name="/dataimport" class="solr.DataImportHandler">
      <lst name="defaults">
        <str name="config">data-config.xml</str>
      </lst>
</requestHandler>

然后在test的managed-schema配置文件中添加需要的字段

<field name="name" type="string" indexed="true" stored="true">

name 前后有下划线的name是系统保留的名字,比如“version
type 类型,对应于fieldType的name
default 该field的缺省值
indexed 是否为该field建立索引,以让用户可以搜索它、统计它(facet)
stored true/false,定义这个field是否可以返回给查询者
multiValued true/false,是否可以容纳多个值(比如多个copyField的dest指向它)。如果是true,则该field不能被排序、不能作为uniqueKey
required true/false,告诉solr这个field是否接受空值,缺省为false
docValues true/false,建立document-to-value索引,以提高某些特殊搜索的效率(排序、统计、高亮)

第五步:导入必备的三个jar包

第一个:
mysql-connector-java-5.1.15-bin.jar
可以自搜下载(https://link.juejin.im/?target=http://central.maven.org/maven2/mysql/mysql-connector-java/8.0.11/mysql-connector-java-8.0.11.jar

第二、三个:
solr-dataimporthandler-7.5.0.jar
solr-dataimporthandler-extras-7.5.0.jar
这两个包去dist目录下找

将这三个包导入到 /usr/local/solr/server/solr-webapp/webapp/WEB-INF/lib

第六步:重启导入数据

solr restart -force


图片.png
图片.png
第七步:添加中文分词,通常情况下都是需要分词的,一般使用IK分词居多,还有庖丁之类的分词

下载分词器 https://search.maven.org/search?q=g:com.github.magese
将分词器的jar包导入/usr/local/solr/server/solr-webapp/webapp/WEB-INF/lib目录下

在test下的配置文件中追加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>  

这样我们就可以修改type类型为text_ik了

<field name="title" type="text_ik" indexed="true" stored="true"/>
<field name="content" type="text_ik" indexed="true" stored="true"/>
......

重启生效

安全配置

理论上solr是不对外网的,但是有时候为了测试什么的,还是需要用一下,所以可以配置用户名、密码来防止其他人直接操作你的solr,最重要的是可以直接看见你的数据用户名、密码;

首先创建配置文件:touch /usr/local/solr/server/etc/role.properties
追加如下内容:用户为test 密码123 权限为admin)

test: 123,admin

然后编辑文件:vim /usr/local/solr/server/contexts/solr-jetty-context.xml
追加如下内容:

<Get name="securityHandler">    
        <Set name="loginService">    
                <New class="org.eclipse.jetty.security.HashLoginService">    
                        <Set name="name">你猜猜账号密码多少啊?</Set> 
                        <Set name="config"><SystemProperty name="jetty.home" default="."/>/etc/role.properties</Set>    
                </New>    
        </Set>    
</Get>   

接着编辑文件:vim /usr/local/solr/server/solr-webapp/webapp/WEB-INF/web.xml
追加如下内容:

<security-constraint>      
    <web-resource-collection>      
        <web-resource-name>Solr</web-resource-name>
        <url-pattern>/</url-pattern>              
    </web-resource-collection>   
    <auth-constraint>      
        <role-name>admin</role-name>
    </auth-constraint>      
</security-constraint>      
<login-config>      
    <auth-method>BASIC</auth-method>         
    <realm-name>你猜猜账号密码多少啊?</realm-name>   
</login-config>

最后需要重启下solr就好了

一个实例装载同库多表

说明:有时候我们可能需要将一个数据库中的多张表融合到一个实例中去使用,当然这些表可以字段一样,也可以不一样,其实我们主要就修改data-config.xml就好了

<?xml version="1.0" encoding="UTF-8"?>
<dataConfig>
    <dataSource name="database_name" type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1:3306/database_name" user="xxxxx" password="xxxxx" />
    <document>
        <entity name="table_1_name"
            pk="id"
            dataSource="database_name"
            query="select * from table_1_name"
            deltaImportQuery="select * from table_1_name id = '${dih.delta.id}'"
            deltaQuery="select id from table_1_name where update_date > '${dataimporter.last_index_time}' and status_flag = 'success'">
            <field column="id" name="id"/>
            ......
        </entity>

        <entity name="table_2_name"
            pk="id"
            dataSource="database_name"
            query="select * from table_2_name"
            deltaImportQuery="select * from table_2_name id = '${dih.delta.id}'"
            deltaQuery="select id from table_2_name where update_date > '${dataimporter.last_index_time}' and status_flag = 'success'">
            <field column="id" name="id"/>
            ......
        </entity>
    </document>
</dataConfig>

# dataSource这个是数据源标识,两个entity代表两个不同的表,都要使用相同的数据源标识(标识名你可以自定义)
一个实例装载多库多表
<?xml version="1.0" encoding="UTF-8"?>
<dataConfig>
    <dataSource name="database_name_1" type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1:3306/database_name_1" 
                user="xxxxxx" password="xxxxxx" />
                
    <dataSource name="database_name_2" type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1:3306/database_name_2"                
                user="xxxxxx" password="xxxxxx" />
    <document>
        # 这是第一个数据源的表
        <entity name="test1"
            pk="id"
            dataSource="database_name_1"
            query="select * from table_1"
            deltaImportQuery="select * from table_1 id = '${dih.delta.id}'"
            deltaQuery="select id from table_1 where update_date > '${dataimporter.last_index_time}' and status_flag = 'success'">
            <field column="id" name="id"/>
            <field column="update_date" name="update_date"/>
        </entity>

        <entity name="test1"
            pk="id"
            dataSource="database_name_1"
            query="select * from table_2"
            deltaImportQuery="select * from table_2 id = '${dih.delta.id}'"
            deltaQuery="select id from table_2 where update_date > '${dataimporter.last_index_time}' and status_flag = 'success'">
            <field column="id" name="id"/>
            <field column="update_date" name="update_date"/>
        </entity>

        # 这是第二个数据源的表
        <entity name="test2"
            pk="id"
            dataSource="database_name_2"
            query="select * from table_3"
            deltaImportQuery="select * from table_3 id = '${dih.delta.id}'"
            deltaQuery="select id from table_3 where update_date > '${dataimporter.last_index_time}' and status_flag = 'success'">
            <field column="id" name="id"/>
            <field column="update_date" name="update_date"/>
        </entity>
        
        <entity name="test2"
            pk="id"
            dataSource="database_name_2"
            query="select * from table_4"
            deltaImportQuery="select * from table_4 id = '${dih.delta.id}'"
            deltaQuery="select id from table_4 where update_date > '${dataimporter.last_index_time}' and status_flag = 'success'">
            <field column="id" name="id"/>
            <field column="update_date" name="update_date"/>
        </entity>
    </document>
</dataConfig>

# 注意啊:多个表之前如果ID重复了,会被覆盖掉的索引,需要自己去优化
问:如何删除一个实例?

答:直接干掉这个目录,然后重启solr就OK了

问:如何进行增量索引(一般情况下,增量索引都是搞成自动最好,俗称 "自动增量索引")?

答:未完待续

上一篇 下一篇

猜你喜欢

热点阅读