基於Solr 3.5搭建搜尋伺服器

來源:互聯網
上載者:User

Solr已經發布3.5版本了,同時它是基於Lucene 3.5的。我們在基於Solr進行二次開發之前,首先要搭建起一個搜尋伺服器,在熟悉Solr的準系統的基礎上,可以根據實際應用的需要進行個人化定製開發。因為Solr提供了一種外掛程式機制,我們可以根據自己的需要進行定製,然後在Solr的設定檔中(solrconfig.xml)進行配置即可達到預期的要求。在Solr的發行包中給出了一個配置的例子,我們可以直接將其發布到Web容器中,通過瀏覽器訪問來進行測試,具體如何配置,下面根據從易到難的方式,對每種方式進行詳細的介紹。

準備工作

  1. 下載Solr 3.5發行包(http://lucene.apache.org/solr,apache-solr-3.5.0-src.tgz和apache-solr-3.5.0.tgz),並解壓縮到檔案系統中;
  2. 下載Lucene 3.5發行包(http://lucene.apache.org/,lucene-3.5.0-src.tgz和lucene-3.5.0.tgz),並解壓縮到檔案系統中;
  3. 安裝配置Web容器,我使用的apache-tomcat-6.0.32。

第一種方式:基於WAR包搭建

這種方式,我們是直接使用Solr發行包給定的WAR包,一般來說通過它快速瞭解Solr是很有用的,而對於滿足實際需要的開發還遠遠不夠。

按照下面的步驟,進行安裝、配置、驗證:

第1步:將apache-solr-3.5.0\apache-solr-3.5.0\example下面的multicore拷貝到apache-tomcat-6.0.32\conf下面;

multicore目錄下麵包含了Solr的基本配置。Solr支援配置多個執行個體,亦即,可以啟動多個執行個體來服務於前端不同的搜尋請求,每個執行個體對應一個core,而這樣多個core的配置是通過multicore\solr.xml進行配置的,然後在multicore下面的每個目錄中對應著每個core的詳細配置,具體包括schema.xml(配置與Lucene的Field、Analyzer等相關的內容)、solrconfig.xml(這個是Solr執行個體核心的配置)。

另外,如果在solrconfig.xml中沒有指定<dataDir>索引目錄配置,則預設會產生apache-tomcat-6.0.32\conf\multicore\data\index目錄,該目錄下面儲存索引檔案。

第2步:將apache-solr-3.5.0\apache-solr-3.5.0\dist下面的apache-solr-3.5.0.war拷貝到apache-tomcat-6.0.32\webapps目錄下面;

這個不用過多解釋,就是通過使用一個Web歸檔檔案(WAR)來部署一個Web應用,我們的應用就是Solr搜尋應用程式。

第3步:配置WAR程式的Context:在apache-tomcat-6.0.32\conf\Catalina\localhost下面(如果目錄不存在,則手動建立),建立檔案apache-solr-3.5.0.xml;

Context設定檔apache-solr-3.5.0.xml的內容如下所示:

<Context docBase="${catalina.home}/webapps/apache-solr-3.5.0.war" debug="0" crossContext="true" >   <Environment name="solr/home" type="java.lang.String" value="${catalina.home}/conf/multicore" override="true" /></Context>

docBase指定了我們的WAR檔案的位置,上面的“solr/home”非常關鍵,在Web容器啟動以後會載入Solr的基本配置並初始化相應的組件執行個體,它會根據指定的“solr/home”配置的路徑去搜尋相關的配置,例如,上面我們將“solr/home”指向了目錄apache-tomcat-6.0.32\conf\multicore。

第4步:設定Solr的字元集;

預設Solr使用了UTF-8字元集編碼,如果你的Tomcat不是的話,在執行中文搜尋的時候可能會出現亂碼。如果你的Tomcat預設8080連接埠請求字元集就是UTF-8,並且想使用這個預設的連接埠提供搜尋服務,則可以修改apache-tomcat-6.0.32\conf\server.xml檔案的內容,如下所示:

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" URIEncoding="UTF-8" redirectPort="8443" />

上面我們增加了一個URIEncoding="UTF-8"的配置。

如果想使用一個新的未被佔用的連接埠,則可以在apache-tomcat-6.0.32\conf\server.xml中增加一個配置,例如使用8888連接埠,配置內容如下所示:

<Connector port="8888" protocol="HTTP/1.1" connectionTimeout="20000"  URIEncoding="UTF-8" redirectPort="8443" />

第5步:驗證

通過上面的步驟,現在可以啟動Tomcat伺服器,在瀏覽器地址欄這種輸入http://localhost:8080/apache-solr-3.5.0/,你會看到如下內容:

Welcome to Solr!Admin core0 Admin core1 

則說明已經安裝成功了。你可以根據連結,點擊進去,瀏覽一下Solr提供的管理介面及其相關的管理功能。

第二種方式:基於jar包搭建

這種方式,我們不再使用Solr預設提供,並對我們非常透明的WAR包來搭建,而是根據Solr發行包中的相關內容來搭建,更確切地說,我們把Solr在一個開發工具上搭建起來,暫且不考慮源碼層面的內容。我比較習慣使用MyEclipse,我使用了MyEclipse Enterprise Workbench 8.0整合式開發環境。

遵循下面的步驟,就可以實現:

  1. 建立一個Web Project,工程名稱為solr35;
  2. 將apache-solr-3.5.0-src\apache-solr-3.5.0\solr\example\multicore目錄,拷貝到工程solr35下面;
  3. 將apache-solr-3.5.0\apache-solr-3.5.0\dist以及solrj-lib目錄中jar檔案,拷貝到工程solr35\WebRoot\WEB-INF\lib下面;
  4. 將apache-solr-3.5.0-src\apache-solr-3.5.0\solr\lib目錄中jar檔案,拷貝到工程solr35\WebRoot\WEB-INF\lib下面;
  5. 將lucene-3.5.0\lucene-3.5.0\lucene-core-3.5.0.jar、lucene-3.5.0\lucene-3.5.0\contrib\spatial\lucene-spatial-3.5.0.jar、lucene-3.5.0\lucene-3.5.0\contrib\highlighter\lucene-highlighter-3.5.0.jar這三個檔案,拷貝到solr35\WebRoot\WEB-INF\lib下面;
  6. 將apache-solr-3.5.0-src\apache-solr-3.5.0\solr\webapp\web目錄,拷貝到solr35\WebRoot,並覆蓋原來的全部內容;
  7. 修改solr35\WebRoot\WEB-INF\web.xml檔案,增加如下內容:

    <env-entry>       <env-entry-name>solr/home</env-entry-name>       <env-entry-value>E:\Develop\myeclipse\workspace\solr35\multicore</env-entry-value>       <env-entry-type>java.lang.String</env-entry-type>    </env-entry>

實際上,就是指定了Web容器啟動後,Solr載入執行個體的相關配置和索引資料的目錄。

另外,這樣是直接在web.xml中進行了寫入程式碼配置,如果solr/home變化了,每次都需要修改web.xml檔案。還有一種方式是,直接增加Web容器的啟動選項來指定,如下所示:

-Dsolr.solr.home=E:\Develop\myeclipse\workspace\solr35\multicore

這樣,配置就更加靈活了,非常方便。

通過上面的配置,可以啟動Tomcat伺服器了,並通過訪問http://localhost:8080/solr35來進行驗證。

第三種方式:基於源碼搭建

基於源碼搭建的好處的就是,我們在開發過程中可以方便地進行調試跟蹤,這樣也能夠便於更深入地瞭解Solr架構的執行機制。Solr是基於Lucene這個開源搜尋引擎庫開發的架構,通過瞭解Solr的原始碼,你可以更深入地熟悉如何在Lucene之上構建適合自己的搜尋應用,甚至你完全可以將Solr改造成自己需要的應用程式。一般來說,我們使用Solr搭建搜尋伺服器的適合,完全可以不需要熟悉Lucene是怎麼樣實現索引和全文檢索索引的,但是在Solr上進行開發調試,如調試搜尋的相關度時,就需要對Lucene有一定的瞭解,才能在調優的過程中事半功倍。

基於源碼的搭建,我採用了一種Lucene和Solr的原始碼都可以進行修改,即將Lucene和Solr的代碼匯入的開發環境中。具體如何匯入,因為代碼都是開源的,你可以使用任何方法實現,不再累述。這裡,我們簡單說一下,我將solr和Lucene分別匯入到了兩個工程中:Lucene Java Project、Solr Web Project。我把工程的.classpath檔案粘貼一下,以供參考:

Lucene Java Project的.classpath檔案內容如下:

<?xml version="1.0" encoding="UTF-8"?><classpath><classpathentry kind="src" path="src/lucene/src/java"/><classpathentry kind="src" path="src/lucene/contrib/analyzers/common/src/java"/><classpathentry kind="src" path="src/lucene/contrib/analyzers/common/src/test"/><classpathentry kind="src" path="src/lucene/contrib/analyzers/smartcn/src/java"/><classpathentry kind="src" path="src/lucene/contrib/analyzers/smartcn/src/test"/><classpathentry kind="src" path="src/lucene/contrib/analyzers/stempel/src/java"/><classpathentry kind="src" path="src/lucene/contrib/analyzers/stempel/src/test"/><classpathentry kind="src" path="src/lucene/contrib/benchmark/src/java"/><classpathentry kind="src" path="src/lucene/contrib/benchmark/src/test"/><classpathentry kind="src" path="src/lucene/contrib/demo/src/java"/><classpathentry kind="src" path="src/lucene/contrib/demo/src/test"/><classpathentry kind="src" path="src/lucene/contrib/facet/src/java"/><classpathentry kind="src" path="src/lucene/contrib/facet/src/test"/><classpathentry kind="src" path="src/lucene/contrib/facet/src/examples"/><classpathentry kind="src" path="src/lucene/contrib/grouping/src/java"/><classpathentry kind="src" path="src/lucene/contrib/grouping/src/test"/><classpathentry kind="src" path="src/lucene/contrib/highlighter/src/java"/><classpathentry kind="src" path="src/lucene/contrib/highlighter/src/test"/><classpathentry kind="src" path="src/lucene/contrib/icu/src/java"/><classpathentry kind="src" path="src/lucene/contrib/icu/src/tools/java"/><classpathentry kind="src" path="src/lucene/contrib/icu/src/test"/><classpathentry kind="src" path="src/lucene/contrib/instantiated/src/java"/><classpathentry kind="src" path="src/lucene/contrib/instantiated/src/test"/><classpathentry kind="src" path="src/lucene/contrib/join/src/java"/><classpathentry kind="src" path="src/lucene/contrib/join/src/test"/><classpathentry kind="src" path="src/lucene/contrib/memory/src/java"/><classpathentry kind="src" path="src/lucene/contrib/memory/src/test"/><classpathentry kind="src" path="src/lucene/contrib/misc/src/java"/><classpathentry kind="src" path="src/lucene/contrib/misc/src/test"/><classpathentry kind="src" path="src/lucene/contrib/queries/src/java"/><classpathentry kind="src" path="src/lucene/contrib/queries/src/test"/><classpathentry kind="src" path="src/lucene/contrib/queryparser/src/java"/><classpathentry kind="src" path="src/lucene/contrib/queryparser/src/test"/><classpathentry kind="src" path="src/lucene/contrib/remote/src/java"/><classpathentry kind="src" path="src/lucene/contrib/remote/src/test"/><classpathentry kind="src" path="src/lucene/contrib/spatial/src/java"/><classpathentry kind="src" path="src/lucene/contrib/spatial/src/test"/><classpathentry kind="src" path="src/lucene/contrib/spellchecker/src/java"/><classpathentry kind="src" path="src/lucene/contrib/spellchecker/src/test"/><classpathentry kind="src" path="src/lucene/contrib/xml-query-parser/src/java"/><classpathentry kind="src" path="src/lucene/contrib/xml-query-parser/src/test"/><classpathentry kind="src" path="src/lucene/contrib/xml-query-parser/src/demo/java"/><classpathentry kind="src" path="src/lucene/test-framework/src/java"/><classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/><classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/Contributions Dependences"/><classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/Lucene Contrib Dependences"/><classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/JUnit 4.7"/><classpathentry kind="output" path="bin"/></classpath>

Solr Web Project的.classpath檔案內容如下:

<?xml version="1.0" encoding="UTF-8"?><classpath><classpathentry kind="src" path="src/solr/solrj/src/java"/><classpathentry kind="src" path="src/solr/solrj/src/test"/><classpathentry kind="src" path="src/solr/core/src/java"/><classpathentry kind="src" path="src/solr/core/src/test"/><classpathentry kind="src" path="src/solr/contrib/analysis-extras/src/java"/><classpathentry kind="src" path="src/solr/contrib/analysis-extras/src/test"/><classpathentry kind="src" path="src/solr/contrib/clustering/src/java"/><classpathentry kind="src" path="src/solr/contrib/clustering/src/test"/><classpathentry kind="src" path="src/solr/contrib/dataimporthandler/src/java"/><classpathentry kind="src" path="src/solr/contrib/dataimporthandler/src/test"/><classpathentry kind="src" path="src/solr/contrib/dataimporthandler-extras/src/java"/><classpathentry kind="src" path="src/solr/contrib/dataimporthandler-extras/src/test"/><classpathentry kind="src" path="src/solr/contrib/extraction/src/java"/><classpathentry kind="src" path="src/solr/contrib/extraction/src/test"/><classpathentry kind="src" path="src/solr/contrib/langid/src/java"/><classpathentry kind="src" path="src/solr/contrib/langid/src/test"/><classpathentry kind="src" path="src/solr/contrib/uima/src/java"/><classpathentry kind="src" path="src/solr/contrib/uima/src/test"/><classpathentry kind="src" path="src/solr/contrib/velocity/src/java"/><classpathentry kind="src" path="src/solr/contrib/velocity/src/test"/><classpathentry kind="src" path="src/solr/test-framework/src/java"/><classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/><classpathentry kind="con" path="melibrary.com.genuitec.eclipse.j2eedt.core.MYECLIPSE_JAVAEE_5_CONTAINER"/><classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/Solr Contrib Dependences"/><classpathentry kind="lib" path="WebRoot/WEB-INF/lib/apache-solr-noggit-r1099557.jar"/><classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-codec-1.5.jar"/><classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-csv-1.0-SNAPSHOT-r966014.jar"/><classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-fileupload-1.2.1.jar"/><classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-httpclient-3.1.jar"/><classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-io-1.4.jar"/><classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-lang-2.4.jar"/><classpathentry kind="lib" path="WebRoot/WEB-INF/lib/easymock-2.2.jar"/><classpathentry kind="lib" path="WebRoot/WEB-INF/lib/geronimo-stax-api_1.0_spec-1.0.1.jar"/><classpathentry kind="lib" path="WebRoot/WEB-INF/lib/guava-r05.jar"/><classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jcl-over-slf4j-1.6.1.jar"/><classpathentry kind="lib" path="WebRoot/WEB-INF/lib/junit-4.7.jar"/><classpathentry kind="lib" path="WebRoot/WEB-INF/lib/log4j-over-slf4j-1.6.1.jar"/><classpathentry kind="lib" path="WebRoot/WEB-INF/lib/servlet-api-2.4.jar"/><classpathentry kind="lib" path="WebRoot/WEB-INF/lib/slf4j-api-1.6.1.jar"/><classpathentry kind="lib" path="WebRoot/WEB-INF/lib/slf4j-jdk14-1.6.1.jar"/><classpathentry kind="lib" path="WebRoot/WEB-INF/lib/wstx-asl-3.2.7.jar"/><classpathentry combineaccessrules="false" kind="src" path="/lucene35"/><classpathentry kind="lib" path="WebRoot/WEB-INF/lib/core-3.1.1.jar"/><classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jetty-6.1.26-patched-JETTY-1340.jar"/><classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jetty-util-6.1.26-patched-JETTY-1340.jar"/><classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jsp-2.1-glassfish-2.1.v20091210.jar"/><classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jsp-2.1-jetty-6.1.26.jar"/><classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jsp-api-2.1-glassfish-2.1.v20091210.jar"/><classpathentry kind="lib" path="WebRoot/WEB-INF/lib/servlet-api-2.5-20081211.jar"/><classpathentry kind="output" path="WebRoot/WEB-INF/classes"/></classpath>

搭建起來開發環境,你可以更加深入的學習Solr了。

Solr相關連結

這裡,分享一些有關學習開發Solr的參考連結,可能在實際學習和開發中,會起到一些協助。

01. Solrhttp://lucene.apache.org/solr/http://wiki.apache.org/solr/SolrResourceshttp://lucene.apache.org/solr/features.htmlhttp://lucene.apache.org/solr/tutorial.htmlhttp://wiki.apache.org/solrhttps://issues.apache.org/jira/browse/SOLRhttp://wiki.apache.org/solr/FAQhttp://wiki.apache.org/solr/SolrRelevancyFAQhttp://wiki.apache.org/solr/SolrRelevancyCookbookhttp://khaidoan.wikidot.com/solrhttp://yonik.wordpress.com/ 02. Solr mailing listhttp://mail-archives.apache.org/mod_mbox/lucene-solr-user/http://lucene.472066.n3.nabble.com/Solr-f472067.html 03. Solr Insntallationhttp://wiki.apache.org/solr/SolrTomcathttp://wiki.apache.org/solr/SolrJettyhttp://wiki.apache.org/solr/SolrResinhttp://wiki.apache.org/solr/SolrJBosshttp://wiki.apache.org/solr/SolrWebSpherehttp://wiki.apache.org/solr/SolrWeblogichttp://wiki.apache.org/solr/SolrGlassfishhttp://redmine.synyx.org/projects/opencms-solr-module/wiki/Integrating_Solr_into_an_existing_application 04. Solr Basishttp://wiki.apache.org/solr/SolrTerminologyhttp://wiki.apache.org/solr/SchemaXmlhttp://svn.apache.org/viewvc/lucene/dev/trunk/solr/example/solr/conf/schema.xml?view=markuphttp://wiki.apache.org/solr/SolrConfigXmlhttp://svn.apache.org/repos/asf/lucene/dev/trunk/solr/example/solr/conf/solrconfig.xmlhttp://wiki.apache.org/solr/UpdateXmlMessageshttp://wiki.apache.org/solr/CommonQueryParametershttp://wiki.apache.org/solr/LocalParamshttp://wiki.apache.org/solr/SolrQuerySyntaxhttp://wiki.apache.org/solr/AnalyzersTokenizersTokenFiltershttp://wiki.apache.org/solr/SolrInstallhttp://wiki.apache.org/solr/mySolr 05. Solr Functionshttp://wiki.apache.org/solr/FunctionQueryhttp://www.lucidimagination.com/blog/2009/11/20/fun-with-solr-functions/http://www.supermind.org/blog/756/how-to-write-a-custom-solr-functionqueryhttp://search.lucidimagination.com/search/out?u=http%3A%2F%2Flucene.472066.n3.nabble.com%2Fhow-do-I-create-custom-function-that-uses-multiple-ValuesSources-tp1402645p1402645.html 06. Solr Payloadhttp://wiki.apache.org/solr/Payloadshttp://lucene.472066.n3.nabble.com/How-to-use-Payloads-with-Solr-td679691.htmlhttp://stephenpope.co.uk/?p=32http://lucene.472066.n3.nabble.com/synonym-payload-boosting-td563432.html 07. Solr Dismaxhttp://wiki.apache.org/solr/DisMaxhttp://www.lucidimagination.com/blog/2010/05/23/whats-a-dismax/http://wiki.apache.org/solr/DisMaxRequestHandlerhttp://wiki.apache.org/solr/DisMaxQParserPluginhttp://lucene.apache.org/solr/api/org/apache/solr/util/doc-files/min-should-match.htmlhttp://lucene.472066.n3.nabble.com/Dismax-Boosting-td1906083.htmlhttp://lucene.472066.n3.nabble.com/configure-dismax-requesthandlar-for-boost-a-field-td3137239.htmlhttp://code.google.com/p/solr-boostmax/http://www.lucidimagination.com/blog/2009/03/31/nested-queries-in-solr/ 08. Solr Performancehttp://wiki.apache.org/solr/SolrPerformanceDatahttp://wiki.apache.org/solr/SolrPerformanceFactorshttp://wiki.apache.org/solr/BenchmarkingSolrhttp://code.google.com/p/solrmeter/ 09. Solr Cachehttp://wiki.apache.org/solr/SolrCachinghttp://wiki.apache.org/solr/SolrConfigXml#HTTP_Cachinghttp://wiki.apache.org/solr/SolrAndHTTPCacheshttp://java.dzone.com/news/optimization-%E2%80%93-filter-cache?mz=33057-solr_lucene 10. Solr Faceted Searchhttp://wiki.apache.org/solr/SolrFacetingOverviewhttp://wiki.apache.org/solr/SimpleFacetParametershttp://www.lucidimagination.com/Community/Hear-from-the-Experts/Articles/Faceted-Search-Solrhttp://www.lucidimagination.com/devzone/technical-articles/faceted-search-solrhttp://stackoverflow.com/questions/3068810/solr-multiple-facet-dates 11. Solr Distributedhttp://wiki.apache.org/solr/DistributedSearchhttp://wiki.apache.org/solr/CollectionDistributionhttp://wiki.apache.org/solr/SolrReplicationhttp://wiki.apache.org/solr/WritingDistributedSearchComponentshttp://wiki.apache.org/solr/SolrCloudhttp://wiki.apache.org/solr/SolrCollectionDistributionScriptshttp://wiki.apache.org/solr/SolrCollectionDistributionStatusStatshttp://wiki.apache.org/solr/SolrCollectionDistributionOperationsOutline 12. Solr Clusteringhttp://wiki.apache.org/solr/ClusteringComponenthttp://www.lucidimagination.com/blog/2009/09/28/solrs-new-clustering-capabilities/ 13. Solr Near Realtime Searchhttp://wiki.apache.org/solr/NearRealtimeSearchhttp://www.lucidimagination.com/blog/2011/07/11/benchmarking-the-new-solr-%E2%80%98near-realtime%E2%80%99-improvements/ 14. Solr Spellcheckerhttp://wiki.apache.org/solr/SpellCheckComponenthttp://wiki.apache.org/solr/SpellCheckingAnalysis

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.