基於Solr 3.5搭建搜索服務器

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. Solr

http://lucene.apache.org/solr/
http://wiki.apache.org/solr/SolrResources
http://lucene.apache.org/solr/features.html
http://lucene.apache.org/solr/tutorial.html
http://wiki.apache.org/solr
https://issues.apache.org/jira/browse/SOLR
http://wiki.apache.org/solr/FAQ
http://wiki.apache.org/solr/SolrRelevancyFAQ
http://wiki.apache.org/solr/SolrRelevancyCookbook
http://khaidoan.wikidot.com/solr
http://yonik.wordpress.com/

 02. Solr mailing list

http://mail-archives.apache.org/mod_mbox/lucene-solr-user/
http://lucene.472066.n3.nabble.com/Solr-f472067.html

 03. Solr Insntallation

http://wiki.apache.org/solr/SolrTomcat
http://wiki.apache.org/solr/SolrJetty
http://wiki.apache.org/solr/SolrResin
http://wiki.apache.org/solr/SolrJBoss
http://wiki.apache.org/solr/SolrWebSphere
http://wiki.apache.org/solr/SolrWeblogic
http://wiki.apache.org/solr/SolrGlassfish
http://redmine.synyx.org/projects/opencms-solr-module/wiki/Integrating_Solr_into_an_existing_application

 04. Solr Basis

http://wiki.apache.org/solr/SolrTerminology
http://wiki.apache.org/solr/SchemaXml
http://svn.apache.org/viewvc/lucene/dev/trunk/solr/example/solr/conf/schema.xml?view=markup
http://wiki.apache.org/solr/SolrConfigXml
http://svn.apache.org/repos/asf/lucene/dev/trunk/solr/example/solr/conf/solrconfig.xml
http://wiki.apache.org/solr/UpdateXmlMessages
http://wiki.apache.org/solr/CommonQueryParameters
http://wiki.apache.org/solr/LocalParams
http://wiki.apache.org/solr/SolrQuerySyntax
http://wiki.apache.org/solr/AnalyzersTokenizersTokenFilters
http://wiki.apache.org/solr/SolrInstall
http://wiki.apache.org/solr/mySolr

 05. Solr Functions

http://wiki.apache.org/solr/FunctionQuery
http://www.lucidimagination.com/blog/2009/11/20/fun-with-solr-functions/
http://www.supermind.org/blog/756/how-to-write-a-custom-solr-functionquery
http://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 Payload

http://wiki.apache.org/solr/Payloads
http://lucene.472066.n3.nabble.com/How-to-use-Payloads-with-Solr-td679691.html
http://stephenpope.co.uk/?p=32
http://lucene.472066.n3.nabble.com/synonym-payload-boosting-td563432.html

 07. Solr Dismax

http://wiki.apache.org/solr/DisMax
http://www.lucidimagination.com/blog/2010/05/23/whats-a-dismax/
http://wiki.apache.org/solr/DisMaxRequestHandler
http://wiki.apache.org/solr/DisMaxQParserPlugin
http://lucene.apache.org/solr/api/org/apache/solr/util/doc-files/min-should-match.html
http://lucene.472066.n3.nabble.com/Dismax-Boosting-td1906083.html
http://lucene.472066.n3.nabble.com/configure-dismax-requesthandlar-for-boost-a-field-td3137239.html
http://code.google.com/p/solr-boostmax/
http://www.lucidimagination.com/blog/2009/03/31/nested-queries-in-solr/

 08. Solr Performance

http://wiki.apache.org/solr/SolrPerformanceData
http://wiki.apache.org/solr/SolrPerformanceFactors
http://wiki.apache.org/solr/BenchmarkingSolr
http://code.google.com/p/solrmeter/

 09. Solr Cache

http://wiki.apache.org/solr/SolrCaching
http://wiki.apache.org/solr/SolrConfigXml#HTTP_Caching
http://wiki.apache.org/solr/SolrAndHTTPCaches
http://java.dzone.com/news/optimization-%E2%80%93-filter-cache?mz=33057-solr_lucene

 10. Solr Faceted Search

http://wiki.apache.org/solr/SolrFacetingOverview
http://wiki.apache.org/solr/SimpleFacetParameters
http://www.lucidimagination.com/Community/Hear-from-the-Experts/Articles/Faceted-Search-Solr
http://www.lucidimagination.com/devzone/technical-articles/faceted-search-solr
http://stackoverflow.com/questions/3068810/solr-multiple-facet-dates

 11. Solr Distributed

http://wiki.apache.org/solr/DistributedSearch
http://wiki.apache.org/solr/CollectionDistribution
http://wiki.apache.org/solr/SolrReplication
http://wiki.apache.org/solr/WritingDistributedSearchComponents
http://wiki.apache.org/solr/SolrCloud
http://wiki.apache.org/solr/SolrCollectionDistributionScripts
http://wiki.apache.org/solr/SolrCollectionDistributionStatusStats
http://wiki.apache.org/solr/SolrCollectionDistributionOperationsOutline

 12. Solr Clustering

http://wiki.apache.org/solr/ClusteringComponent
http://www.lucidimagination.com/blog/2009/09/28/solrs-new-clustering-capabilities/

 13. Solr Near Realtime Search

http://wiki.apache.org/solr/NearRealtimeSearch
http://www.lucidimagination.com/blog/2011/07/11/benchmarking-the-new-solr-%E2%80%98near-realtime%E2%80%99-improvements/

 14. Solr Spellchecker

http://wiki.apache.org/solr/SpellCheckComponent
http://wiki.apache.org/solr/SpellCheckingAnalysis


發佈了73 篇原創文章 · 獲贊 19 · 訪問量 110萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章