溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Solr使用方法是什么

發(fā)布時間:2021-12-22 17:42:22 來源:億速云 閱讀:145 作者:iii 欄目:云計算

這篇文章主要介紹“Solr使用方法是什么”,在日常操作中,相信很多人在Solr使用方法是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Solr使用方法是什么”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

由于搜索引擎功能在門戶社區(qū)中對提高用戶體驗(yàn)有著重在門戶社區(qū)中涉及大量需要搜索引擎的功能需求,目前在實(shí)現(xiàn)搜索引擎的方案上有集中方案可供選擇:

1. 基于Lucene自己進(jìn)行封裝實(shí)現(xiàn)站內(nèi)搜索。工作量及擴(kuò)展性都較大,不采用。

2. 調(diào)用Google、Baidu的API實(shí)現(xiàn)站內(nèi)搜索。同第三方搜索引擎綁定太死,無法滿足后期業(yè)務(wù)擴(kuò)展需要,暫時不采用。

3. 基于Compass+Lucene實(shí)現(xiàn)站內(nèi)搜索。適合于對數(shù)據(jù)庫驅(qū)動的應(yīng)用數(shù)據(jù)進(jìn)行索引,尤其是替代傳統(tǒng)的like ‘%expression%’來實(shí)現(xiàn)對varchar或clob等字段的索引,對于實(shí)現(xiàn)站內(nèi)搜索是一種值得采納的方案。但在分布式處理、接口封裝上尚需要自己進(jìn)行一定程度的封裝,暫時不采用。

4. 基于Solr實(shí)現(xiàn)站內(nèi)搜索。封裝及擴(kuò)展性較好,提供了較為完備的解決方案,因此在門戶社區(qū)中采用此方案,后期加入Compass方案。

1、 Solr簡介

Solr是一個基于Lucene的Java搜索引擎服務(wù)器。Solr 提供了層面搜索、命中醒目顯示并且支持多種輸出格式(包括 XML/XSLT 和 JSON 格式)。它易于安裝和配置,而且附帶了一個基于 HTTP 的管理界面。Solr已經(jīng)在眾多大型的網(wǎng)站中使用,較為成熟和穩(wěn)定。Solr 包裝并擴(kuò)展了 Lucene,所以Solr的基本上沿用了Lucene的相關(guān)術(shù)語。更重要的是,Solr 創(chuàng)建的索引與 Lucene 搜索引擎庫完全兼容。通過對Solr 進(jìn)行適當(dāng)?shù)呐渲?,某些情況下可能需要進(jìn)行編碼,Solr 可以閱讀和使用構(gòu)建到其他 Lucene 應(yīng)用程序中的索引。此外,很多 Lucene 工具(如Nutch、 Luke)也可以使用Solr 創(chuàng)建的索引。


2、 Tomcat下Solr安裝配置
由于Solr基于java開發(fā),因此Solr在windows及Linux都能較好部署使用,但由于Solr提供了一些用于測試及管理、維護(hù)較為方便的shell腳本,因此在生產(chǎn)部署時候建議安裝在Linux上,測試時候可以在windows使用。

下面以Linux下安裝配置Solr進(jìn)行說明,windows與此類似。
wget http://apache.mirror.phpchina.com/tomcat/tomcat-6/v6.0.16/bin/apache-tomcat-6.0.16.zip
unzip apache-tomcat-6.0.16.zip
mv apache-tomcat-6.0.16 /opt/tomcat
chmod 755 /opt/tomcat/bin/*
wget http://apache.mirror.phpchina.com/lucene/solr/1.2/apache-solr-1.2.0.tgz
tar zxvf apache-solr-1.2.0.tgz

Solr的安裝配置最為麻煩的是對solr.solr.home的理解和配置,主要有三種

基于當(dāng)前路徑的方式 
cp apache-solr-1.2.0/dist/apache-solr-1.2.0.war /opt/tomcat/webapps/solr.war
mkdir /opt/solr-tomcat
cp -r apache-solr-1.2.0/example/solr/ /opt/solr-tomcat/
cd /opt/solr-tomcat
/opt/tomcat/bin/startup.sh

由于在此種情況下(沒有設(shè)定solr.solr.home環(huán)境變量或JNDI的情況下),Solr查找./solr,因此在啟動時候需要切換到/opt/solr-tomcat

基于環(huán)境變量solr.solr.home
在當(dāng)前用戶的環(huán)境變量中(.bash_profile)或在/opt/tomcat/catalina.sh中添加如下環(huán)境變量
export JAVA_OPTS="$JAVA_OPTS -Dsolr.solr.home=/opt/solr-tomcat/solr"

基于JNDI配置 
mkdir –p /opt/tomcat/conf/Catalina/localhost
touch /opt/tomcat/conf/Catalina/localhost/solr.xml ,內(nèi)容如下:

<Context docBase="/opt/tomcat/webapps/solr.war" debug="0" crossContext="true" >  
  <Environment name="solr/home" type="java.lang.String" value="/opt/solr-tomcat/solr" override="true" />  
</Context>

訪問solr管理界面 http://ip:port/solr


3、 Solr原理 

Solr對外提供標(biāo)準(zhǔn)的http接口來實(shí)現(xiàn)對數(shù)據(jù)的索引的增加、刪除、修改、查詢。在 Solr 中,用戶通過向部署在servlet 容器中的 Solr Web 應(yīng)用程序發(fā)送 HTTP 請求來啟動索引和搜索。Solr 接受請求,確定要使用的適當(dāng)SolrRequestHandler,然后處理請求。通過 HTTP 以同樣的方式返回響應(yīng)。默認(rèn)配置返回Solr 的標(biāo)準(zhǔn) XML 響應(yīng),也可以配置Solr 的備用響應(yīng)格式。

可以向 Solr 索引 servlet 傳遞四個不同的索引請求:
add/update 允許向 Solr 添加文檔或更新文檔。直到提交后才能搜索到這些添加和更新。
commit 告訴 Solr,應(yīng)該使上次提交以來所做的所有更改都可以搜索到。
optimize 重構(gòu) Lucene 的文件以改進(jìn)搜索性能。索引完成后執(zhí)行一下優(yōu)化通常比較好。如果更新比較頻繁,則應(yīng)該在使用率較低的時候安排優(yōu)化。一個索引無需優(yōu)化也可以正常地運(yùn)行。優(yōu)化是一個耗時較多的過程。
delete 可以通過 id 或查詢來指定。按 id 刪除將刪除具有指定 id 的文檔;按查詢刪除將刪除查詢返回的所有文檔。 

一個典型的Add請求報文

<add>  
  <doc>  
    <field name="id">TWINX2048-3200PRO</field>  
    <field name="name">CORSAIR  XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail</field>  
    <field name="manu">Corsair Microsystems Inc.</field>  
    <field name="cat">electronics</field>  
    <field name="cat">memory</field>  
    <field name="features">CAS latency 2, 2-3-3-6 timing, 2.75v, unbuffered, heat-spreader</field>  
    <field name="price">185</field>  
    <field name="popularity">5</field>  
    <field name="inStock">true</field>  
  </doc>  
  <doc>  
    <field name="id">VS1GB400C3</field>  
    <field name="name">CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail</field>  
    <field name="manu">Corsair Microsystems Inc.</field>  
    <field name="cat">electronics</field>  
    <field name="cat">memory</field>  
    <field name="price">74.99</field>  
    <field name="popularity">7</field>  
    <field name="inStock">true</field>  
  </doc>  
</add>

一個典型的搜索結(jié)果報文:

<response>  
    <lst name="responseHeader">  
        <int name="status">0</int>  
        <int name="QTime">6</int>  
        <lst name="params">  
            <str name="rows">10</str>  
            <str name="start">0</str>  
            <str name="fl">*,score</str>  
            <str name="hl">true</str>  
            <str name="q">content:"faceted browsing"</str>  
        </lst>  
    </lst>  
  
    <result name="response" numFound="1" start="0" maxScore="1.058217">  
        <doc>  
            <float name="score">1.058217</float>  
            <arr name="all">  
                <str>http://localhost/myBlog/solr-rocks-again.html</str>  
                <str>Solr is Great</str>  
                <str>solr,lucene,enterprise,search,greatness</str>  
                <str>Solr has some really great features, like faceted browsing and replication</str>  
            </arr>  
            <arr name="content">  
                <str>Solr has some really great features, like faceted browsing and replication</str>  
            </arr>  
            <date name="creationDate">2007-01-07T05:04:00.000Z</date>  
            <arr name="keywords">  
                <str>solr,lucene,enterprise,search,greatness</str>  
            </arr>  
            <int name="rating">8</int>  
            <str name="title">Solr is Great</str>  
            <str name="url">http://localhost/myBlog/solr-rocks-again.html</str>  
        </doc>  
    </result>  
  
    <lst name="highlighting">  
        <lst name="http://localhost/myBlog/solr-rocks-again.html">  
            <arr name="content">  
                <str>Solr has some really great features, like <em>faceted</em>  
                <em>browsing</em> and replication</str>  
            </arr>  
        </lst>  
    </lst>  
</response>

關(guān)于solr的詳細(xì)使用說明,請參考
http://wiki.apache.org/solr/FrontPage

4、 Solr測試使用 

Solr的安裝包包含了相關(guān)的測試樣例,路徑在apache-solr-1.2.0/example/exampledocs

1. 使用shell腳本(curl)測試Solr的操作:
cd apache-solr-1.2.0/example/exampledocs
vi post.sh,根據(jù)tomcat的ip、port修改URL變量的值URL=http://localhost:8080/solr/update
./post.sh *.xml                 # 

2. 使用Solr的java 包測試Solr的操作:
查看幫助:java -jar post.jar –help
提交測試數(shù)據(jù):
java -Durl=http://localhost:8080/solr/update -Ddata=files -jar post.jar  *.xml     

下面以增加索引字段liangchuan、url為例,說明一下Solr中索引命令的使用

1) 修改solr的schema,配置需要索引字段的說明:
vi /opt/solr-tomcat/solr/conf/schema.xml ,在<fields>中增加如下內(nèi)容

<field name="liangchuan"  type="string" indexed="true" stored="true"/>  
<field name="url"  type="string" indexed="true" stored="true"/>

2) 創(chuàng)建增加索引請求的xml測試文件
touch /root/apache-solr-1.2.0/example/exampledocs/liangchuan.xml,內(nèi)容如下:

<add>  
  <doc>  
    <field name="id">liangchuan000</field>  
    <field name="name">Solr, the Enterprise Search Server</field>  
    <field name="manu">Apache Software Foundation</field>  
    <field name="liangchuan">liangchuan's solr "hello,world" test</field>  
    <field name="url">http://www.google.com</field>  
  </doc>  
</add>

3) 提交索引請求
cd apache-solr-1.2.0/example/exampledocs
./post.sh liangchuan.xml

4) 查詢
通過solr的管理員界面http://localhost:8080/solr/admin查詢
或通過curl 測試:
export URL="http://localhost:8080/solr/select/"
curl "$URL?indent=on&q=liangchuan&fl=*,score"

5、Solr查詢條件參數(shù)說明
參數(shù)     描述     示例
q    

Solr 中用來搜索的查詢??梢酝ㄟ^追加一個分號和已索引且未進(jìn)行斷詞的字段的名稱來包含排序信息。默認(rèn)的排序是 score desc,指按記分降序排序。    
q=myField:Java AND otherField:developerWorks; date asc
此查詢搜索指定的兩個字段并根據(jù)一個日期字段對結(jié)果進(jìn)行排序。

start     將初始偏移量指定到結(jié)果集中??捎糜趯Y(jié)果進(jìn)行分頁。默認(rèn)值為 0。     
start=15
返回從第 15 個結(jié)果開始的結(jié)果。

rows     返回文檔的最大數(shù)目。默認(rèn)值為 10。     rows=25

fq     提供一個可選的篩選器查詢。查詢結(jié)果被限制為僅搜索篩選器查詢返回的結(jié)果。篩選過的查詢由 Solr 進(jìn)行緩存。它們對提高復(fù)雜查詢的速度非常有用。
任何可以用 q 參數(shù)傳遞的有效查詢,排序信息除外。

hl     當(dāng) hl=true 時,在查詢響應(yīng)中醒目顯示片段。默認(rèn)為 false。參看醒目顯示參數(shù)上的 Solr Wiki 部分可以查看更多選項     hl=true

fl     作為逗號分隔的列表指定文檔結(jié)果中應(yīng)返回的 Field 集。默認(rèn)為 “*”,指所有的字段?!皊core” 指還應(yīng)返回記分。     
*,score


Solr的查詢條件參數(shù)q的格式與Lucene相同

6、 在門戶社區(qū)中solr使用模式 
在門戶社區(qū)中需要使用solr,可采用如下模式:

對原有系統(tǒng)已有的數(shù)據(jù)或需要索引的數(shù)據(jù)量較大的情況 
直接采用通過http方式調(diào)用solr的接口方式,效率較差,采用solr本身對csv 的支持,將數(shù)據(jù)導(dǎo)出為csv格式,然后調(diào)用solr的csv接口http://localhost:8080/solr/update/csv

對系統(tǒng)新增的數(shù)據(jù) 
先將需要索引查詢的數(shù)據(jù)組裝成xml格式,然后使用httpclient 將數(shù)據(jù)提交到solr 的http接口,例如   
http://localhost:8080/solr/update

到此,關(guān)于“Solr使用方法是什么”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI