solr的安装及配置详细教程/solr入门教程
1 solr的下載
從Solr官方網站(http://lucene.apache.org/solr/)下載Solr最新版本,根據Solr的運行環境,Linux下需要下載solr-7.3.1.tgz,windows下需要下載solr-7.3.1.zip。
Solr使用指南可參考:https://wiki.apache.org/solr/FrontPage。
本教程中我使用的是solr-4.10.3.zip版本。新版本大體上都是一樣的。注意solr-7.3.1需要的jdk的版本須在1.8及其以上。
2 solr的文件夾結構
bin:solr的運行腳本 ,想要運行服務的服務的時候,可以雙擊運行里面的solr.cmd文件,不過使用的自帶的jetty小服務器。
contrib:solr的一些貢獻軟件/插件,用于增強solr的功能。
dist:該目錄包含build過程中產生的war和jar文件,以及相關的依賴文件。
docs:solr的API文檔
example:solr工程的例子目錄:
example/solr:
該目錄是一個包含了默認配置信息的Solr的Core目錄。
example/multicore:
該目錄包含了在Solr的multicore中設置的多個Core目錄。
example/webapps:
該目錄中包括一個solr.war,該war可作為solr的運行實例工程。
licenses:solr相關的一些許可信息
system_requirements.txt : 系統需求信息【里面可以看到對應版本需要的jdk版本信息】
3 運行環境
solr 需要運行在一個Servlet容器中,Solr4.10.3要求jdk使用1.7以上,Solr默認提供Jetty(java寫的Servlet容器),本教程使用Tocmat作為Servlet容器,環境如下:
Solr:Solr4.10.3
Jdk:jdk1.7.0_80
Tomcat:apache-tomcat-7.0.52
4 solr整合tomcat
因為我之前已經安裝好了jdk,環境變量都已經配置好了。這里就不需要安裝jdk和配置環境變量了。
1 創建目錄
第一步:在D盤下面創建一個solr的文件夾
2 解壓tomcat到創建的目錄
第二步:解壓tomcat的安裝文件到solr目錄下。
這一步建議使用新的安裝文件解壓,不要使用之前用過的tomcat
完成后的效果:
3 解壓solr到創建的目錄
第三步:解壓solr-4.10.3.zip壓縮文件到solr目錄下
完成后的效果:
4 拷貝solr.war
第四步:拷貝solr-4.10.3里面的solr.war包到tomcat的的webapps里面去
1.solr.war在D:solrsolr-4.10.3examplewebapps目錄下
2.拷貝到D:solrapache-tomcat-7.0.52webapps里面去
5 解壓solr.war
第五步:使用解壓工具解壓solr.war包 或者 啟動tomcat服務自動完成解壓
如果啟動tomcat服務自動解壓的話,解壓完成后記得停止tomcat服務,因為我們的配置工作還沒有完成,
因此這里建議使用解壓工具手動解壓。
6 刪除solr.war
第六步:刪除tomcat下的webapps里面的solr.war包
7 拷貝jar包到工程中
第七步:把solr-4.10.3examplelibext目錄下的所有的jar包添加到solr工程中
注意:對于一個tomcat有兩個存放lib包的地方。
1.第一個地方是:你的項目下面的WEB-INF下面的lib目錄里面
2.第二個地方是:tomcat安裝目錄下的lib目錄里面
對于這兩處地方,我們建議放到你的項目下面的WEB-INF下面的lib目錄里面。
8 準備solrhome
第八步:配置solrHome 和 solrCore
1.在D:solr目錄下創建一個solrhome文件夾(存放solr所有配置文件的一個文件夾)
solr-4.10.3examplesolr目錄就是一個標準的solrhome。
2.復制solr-4.10.3examplesolr文件夾里面的全部內容到剛剛創建的solrhome目錄下面
為什么不使用solr-4.10.3examplesolr這個標準的solrhome呢?
答:如果是單機版用這個還可以。如果搭建集群我們需要很多個solrhome,你就一個源代碼包是不夠用的。
補充:
在solrhome下有一個文件夾叫做collection1這就是一個solrcore。就是一個solr的實例。一個solrcore相當于mysql中一個數據庫。Solrcore之間是相互隔離。
在solrcore中有一個文件夾叫做conf,包含了索引solr實例的配置信息。
在conf文件夾下有一個solrconfig.xml。配置實例的相關信息。如果使用默認配置可以不用做任何修改。
Xml的配置信息:
Lib:solr服務依賴的擴展包,默認的路徑是collection1lib文件夾,如果沒有 就創建一個
dataDir:配置了索引庫的存放路徑。默認路徑是collection1data文件夾,如果沒有data文件夾,會自動創建。
requestHandler:
9 配置solrhome到web.xml中
第九步:配置solrhome到我們的web.xml里面
其它瞎掰:weblogic服務器一般在我們的政府機構使用,電商一般使用的是tomcat.銀行一般使用的IBM公司的websphere,銀行使用的數據庫一般是db2.
下面正式說一下怎么配置我們的solrhome:
打開web.xml,找到下面的內容
修改為:D:solrsolrhome就是上面配置的solrhome所在的位置
注意:solr/home名稱必須是固定的。
10 啟動tomcat
第十步:進入tomcat的bin目錄,雙擊startup.bat啟動tomcat服務
11 訪問
第十一步:瀏覽器訪問localhost:8080/solr/出現下面的界面說明安裝成功了
4 安裝中文分詞器
第一步:把IKAnalyzer2012FF_u1.jar添加到solr/WEB-INF/lib目錄下。
第二步:在solr/WEB-INF/下面創建classes目錄
第三步:復制IKAnalyzer的配置文件和自定義詞典和停用詞典到solr的classpath下。即是復制到上一步創建的classes目錄下。
注意:eclipse的文本編輯是以UTF-8 無BOM格式編碼的,如果使用文本編輯器編輯推薦使用notepad++編輯,
選擇一樣的編碼格式【以UTF-8 無BOM格式編碼】,這樣可以避免,你修改ext.dic或者stopword.dic文檔之后就不好使了。
第四步:在D:solrsolrhomecollection1conf下的schema.xml文件中添加一個自定義的fieldType,使用中文分析器。
直接添加下面的代碼到配置文件里面就可以了:
<!-- IKAnalyzer-->
<fieldType name="text_ik" class="solr.TextField">
<analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
第五步:定義一個field,指定field的Type屬性為text_ik.
復制下面的代碼添加到schema.xml配置文件就可以了。
<!--IKAnalyzer Field--> <field name="title_ik" type="text_ik" indexed="true" stored="true" /> <field name="content_ik" type="text_ik" indexed="true" stored="false" multiValued="true"/>
四五兩步配置后的效果:
第六步:重啟tomcat,配置生效
第七步:測試
5 批量導入數據
使用dataimport插件批量導入數據。
第一步:把dataimport插件的依賴的jar包添加到solrcore(collection1lib)中去。
1.jar包所在位置
2.D:solrsolrhomecollection1下面沒有lib文件夾,創建一個
3.將jar包拷貝進去
第二步:導入數據庫的驅動包到solrcore(collection1lib)中去。
這里需要的數據庫的驅動包需要你自己準備
完成后的效果:
第三步:修改D:solrsolrhomecollection1conf下的solrconfig.xml文件,添加一個requestHandler。
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requestHandler>
效果:
第四步:創建一個data-config.xml,保存到D:solrsolrhomecollection1conf目錄下
1.創建data-config.xml文件。
2.復制下面的代碼到data-config.xml文件中
<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
<dataSource type="JdbcDataSource"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://39.105.94.154:3306/lucene"
user="tom"
password="tom"/>
<document>
<entity name="product" query="SELECT pid,name,catalog_name,price,description,picture FROM products ">
<field column="pid" name="id"/>
<field column="name" name="product_name"/>
<field column="catalog_name" name="product_catalog_name"/>
<field column="price" name="product_price"/>
<field column="description" name="product_description"/>
<field column="picture" name="product_picture"/>
</entity>
</document>
</dataConfig>
dataSource和entity里面的內容根據自己的實際情況修改.
對于沒有的域需要自己配置。
<!--product--> <field name="product_name" type="text_ik" indexed="true" stored="true"/> <field name="product_price" type="float" indexed="true" stored="true"/> <field name="product_description" type="text_ik" indexed="true" stored="false" /> <field name="product_picture" type="string" indexed="false" stored="true" /> <field name="product_catalog_name" type="string" indexed="true" stored="true" /> <field name="product_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/> <copyField source="product_name" dest="product_keywords"/> <copyField source="product_description" dest="product_keywords"/>
在schema.xml中加入上面的代碼,效果如下
第五步:重啟tomcat,讓配置生效。
第六步:點擊Execute按鈕導入數據。
勾選Auto-Refresh Status 可以自動刷新,看到導入的狀態
導入數據前會先清空索引庫,然后再導入。
6 Solr后臺管理界面維護索引
1 添加/修改文檔
id相同的話,會先刪除原來的,然后保存新的。
2 刪除文檔
1 刪除指定id的索引
2 刪除查詢到的索引數據
3 刪除所有的索引數據
7 solr后臺管理界面查詢索引
通過/select 搜索索引,Solr 制定一些參數完成不同需求的搜索
1 q query
1 q - 查詢字符串,必須的,如果查詢所有使用*:*。
2 fq filter query
3 sort
4 start&rows
5 fl
6 df
7 wt
9 hl
8 Solr 4.10.1 使用 SolrJ 管理索引庫
1 什么是SolrJ
solrj 是訪問 Solr 服務的 java 客戶端,提供索引和搜索的請求方法,SolrJ 通常在嵌入在業務系統中,通過 SolrJ
的 API 接口操作 Solr 服務.如下圖:
2 依賴的jar包
3 使用SolrJ添加文檔
實現步驟:
第一步:創建一個 java 工程。
第二步:導入 jar 包。
maven工程添加下面的依賴
<!-- solr客戶端 -->
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>4.10.3</version>
</dependency>
第三步:和 Solr 服務器建立連接。HttpSolrServer 對象建立連接。
第四步:創建一個 SolrInputDocument 對象,然后添加域。
第五步:將 SolrInputDocument 添加到索引庫。
第六步:提交。
//向索引庫中添加索引
@Test
public void addDocument() throws Exception {
//和solr 服務器創建連接
//參數:solr 服務器的地址,默認找的collection1,如不不是需要指明。
SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr");
//創建一個文檔對象
SolrInputDocument document = new SolrInputDocument();
//向文檔中添加域
//第一個參數:域的名稱,域的名稱必須是在 schema.xml 中定義的
//第二個參數:域的值
document.addField("id", "c0001");
document.addField("title_ik", "使用 solrJ 添加的文檔");
document.addField("content_ik", "文檔的內容");
document.addField("product_name", "商品名稱");
//把 document 對象添加到索引庫中
solrServer.add(document);
//提交修改
solrServer.commit();
}
4根據 id 刪除文檔
//刪除文檔,根據 id 刪除
@Test
public void deleteDocumentByid() throws Exception {
//創建連接
SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr");
//根據 id 刪除文檔
solrServer.deleteById("c0001");
//提交修改
solrServer.commit();
}
5根據查詢刪除文檔
查詢語法完全支持 Lucene 的查詢語法。
//根據查詢條件刪除文檔
@Test
public void deleteDocumentByQuery() throws Exception {
//創建連接
SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr");
//根據查詢條件刪除文檔,*:*刪除所有
solrServer.deleteByQuery("*:*");
//提交修改
solrServer.commit();
}
6 修改文檔
在 solrJ 中修改沒有對應的 update 方法,只有 add 方法,只需要添加一條新的文檔,和被修改的文檔 id 一致就,可以
修改了。本質上就是先刪除后添加。
7 簡單查詢
//查詢索引
@Test
public void queryIndex() throws Exception {
//創建連接
SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr");
//創建一個 query 對象
SolrQuery query = new SolrQuery();
//設置查詢條件
query.setQuery("*:*");
//也可以這樣寫:query.set("q","*:*");
//執行查詢
QueryResponse queryResponse = solrServer.query(query);
//取查詢結果
SolrDocumentList solrDocumentList = queryResponse.getResults();
//共查詢到商品數量
System.out.println("共查詢到商品數量:" + solrDocumentList.getNumFound());
//遍歷查詢的結果
for (SolrDocument solrDocument : solrDocumentList) {
System.out.println(solrDocument.get("id"));
System.out.println(solrDocument.get("product_name"));
System.out.println(solrDocument.get("product_price"));
System.out.println(solrDocument.get("product_catalog_name"));
System.out.println(solrDocument.get("product_picture"));
}
}
8 復雜查詢
其中包含查詢、過濾、分頁、排序、高亮顯示等處理。
// 復雜查詢索引
@Test
public void queryIndex2() throws Exception {
// 創建連接
SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr");
// 創建一個 query 對象
SolrQuery query = new SolrQuery();
// 設置查詢條件
query.setQuery("鉆石");
// 也可以這樣寫:query.set("q","鉆石");
// 也可以這樣寫:query.set("q", "id:1");
// 也可以這樣寫:query.set("q", "*:*");
// 過濾條件
query.setFilterQueries("product_catalog_name:幽默雜貨");
// 也可以這樣寫:query.set("fq","product_catalog_name:幽默雜貨");
// 也可以這樣寫: query.set("fq","product_price:[* TO 10]");
// 排序條件
query.setSort("product_price", ORDER.asc);
// 也可以這樣寫:query.addSort("product_price asc");
// 也可以這樣寫:query.addSort("product_price",ORDER.asc);
// 也可以這樣寫:query.set("sort", "product_price asc");
// 分頁處理
query.setStart(0);
query.setRows(10);
// 也可以這樣寫:query.set("start", 0);
// 也可以這樣寫:query.set("rows", 10);
// 結果中域的列表
query.setFields("id", "product_name", "product_price", "product_catalog_name", "product_picture");
// 也可以寫成下面這樣:query.set("fl", "product_name","product_price");
// 設置默認搜索域
query.set("df", "product_keywords");
// 打開高亮顯示
query.setHighlight(true);
// 高亮顯示的域
query.addHighlightField("product_name");
// 高亮顯示的前綴
query.setHighlightSimplePre("<span style='color:red;'>");
// 高亮顯示的后綴
query.setHighlightSimplePost("</span>");
// 執行查詢
QueryResponse queryResponse = solrServer.query(query);
// 取查詢結果
SolrDocumentList solrDocumentList = queryResponse.getResults();
// 共查詢到商品數量
System.out.println("共查詢到商品數量:" + solrDocumentList.getNumFound());
// 取高亮顯示部分的內容
// Map k id V Map
// Map k 域名 V List
// List list.get(0);
Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
// 遍歷查詢的結果
for (SolrDocument solrDocument : solrDocumentList) {
System.out.println(solrDocument.get("id"));
//看高亮顯示部分是否有此 id 的內容,有取出。沒有返回 null.
Map<String, List<String>> map = highlighting.get(solrDocument.get("id"));
// 判斷一下,防止出現空指針異常
List<String> list = null;
if (map != null) {
list = map.get("product_name");
}
// 判斷是否有高亮內容
String productName = "";
if (null != list) {
productName = list.get(0);
} else {
productName = (String) solrDocument.get("product_name");
}
System.out.println(productName);
System.out.println(solrDocument.get("product_price"));
System.out.println(solrDocument.get("product_catalog_name"));
System.out.println(solrDocument.get("product_picture"));
}
}
query.setQuery("鉆石"); 如果不指定默認搜索域,默認配置文件里面走的是text域。
可以看到默認的所搜域是text , 默認的條數是10條。
總結
以上是生活随笔為你收集整理的solr的安装及配置详细教程/solr入门教程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 软件的版本控制
- 下一篇: 怎么创建具有真实纹理的CG场景岩石?