一:下載solr5.3安裝包;點(diǎn)擊 solr
二:安裝
1:解壓安裝tomcat ,本文中使用 tomcat7.0
安裝路徑 :D:\tomcat\apache-tomcat-7.0.40
2:解壓 安裝 solr 5.3.1星立;
本文安裝路徑: D:\solr\solr-5.3.1
3:將 solr-5.3.1\server\solr-webapp 文件夾底下的 webapp 復(fù)制到 tomcat 對(duì)應(yīng)目錄底下的 webapps 中,并將文件夾名字改為 solr
本文路徑:D:\tomcat\apache-tomcat-7.0.40\webapps\solr
4:將 solr-5.3.1\server\lib\ext 文件夾底下的lib全部復(fù)制到tomcat底下的 solr/WEB-INF/lib/ 中
本文路徑:D:\tomcat\apache-tomcat-7.0.40\webapps\solr\WEB-INF\lib
5:將solr-5.3.1\server\resources 下的log4j.properties文件復(fù)制到tomcat/webapps/solr/WEB-INF/classes目錄下罩息,如果該目錄不存在則新建借宵。
6:將solr-5.3.1\server 下的 solr 文件夾復(fù)制到D:\tomcat\apache-tomcat-7.0.40\bin 目錄下 , 這個(gè)就是 solr/home(存放的檢索數(shù)據(jù))
7:設(shè)置solr/home 雪侥, 編輯 D:\tomcat\apache-tomcat-7.0.40\webapps\solr\WEB-INF\web.xml ;
solr 啟動(dòng)的時(shí)候會(huì)去這個(gè)目錄下加載配置信息
<env-entry>
<env-entry-name>solr/home</env-entry-name>
<env-entry-value>D:/tomcat/apache-tomcat-7.0.40/bin/solr</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>
三:?jiǎn)?dòng)tomcat http://localhost:9090/solr 查看是否配置成功
四: 添加自定義 solr
1: 在剛才 定義的 solr/ home 中 新建一個(gè)文件夾 core
如 本文路徑:D:\tomcat\apache-tomcat-7.0.40\bin\solr\core
2:在core 目錄下新建 data 文件夾 精绎,將 D:\tomcat\apache-tomcat-7.0.40\bin\solr\configsets\basic_configs 目錄下的 conf 文件夾復(fù)制到 core 目錄下
3:在sorl 控制臺(tái) 點(diǎn)擊 add core
五:配置中文分詞(mmseg4j)
1:下載jar 包 (mmseg4j-core-1.10.0.jar速缨、mmseg4j-solr-2.3.0.jar),并復(fù)制到tomcat底下的 solr/WEB-INF/lib/
這里包貌似不太好找代乃,可以下載 solr-core 包旬牲,里面會(huì)包含這兩個(gè)包
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-core</artifactId>
<version>5.3.1</version>
</dependency>
2:D:\tomcat\apache-tomcat-7.0.40\bin\solr\core\conf\schema.xml 新增
<fieldtype name="textComplex" class="solr.TextField" positionIncrementGap="100">
<analyzer>
<tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="complex" dicPath="dic"/>
</analyzer>
</fieldtype>
<fieldtype name="textMaxWord" class="solr.TextField" positionIncrementGap="100">
<analyzer>
<tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="max-word" />
</analyzer>
</fieldtype>
<fieldtype name="textSimple" class="solr.TextField" positionIncrementGap="100">
<analyzer>
<tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="simple" dicPath="d:/my_dic" />
</analyzer>
</fieldtype>
六:java 中調(diào)用
1: 在上面說的schema.xml中,添加
<field name="content_test" type="textMaxWord" indexed="true" stored="true" multiValued="true"/>
2:新建測(cè)試類
// 需要導(dǎo)入 上文中的 solr-core 包即可
public class App
{
//solr url
public static final String URL = "http://127.0.0.1:9090/solr";
//solr應(yīng)用
public static final String SERVER = "my_solr";
//待索引搁吓、查詢字段
public static String[] docs = {"Solr是一個(gè)獨(dú)立的企業(yè)級(jí)搜索應(yīng)用服務(wù)器",
"用戶可以通過http請(qǐng)求",
"向搜索引擎服務(wù)器提交一定格式的XML文件生成索引",
"也可以通過Http Get操作提出查找請(qǐng)求"};
public static SolrClient getSolrClient(){
return new HttpSolrClient(URL+"/"+SERVER);
}
/**
* 新建索引
*/
public static void ctIndex(){
SolrClient client = getSolrClient();
int i = 0;
List<SolrInputDocument> docList = new ArrayList<SolrInputDocument>();
for(String str : docs){
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id",i++);
doc.addField("content_test", str);
docList.add(doc);
}
try {
client.add(docList);
client.commit();
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
};
/**
* 搜索
*/
public static void search(){
SolrClient client = getSolrClient();
SolrQuery query = new SolrQuery();
query.setQuery("content_test:搜索"); //搜索
QueryResponse response = null;
try {
response = client.query(query);
System.out.println(response.toString());
System.out.println();
SolrDocumentList docs = response.getResults();
System.out.println("文檔個(gè)數(shù):" + docs.getNumFound());
System.out.println("查詢時(shí)間:" + response.getQTime());
for (SolrDocument doc : docs) {
System.out.println("id: " + doc.getFieldValue("id") + " content: " + doc.getFieldValue("content_test"));
}
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ctIndex();
search();
}
}
3:查看執(zhí)行結(jié)果
// 其中找到 帶"搜索" 的 文檔個(gè)數(shù) 2個(gè)
{responseHeader={status=0,QTime=7,params={q=content_test:搜索,wt=javabin,version=2}},response={numFound=2,start=0,docs=[SolrDocument{id=0, content_test=[Solr是一個(gè)獨(dú)立的企業(yè)級(jí)搜索應(yīng)用服務(wù)器], _version_=1538905774611234816}, SolrDocument{id=2, content_test=[向搜索引擎服務(wù)器提交一定格式的XML文件生成索引], _version_=1538905774615429120}]}}
文檔個(gè)數(shù):2
查詢時(shí)間:7
id: 0 content: [Solr是一個(gè)獨(dú)立的企業(yè)級(jí)搜索應(yīng)用服務(wù)器]
id: 2 content: [向搜索引擎服務(wù)器提交一定格式的XML文件生成索引]
七: 與數(shù)據(jù)庫整合
1: 以mysql作為示例原茅。找到 D:\tomcat\apache-tomcat-7.0.40\bin\solr\core\conf\ solrconfig.xml 新增以下代碼
// 特別注意 : 這里需要在 D:\tomcat\apache-tomcat-7.0.40\webapps\solr\WEB-INF\lib 下導(dǎo)入 solr-dataimporthandler-5.3.1.jar
<!--這個(gè)主要用來導(dǎo)入數(shù)據(jù)庫 的配置文件-->
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requestHandler>
2: 在D:\tomcat\apache-tomcat-7.0.40\bin\solr\core\conf 下新建 data-config.xml 文件
<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
<!--這里配置自己的數(shù)據(jù)庫信息-->
<dataSource name="solrDB" type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/demo" user="root" password="123456" batchSize="-1" />
<document>
<entity name="student" dataSource="solrDB" query="SELECT * from student">
<field column="stuId" name="id" />
<field column="stuName" name="stuName" />
<field column="age" name="age" />
<field column="sex" name="sex" />
</entity>
</document>
</dataConfig>
3: 修改 D:\tomcat\apache-tomcat-7.0.40\bin\solr\my_solr\conf\schema.xml ,新增如下代碼
<!--這里的name 注意和上面的配置 對(duì)應(yīng)-->
<field name="stuName" type="string" indexed="true" stored="true" multiValued="true"/>
<field name="age" type="int" indexed="true" stored="true" />
<field name="sex" type="string" indexed="true" stored="true" />
4:打開solr 控制臺(tái) ,點(diǎn)擊左側(cè)菜單“Dataimport“堕仔,默認(rèn)勾選項(xiàng)即可擂橘,點(diǎn)擊”Excute“按鈕,這時(shí)會(huì)按照剛才的配置導(dǎo)入相應(yīng)的數(shù)據(jù)到solr中 摩骨,這里貌似不會(huì)自己刷新通贞,你可以點(diǎn)擊下面的 Refresh status 查看是否導(dǎo)入完成朗若, 同時(shí)你也可以在 左側(cè)導(dǎo)航欄, Logging 中查看導(dǎo)入日志
5:導(dǎo)入成功以后滑频,可以點(diǎn)擊左側(cè)導(dǎo)航欄 Query 捡偏,在Q 欄中輸入 字段 :值, 進(jìn)行查詢