第一步:安裝啟動
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數(shù)據(jù)庫配置文件
<?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:是獲取全部數(shù)據(jù)的SQL
deltaImportQuery:是獲取增量數(shù)據(jù)時使用的SQL
deltaQuery:是獲取pk的SQL
field:column數(shù)據(jù)庫字段名坦弟,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是系統(tǒng)保留的名字炕檩,比如“version”
type 類型,對應(yīng)于fieldType的name
default 該field的缺省值
indexed 是否為該field建立索引,以讓用戶可以搜索它豺型、統(tǒng)計它(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索引措译,以提高某些特殊搜索的效率(排序别凤、統(tǒng)計、高亮)
第五步:導(dǎo)入必備的三個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目錄下找
將這三個包導(dǎo)入到 /usr/local/solr/server/solr-webapp/webapp/WEB-INF/lib
第六步:重啟導(dǎo)入數(shù)據(jù)
solr restart -force
第七步:添加中文分詞规哪,通常情況下都是需要分詞的,一般使用IK分詞居多塌衰,還有庖丁之類的分詞
下載分詞器 https://search.maven.org/search?q=g:com.github.magese
將分詞器的jar包導(dǎo)入/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是不對外網(wǎng)的诉稍,但是有時候為了測試什么的,還是需要用一下最疆,所以可以配置用戶名杯巨、密碼來防止其他人直接操作你的solr,最重要的是可以直接看見你的數(shù)據(jù)用戶名肚菠、密碼舔箭;
首先創(chuàng)建配置文件:touch /usr/local/solr/server/etc/role.properties
追加如下內(nèi)容:用戶為test 密碼123 權(quán)限為admin)
test: 123,admin
然后編輯文件:vim /usr/local/solr/server/contexts/solr-jetty-context.xml
追加如下內(nèi)容:
<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
追加如下內(nèi)容:
<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就好了
一個實例裝載同庫多表
說明:有時候我們可能需要將一個數(shù)據(jù)庫中的多張表融合到一個實例中去使用,當(dāng)然這些表可以字段一樣镜会,也可以不一樣檬寂,其實我們主要就修改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這個是數(shù)據(jù)源標(biāo)識,兩個entity代表兩個不同的表戳表,都要使用相同的數(shù)據(jù)源標(biāo)識(標(biāo)識名你可以自定義)
一個實例裝載多庫多表
<?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>
# 這是第一個數(shù)據(jù)源的表
<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>
# 這是第二個數(shù)據(jù)源的表
<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重復(fù)了,會被覆蓋掉的索引匾旭,需要自己去優(yōu)化
問:如何刪除一個實例镣屹?
答:直接干掉這個目錄,然后重啟solr就OK了
問:如何進(jìn)行增量索引(一般情況下价涝,增量索引都是搞成自動最好女蜈,俗稱 "自動增量索引")?
答:未完待續(xù)