這篇文章將為大家詳細講解有關(guān)搜索引擎solr的使用方法,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
10余年的新鄭網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。營銷型網(wǎng)站建設(shè)的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整新鄭建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)從事“新鄭網(wǎng)站設(shè)計”,“新鄭網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。
一、下載、安裝:
1. 下載地址:https://lucene.apache.org/solr/到官網(wǎng)點擊Download下載
2. 下載后解壓,如下圖
二、運行 (單機)
1. 運行win命令窗口(cmd)
2. 在solr的bin目錄下執(zhí)行啟動命令 solr start,solr應(yīng)用服務(wù)器默認端口為8983,如果想指定端口號啟動可以加參數(shù)–p例 如:solr start –p 8888
啟動成功 在瀏覽器輸入 http://localhost:8983/solr打開,如下圖:
2. solr常用命令:
solr start –p 端口號 單機版啟動solr服務(wù)
solr restart –p 端口號 重啟solr服務(wù) (注意:使用重啟命令必須帶有端口號)
solr stop –p 端口號關(guān)閉solr服務(wù)
solr create –c name 創(chuàng)建一個core實例(core概念后面介紹)
3. 注意點:
在啟動的時候如果打印java異常堆棧log4j2.xml 文件名、目錄名或卷標語法不正確,原因: log4j的bug, solr.cmd 批處理沒弄好。如下圖
這個錯不影響使用,可以忽略。也可以將 solr-7.4.0/bin/solr.cmd文件里面的所有file: 改成file:/// 就行了
三、創(chuàng)建一個core實例
1. core簡介:簡單說core就是solr的一個實例,一個solr服務(wù)下可以有多個core,每個core下都有自己的索引庫和與之相應(yīng)的配置文件,所以在操作solr創(chuàng)建索引之前要創(chuàng)建一個core,因為索引都存在core下面。
2. 創(chuàng)建core有2中方式,一種是命令 (solr create -c 創(chuàng)建的名稱),創(chuàng)建成功后會在 D:\solr-7.4.0\server\solr 目錄下出現(xiàn)你創(chuàng)建的core文件夾,如下圖:
還有一種是在solr管理頁面上創(chuàng)建,在先要在 D:\soft\solr-7.4.0\server\solr 目下創(chuàng)建文件夾并命名為你要創(chuàng)建core的名稱,然后到 D:\solr-7.4.0\example\example-DIH\solr\db 目錄下將 conf文件夾復(fù)制到你的core,最后到solr管理頁面上創(chuàng)建core,如下圖:
創(chuàng)建成功,如下圖:
四、配置managed-schema
1. managed-schema簡介
managed-schema是用來告訴solr如何建立索引的,他的配置圍繞著一個managed-schema配置文件,這個配置文件決定著solr如何建立索引,每個字段的數(shù)據(jù)類型,分詞方式等,老版本的schema配置文件的名字叫做schema.xml他的配置方式就是手工編輯,但是現(xiàn)在新版本的schema配置文件的名字叫做managed-schema,他的配置方式不再是用手工編輯而是使用schemaAPI來配置,官方給出的解釋是使用schemaAPI修改managed-schema內(nèi)容后不需要重新加載core或者重啟solr更適合在生產(chǎn)環(huán)境下維護,如果使用手工編輯的方式更改配置不進行重加載core有可能會造成配置丟失,managed-schema所在目錄為 D:\solr-7.4.0\server\solr\test1\conf,如下圖:
2. managed-schema主要成員
fieldType:為field定義類型,最主要作用是定義分詞器,分詞器決定著如何從文檔中檢索關(guān)鍵字,multiValued屬性為true時支持該類型為數(shù)組結(jié)構(gòu)。
analyzer:他是fieldType下的子元素,這就是傳說中的分詞器,他由一組tokenizer和filter組成。
field:他是創(chuàng)建索引用的字段,如果想要這個字段生成索引需要配置他的indexed屬性為true,stored屬性為true表示存儲該索引。如下圖所示每個field都要引用一種fieldType由type屬性定義,multiValued屬性為true該字段為數(shù)組,數(shù)組中每個下標的值solr都會創(chuàng)建索引
關(guān)于更多managed-schema的介紹請參考http://lucene.apache.org/solr/guide/7_4/documents-fields-and-schema-design.html
常用的數(shù)據(jù)類型基本都有,都在managed-schema文件中,搜索fieldType就可以找到
注:一般主鍵 ID 不用手寫定義 field 標簽,會自動生成,如果手動定義了在運行的時候回報錯。由于managed-schema是復(fù)制過來的 最好將文件中定義的
field 標簽?zāi)軇h除的都刪,避免和自己定義的 field 沖突,定義id的field客源不刪除,如下圖:
3. Schema API:
Schema API其實就是用post請求向solr服務(wù)器發(fā)送攜帶json參數(shù)的請求,所有操作內(nèi)容都封裝在json中,如果是linux系統(tǒng)直接使用curl工具,如果是windows系統(tǒng)推薦使用Postman
這里以添加一個field為例,下面列出其他API:
add-field: add a new field with parameters youprovide.
delete-field: delete a field.
replace-field: replace an existing field withone that is differently configured.
觀看更多API內(nèi)容請參考http://lucene.apache.org/solr/guide/7_4/schema-api.html
四、DIH導(dǎo)入索引數(shù)據(jù)
1. DIH簡介:
DIH全稱是Data Import Handler 數(shù)據(jù)導(dǎo)入處理器,顧名思義這是向solr中導(dǎo)入數(shù)據(jù)的,我們的solr目的就是為了能讓我們的應(yīng)用程序更快的查詢出用戶想要的數(shù)據(jù),而數(shù)據(jù)存儲在應(yīng)用中的各種地方入xml、pdf、關(guān)系數(shù)據(jù)庫中,那么solr首先就要能夠獲取這些數(shù)據(jù)并在這些數(shù)據(jù)中建立索引來達成快速搜索的目的,這里就列舉我們最常用的從關(guān)系型數(shù)據(jù)庫中向solr導(dǎo)入索引數(shù)據(jù)。
2. 創(chuàng)建DIH
在自己創(chuàng)建的core目錄下conf目錄下(比如:D:\solr-7.4.0\server\solr\test1\conf)有個 db-data-config.xml 文件( 如何目錄下沒有改文件可以到D:\solr-7.4.0\example\example-DIH\solr\db\conf 目下拷貝到自己的目錄下),用于連接數(shù)據(jù)庫抽取數(shù)據(jù),如下圖:
有超文本編輯器打開db-data-config.xml開始編輯,如下圖:
entity的默認屬性:
name(必需的):name是唯一的,用以標識entity
processor:只有當datasource不是RDBMS時才是必需的。默認值是SqlEntityProcessor
transformer:轉(zhuǎn)換器將會被應(yīng)用到這個entity上,詳情請瀏覽transformer部分。
pk:entity的主鍵,它是可選的,但使用“增量導(dǎo)入”的時候是必需。它跟schema.xml中定義的uniqueKey沒有必然的聯(lián)系,但它們可以相同。
rootEntity:默認情況下,document元素下就是根實體了,如果沒有根實體的話,直接在實體下面的實體將會被看做跟實體。對于根實體對應(yīng)的數(shù)據(jù)庫中返回的數(shù)據(jù)的每一行,solr都將生成一個document。
SqlEntityProcessor的屬性:
query (required) :是獲取全部數(shù)據(jù)的SQL
deltaQuery : 只在“增量導(dǎo)入”中使用,只獲取增量pk的SQL
parentDeltaQuery:只在“增量導(dǎo)入”中使用,只獲取父Entity的pk的SQL
deletedPkQuery:只在“增量導(dǎo)入”中使用,獲取當前Entity被刪除的pk
deltaImportQuery:(只在“增量導(dǎo)入”中使用) . 如果這個存在,那么它將會在“增量導(dǎo)入”中導(dǎo)入phase時代替query產(chǎn)生作用。
Full Import工作原理:
執(zhí)行本Entity的Query,獲取所有數(shù)據(jù);
針對每個行數(shù)據(jù)Row,獲取pk,組裝子Entity的Query;
執(zhí)行子Entity的Query,獲取子Entity的數(shù)據(jù)。
Delta Import工作原理:
查找子Entity,直到?jīng)]有為止;
執(zhí)行Entity的deltaQuery,獲取變化數(shù)據(jù)的pk;
合并子Entity parentDeltaQuery得到的pk;
針對每一個pk Row,組裝父Entity的parentDeltaQuery;
執(zhí)行parentDeltaQuery,獲取父Entity的pk;
執(zhí)行deltaImportQuery,獲取自身的數(shù)據(jù);
如果沒有deltaImportQuery,就組裝Query
限制:
子Entity的query必須引用父Entity的pk
子Entity的parentDeltaQuery必須引用自己的pk
子Entity的parentDeltaQuery必須返回父Entity的pk
deltaImportQuery引用的必須是自己的pk
在solrconfig.xml(目錄 D:\solr-7.4.0\server\solr\test1\conf)文件最下面增加配置:
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"> <lst name="defaults"> <str name="config">db-data-config.xml</str> </lst> </requestHandler>
如下圖:
在創(chuàng)建的core目錄下(D:\solr-7.4.0\server\solr\test1)創(chuàng)建 lib 文件夾,將數(shù)據(jù)庫驅(qū)動jar包復(fù)制進去。在D:\soft\solr-7.4.0\dist文件夾下的solr-dataimporthandler-4.5.1.jar和solr-dataimporthandler-extras-4.5.1.jar也復(fù)制進去(這2個jar不一定需要復(fù)制,跟進啟動后看日志是否報 沒有這個類 org.apache.solr.handler.dataimport.DataImportHandler,報了就復(fù)制進去)
之后重啟solr,如下圖:
重啟成功之后打開solr管理,選擇創(chuàng)建的core并抽取數(shù)據(jù),可以在D:\solr-7.4.0\server\logs下查詢?nèi)罩居涗?,如下圖:
執(zhí)行成功之后,查看結(jié)果,如下圖:
刪除solr數(shù)據(jù),如下圖:
<delete><query>*:*</query></delete> <commit/>
也可以使用 訪問url刪除
根據(jù)ID刪除
http://localhost:8080/solr/update/?stream.body=
<delete><id>id值</id></delete>&stream.contentType=text/xml;charset=utf-8&commit=true
根據(jù)查詢條件查詢
http://localhost:8080/solr/update/?stream.body=
<delete><query>參數(shù)</query></delete>&stream.contentType=text/xml;charset=utf-8&commit=true
五、Solr中啟用定時器自動更新
下載 solr-dataimport-scheduler.jar 將其復(fù)制到 D:\solr-7.4.0\server\solr-webapp\webapp\WEB-INF\lib 下,如下圖:
找到web.xml (D:\solr-7.4.0\server\solr-webapp\webapp\WEB-INF)在第一個servlet標簽添加
<listener> <listener-class> org.apache.solr.handler.dataimport.scheduler.ApplicationListener </listener-class> </listener>
如下圖:
在D:\solr-7.4.0\server\solr目錄下成交conf文件夾,之后再conf下創(chuàng)建dataimport.properties文件,如下圖:
編輯dataimport.properties文件
################################################# # # # dataimport scheduler properties # # # ################################################# # tosync or not to sync # 1- active; anything else - inactive # 這里的配置不用修改 syncEnabled=1 # which cores to schedule # ina multi-core environment you can decide which cores you want syncronized # leave empty or comment it out if using single-core deployment # 修改成你所使用的core,我這里是我自定義的core:test1 syncCores=test1 # solr server name or IP address # [defaults to localhost if empty] 這個一般都是localhost不會變 server=localhost # solr server port # [defaults to 80 if empty] # 安裝solr的tomcat端口,如果你使用的是默認的端口,就不用改了,否則改成自己的端口就好了 port=8983 # application name/context # [defaults to current ServletContextListener's context (app) name] # 這里默認不改 webapp=solr # URL params [mandatory] # remainder of URL # 這里改成下面的形式,solr同步數(shù)據(jù)時請求的鏈接 # command=delta-import 是增量抽取,command=full-import 是全量抽取 #params=/dataimport?command=delta-import&clean=false&commit=true params=/dataimport?command=full-import&clean=false&commit=true # schedule interval # number of minutes between two runs # [defaults to 30 if empty] #這里是設(shè)置定時任務(wù)的,單位是分鐘,也就是多長時間你檢測一次數(shù)據(jù)同步,根據(jù)項目需求修改 # 開始測試的時候為了方便看到效果,時間可以設(shè)置短一點 interval=1 # 重做索引的時間間隔,單位分鐘,默認7200,即5天; # 為空,為0,或者注釋掉:表示永不重做索引 reBuildIndexInterval=7200 # 重做索引的參數(shù) reBuildIndexParams=/select?qt=/dataimport&command=full-import&clean=true&commit=true # 重做索引時間間隔的計時開始時間,第一次真正執(zhí)行的時間=reBuildIndexBeginTime+reBuildIndexInterval*60*1000; # 兩種格式:2012-04-11 03:10:00 或者 03:10:00,后一種會自動補全日期部分為服務(wù)啟動時的日期 reBuildIndexBeginTime=03:10:00
在重啟之后dataimport.properties文件注釋會都沒有了,如下圖:
之后重新啟動solr,之后定時就啟用了。
六、集成到項目中
maven引入jar,注:如果有特殊需要需要引入httpclient 那要注意httpclient的版本與solr的版本兼容,solr7.4的httpclient不得低于4.5.3
<dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> <version>7.4.0</version> </dependency> <!-- <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.3</version> </dependency> -->
spring-solr.xml
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!--配置Solr,一下2中方式都可以--> <!-- <bean id="builder" class="org.apache.solr.client.solrj.impl.HttpSolrClient.Builder"> </bean> <bean id="solr" class="org.apache.solr.client.solrj.impl.HttpSolrClient"> <constructor-arg name="builder" value="builder" /> <property name="baseURL" value="http://localhost:8983/solr/db" /> </bean> --> <bean id="builder" class="org.apache.solr.client.solrj.impl.HttpSolrClient.Builder"> <constructor-arg name="baseSolrUrl" value="http://localhost:8983/solr/db" /> </bean> <bean factory-bean="builder" factory-method="build" id="httpSolrClient"> </bean> </beans>
在spring的ApplicationContext.xml增加
<import resource="spring-solr.xml"/>
在Java中簡單使用
package com.solr; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.impl.HttpSolrClient; import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.client.solrj.response.UpdateResponse; import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocumentList; import org.apache.solr.common.SolrInputDocument; import java.io.IOException; public class SolrTest { private static HttpSolrClient httpSolrClient; /** * 新增或更新 */ public void save() throws IOException, SolrServerException { SolrInputDocument document = new SolrInputDocument(); //注意是SolrInputDocument,而非SolrDocument //可為文檔指定id,即addField("id","主鍵id"),如果id相同則操作為更新。 document.addField("id", "123"); document.addField("name", "54151"); //key一直會在solr 將 type構(gòu)造成數(shù)組結(jié)構(gòu) document.addField("type", 555); document.addField("type", 432434); UpdateResponse responseAdd = httpSolrClient.add(document); //這里也不一樣,不像以往版本的提交 httpSolrClient.commit(); //提交 System.out.println("save成功"); } /** * 查詢 */ public void query() throws IOException, SolrServerException { //聲明查詢對象,并設(shè)置查詢條件 SolrQuery query = new SolrQuery(); query.set("q", "id:123"); //執(zhí)行查詢 QueryResponse response = httpSolrClient.query(query); //獲取查詢結(jié)果 SolrDocumentList documentList = response.getResults(); for (SolrDocument document : documentList) { System.out.println("查詢到name:" + document.get("name")); System.out.println("ID:" + document.get("id")); System.out.println(); } } /** * 刪除 */ public void delete() throws IOException, SolrServerException { httpSolrClient.deleteByQuery("id:123"); //這是根據(jù)查詢條件刪除,也可根據(jù)id刪除 httpSolrClient.commit(); System.out.println("刪除成功"); } }
關(guān)于搜索引擎solr的使用方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
新聞名稱:搜索引擎solr的使用方法
轉(zhuǎn)載來于:http://www.rwnh.cn/article2/jgpoic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機網(wǎng)站建設(shè)、營銷型網(wǎng)站建設(shè)、移動網(wǎng)站建設(shè)、軟件開發(fā)、定制開發(fā)、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)