日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Solr拼写检查示例

發(fā)布時間:2023/12/8 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Solr拼写检查示例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本文英文版地址:https://examples.javacodegeeks.com/enterprise-java/apache-solr/solr-spellcheck-example/

翻譯備注:本文采用谷歌翻譯,請有時間的同學(xué)可以從英文版學(xué)習(xí)。

在這個Solr Spellcheck示例中,我們將討論如何實現(xiàn)Apache Solr提供的拼寫檢查功能。我們將向您展示如何在Solr中配置拼寫檢查,并討論用于獲取拼寫檢查建議的查詢參數(shù)。為了演示Solr Spellcheck示例,我們將使用基本配置在Solr中創(chuàng)建一個核心,并索引與Solr安裝一起提供的示例文件。

這個例子的首選環(huán)境是solr-5.0.0。在開始Solr安裝之前,請確保已安裝JDK并已正確設(shè)置Java_Home。

1.安裝Apache Solr

首先,讓我們從以下位置下載最新版本的Apache Solr:

http://lucene.apache.org/solr/downloads.html

Apache Solr已經(jīng)經(jīng)歷了從4.xx到5.0.0的各種更改,因此如果您有不同版本的Solr,則需要下載5.xx版本以遵循此示例。

下載Solr zip文件后,將其解壓縮到文件夾中。 提取的文件夾將如下所示:

Solr文件夾

bin文件夾包含用于啟動和停止服務(wù)器的腳本。 example文件夾包含幾個示例文件。 我們將使用其中一個來演示Solr如何對數(shù)據(jù)進(jìn)行索引。server文件夾包含用于寫入所有Solr日志的logs文件夾。在索引期間檢查日志中的任何錯誤將有所幫助。 服務(wù)器下的solr文件夾包含不同的集合或核心。 每個核/集合的配置和數(shù)據(jù)存儲在相應(yīng)的核/集合文件夾中。

Apache Solr附帶了一個內(nèi)置的Jetty服務(wù)器。 但在我們啟動solr實例之前,我們必須驗證JAVA_HOME是否在機(jī)器上設(shè)置。

我們可以使用命令行腳本啟動服務(wù)器。 讓我們從命令提示符轉(zhuǎn)到bin目錄并發(fā)出以下命令:

solr start

這將在默認(rèn)端口8983下啟動Solr服務(wù)器。

我們現(xiàn)在可以在瀏覽器中打開以下URL,并驗證我們的Solr實例正在運行。 solr管理工具的具體細(xì)節(jié)超出了示例的范圍。

http://localhost:8983/solr/

Solr管理控制臺

2.配置Apache Solr

在本節(jié)中,我們將向您展示如何配置solr實例的核心/集合以及如何定義字段。 Apache Solr附帶一個名為Schemaless模式的選項。 此選項允許用戶構(gòu)建有效的模式,而無需手動編輯模式文件。但是對于這個例子,我們將使用Schema配置來理解Solr的內(nèi)部。

當(dāng)Solr服務(wù)器在獨立模式下啟動時,配置被稱為核心,當(dāng)它在SolrCloud模式下啟動時,配置稱為集合。在這個例子中,我們將討論獨立的服務(wù)器和核心。 我們將暫停SolrCloud討論以供日后使用。

首先,我們需要創(chuàng)建一個用于索引數(shù)據(jù)的Core。 Solr create命令有以下選項:

  • -c <name> - 要創(chuàng)建的核心或集合的名稱(必需)。
  • -d <confdir> - 配置目錄,在SolrCloud模式下有用。
  • -n <configName> - 配置名稱。 此默認(rèn)值與核心或集合的名稱相同。
  • -p <port> - 要向其發(fā)送create命令的本地Solr實例的端口;默認(rèn)情況下,腳本嘗試通過查找正在運行的Solr實例來檢測端口。
  • -s <shards> - 將集合拆分為的分片數(shù),默認(rèn)值為1。
  • -rf <replicas> - 集合中每個文檔的副本數(shù)。默認(rèn)值為1。

在本例中,我們將使用-c參數(shù)作為核心名稱,使用-d參數(shù)作為配置目錄。 對于所有其他參數(shù),我們使用默認(rèn)設(shè)置。

現(xiàn)在,在命令窗口中導(dǎo)航到solr-5.0.0\bin文件夾,并發(fā)出以下命令:

solr create -c jcg -d basic_configs

我們可以在命令窗口中看到以下輸出。

1<span class="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><span class="google-src-text"style="direction: ltr; text-align: left">Creating new core'jcg' using command:</span>使用命令創(chuàng)建新核心'jcg':</span>
2<span class="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><span class="google-src-text"style="direction: ltr; text-align: left">http://localhost:8983/solr/admin/cores?action=CREATE&name=jcg&instanceDir=jcg</span> http:// localhost:8983 / solr / admin / cores?action = CREATE&name = jcg&instanceDir = jcg</span>
3?
4<span class="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><span class="google-src-text"style="direction: ltr; text-align: left">{</span> {</span>
5?<span class="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><span class="google-src-text"style="direction: ltr; text-align: left">"responseHeader":{</span> “responseHeader”:{</span>
6?<span class="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><span class="google-src-text"style="direction: ltr; text-align: left">"status":0,</span> “status”:0,</span>
7?<span class="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><span class="google-src-text"style="direction: ltr; text-align: left">"QTime":663},</span> “QTime”:663},</span>
8?<span class="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><span class="google-src-text"style="direction: ltr; text-align: left">"core":"jcg"}</span> “core”:“jcg”}</span>

現(xiàn)在我們導(dǎo)航到以下URL,我們可以看到j(luò)cg core被填充在核心選擇器中。 您還可以查看核心的統(tǒng)計信息。

http://localhost:8983/solr

Solr jcg core

3.修改schema.xml文件

我們需要修改文件夾server\solr\jcg\conf下的schema.xml文件,以包含字段。我們將使用隨同Solr安裝一起提供的示例文件“books.csv”中的一個進(jìn)行索引。 該文件位于文件夾solr-5.0.0\example\exampledocs 。

現(xiàn)在我們導(dǎo)航到文件夾server\solr目錄。 您將看到一個名為jcg的文件夾。 子文件夾即conf和data分別具有核心的配置和索引數(shù)據(jù)。

現(xiàn)在編輯\server\solr\jcg\conf文件夾中的schema.xml文件,并在uniqueKey元素后面添加以下內(nèi)容。

schema.xml

1<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"><uniqueKey>id</uniqueKey></span> <uniqueKey> id </ uniqueKey></span>
2<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"><!-- Fields added for books.csv load--></span> <! - 為books.csv加載添加的字段 - ></span>
3<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"><fieldname="cat"type="text_general"indexed="true"stored="true"/></span> <fieldname =“cat”type =“text_general”indexed =“true”stored =“true”/></span>
4<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"><fieldname="name"type="text_general"indexed="true"stored="true"/></span> <fieldname =“name”type =“text_general”indexed =“true”stored =“true”/></span>
5<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"><fieldname="price"type="tdouble"indexed="true"stored="true"/></span> <fieldname =“price”type =“tdouble”indexed =“true”stored =“true”/></span>
6<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"><fieldname="inStock"type="boolean"indexed="true"stored="true"/></span> <fieldname =“inStock”type =“boolean”indexed =“true”stored =“true”/></span>
7<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"><fieldname="author"type="text_general"indexed="true"stored="true"/></span> <fieldname =“author”type =“text_general”indexed =“true”stored =“true”/></span>

我們已將indexed的屬性設(shè)置為true。 這指定了用于索引的字段,并且可以使用索引檢索記錄。 將值設(shè)置為false將使該字段只存儲,但不能查詢。

還要注意,我們有另一個屬性叫stored并設(shè)置為true。 這指定字段存儲并可以在輸出中返回。 將此字段設(shè)置為false將使該字段僅編入索引,并且無法在輸出中檢索。

我們已經(jīng)為“books.csv”文件中的字段分配了類型。 CSV文件“id”中的第一個字段由schema.xml文件的uniqueKey元素自動處理,用于建立索引。

4.配置拼寫檢查

Solr拼寫檢查組件旨在基于其他類似術(shù)語提供內(nèi)聯(lián)查詢建議。 這些建議的源可以是Solr中的字段中的術(shù)語,外部創(chuàng)建的文本文件或其他Lucene索引中的字段。 在這個例子中,我們將看到如何從Solr中索引的術(shù)語中提取建議,并將討論放在外部文件和其他索引上,以供稍后討論。

在這個例子中,我們將使用DirectSolrSpellChecker 。拼寫檢查器使用Solr索引中的術(shù)語,而不構(gòu)建并行索引。 此拼寫檢查器具有不必定期構(gòu)建的優(yōu)點,這意味著術(shù)語在索引中始終是最新的。

讓我們在文件夾\server\solr\jcg\conf以添加組件和處理程序。在以下配置中,我們使用字段name (書名)作為示例文件中使用的術(shù)語之一。其他配置在每個字段的注釋部分解釋。

solrconfig.xml

01<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"><searchComponentname="spellcheck"class="solr.SpellCheckComponent"></span> <searchComponentname =“spellcheck”class =“solr.SpellCheckComponent”></span>
02<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"><!-- a spellchecker built from a field of the main index --></span> <! - 從主索引的字段構(gòu)建的拼寫檢查 - ></span>
03<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"><lstname="spellchecker"></span> <lstname =“spellchecker”></span>
04<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"><strname="name">default</str></span> <strname =“name”> default </ str></span>
05<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"><strname="field">name</str></span> <strname =“field”> name </ str></span>
06<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"><strname="classname">solr.DirectSolrSpellChecker</str></span> <str name =“classname”> solr.DirectSolrSpellChecker </str></span>
07<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"><!-- minimum accuracy needed to be considered a valid spellcheck suggestion --></span> <! - 最低準(zhǔn)確度需要被視為有效的拼寫檢查建議 - ></span>
08<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"><floatname="accuracy">0.5</float></span> <floatname =“accuracy”> 0.5 </ float></span>
09<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"><!-- the minimum shared prefix when enumerating terms --></span> <! - 枚舉項 - >時的最小共享前綴</span>
10<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"><intname="minPrefix">1</int></span> <intname =“minPrefix”> 1 </ int></span>
11<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"><!-- minimum length of a query term to be considered for correction --></span> <! - 要考慮校正的查詢項的最小長度 - ></span>
12<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"><intname="minQueryLength">3</int></span> <intname =“minQueryLength”> 3 </ int></span>
13<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"></lst></span> </ lst></span>
14<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"></searchComponent></span> </ searchComponent></span>

現(xiàn)在讓我們配置requestHandler。 使用的參數(shù)定義如下:

  • spellcheck.dictionary - 指定應(yīng)用于拼寫檢查的字典。
  • spellcheck - 打開或關(guān)閉對請求的SpellCheck建議。如果為true,則會生成拼寫建議。
  • spellcheck.extendedResults - 導(dǎo)致Solr返回關(guān)于拼寫檢查結(jié)果的其他信息,例如索引中每個原始術(shù)語的頻率。
  • spellcheck.count - 指定要返回的拼寫建議的最大數(shù)量。
  • spellcheck.alternativeTermCount - 為索引和/或字典中存在的每個查詢項返回的建議計數(shù)。
  • spellcheck.maxResultsForSuggest - 請求可返回的最大命中數(shù),以便同時生成拼寫建議并將“correctlySpelled”元素設(shè)置為“false”。

solrconfig.xml

01<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"><requestHandlername="/spell"class="solr.SearchHandler"startup="lazy"></span> <requestHandlername =“/ spell”class =“solr.SearchHandler”startup =“l(fā)azy”></span>
02<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"><lstname="defaults"></span> <lstname =“defaults”></span>
03<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"><strname="spellcheck.dictionary">default</str></span> <str name =“spellcheck.dictionary”> default </str></span>
04<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"><strname="spellcheck">on</str></span> <strname =“spellcheck”> on </ str></span>
05<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"><strname="spellcheck.extendedResults">true</str></span> <str name =“spellcheck.extendedResults”> true </str></span>
06<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"><strname="spellcheck.count">10</str></span> <spanname =“spellcheck.count”> 10 </ str></span>
07<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"><strname="spellcheck.alternativeTermCount">5</str></span> <str name =“spellcheck.alternativeTermCount”> 5 </str></span>
08<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"><strname="spellcheck.maxResultsForSuggest">5</str></span> <str name =“spellcheck.maxResultsForSuggest”> 5 </str></span>
09<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"></lst></span> </ lst></span>
10<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"><arrname="last-components"></span> <arrname =“l(fā)ast-components”></span>
11<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"><str>spellcheck</str></span> <str> spellcheck </ str></span>
12<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"></arr></span> </ arr></span>
13<spanclass="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><spanclass="google-src-text"style="direction: ltr; text-align: left"></requestHandler></span> </ requestHandler></span>

由于我們已經(jīng)修改了配置,我們必須停止和啟動服務(wù)器。 為此,我們需要通過命令行從bin目錄發(fā)出以下命令:

solr stop -all

服務(wù)器將立即停止。 現(xiàn)在啟動服務(wù)器通過命令行從bin目錄發(fā)出以下命令:

solr start

5.索引數(shù)據(jù)

Apache Solr附帶一個名為SimplePostTool的獨立Java程序。 該程序打包到JAR中,安裝在example\exampledocs文件夾下。

現(xiàn)在,我們導(dǎo)航到命令提示符中的example\exampledocs文件夾,并鍵入以下命令。您將看到一堆使用該工具的選項。

java -jar post.jar -h

使用格式一般如下:

Usage: java [SystemProperties] -jar post.jar [-h|-] [<file|folder|url|arg>
[<file|folder|url|arg>...]]

正如我們前面所說,我們將索引solr安裝附帶的“books.csv”文件中的數(shù)據(jù)。 我們將在命令提示符中導(dǎo)航到solr-5.0.0\example\exampledocs并發(fā)出以下命令。

java -Dtype=text/csv -Durl=http://localhost:8983/solr/jcg/update -jar post.jar books.csv

這里使用的SystemProperties是:

  • -Dtype - 數(shù)據(jù)文件的類型。
  • -Durl - jcg核心的URL。

文件“books.csv”現(xiàn)在將被索引,并且命令提示符將顯示以下輸出。

1<span class="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><span class="google-src-text"style="direction: ltr; text-align: left">SimplePostTool version 5.0.0</span> SimplePostTool版本5.0.0</span>
2<span class="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><span class="google-src-text"style="direction: ltr; text-align: left">Posting files to [base] urlhttp://localhost:8983/solr/jcg/update using content-</span>將文件發(fā)布到[base] url http:// localhost:8983 / solr / jcg / update using content-</span>
3<span class="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><span class="google-src-text"style="direction: ltr; text-align: left">typetext/csv...</span>鍵入text / csv ...</span>
4<span class="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><span class="google-src-text"style="direction: ltr; text-align: left">POSTingfile books.csv to [base]</span>將文件books.csv發(fā)布到[base]</span>
5<span class="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><span class="google-src-text"style="direction: ltr; text-align: left">1 files indexed.</span> 1個文件已索引。</span>
6<span class="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><span class="google-src-text"style="direction: ltr; text-align: left">COMMITting Solr index changes tohttp://localhost:8983/solr/jcg/update...</span>提交Solr索引更改為http:// localhost:8983 / solr / jcg / update ...</span>
7<span class="notranslate"onmouseover="_tipon(this)"onmouseout="_tipoff()"><span class="google-src-text"style="direction: ltr; text-align: left">Time spent: 0:00:00.647</span>所用時間:0:00:00.647</span>

6.使用拼寫檢查查詢

現(xiàn)在,我們將啟動以下URL來驗證拼寫檢查功能。 你可以注意到,“Jhereg”書的拼寫錯誤地“Jherag”來測試我們的例子。 spellcheck.q參數(shù)用于指定要進(jìn)行拼寫檢查的查詢。我們也可以使用q參數(shù),而不是spellcheck.q,但它建議使用拼寫檢查前綴在我們做拼寫檢查的地方,以提供清晰度。

http://localhost:8983/solr/jcg/spell?spellcheck.q=Jherag&spellcheck=true

Solr拼寫檢查輸出

7.下載配置

這是Solr Spellcheck的一個例子。

下載
您可以在此下載此示例中使用的配置文件: solr_spell_config

總結(jié)

以上是生活随笔為你收集整理的Solr拼写检查示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。