solr4.0環境搭建
solr4.0環境搭建
因近期想搞個知識庫,所以選擇solr,現在最新的solr是4.0,所以用solr4.0。
服務器:tomcat6
JDK :1.6
SOLR :4.0
中文分詞器 :ik-analyzer,mmseg4j
安裝:目前mmseg4j的版本是mmseg4j-1.9.0.v20120712-SNAPSHOT,經過測試,發現這個版本有bug:
java.lang.RuntimeException: java.lang.NoSuchMethodError: org.apache.l ucene.analysis.Tokenizer.reset(Ljava/io/Reader;)V
由于solr4.0對其中的有些類與方法做了調整,所以還是等待mmseg4j新版本修復吧。果斷使用了ik-analyzer。
一、將apache-solr-4.0.0\example\webapps\solr.war放在tomcat的webapps下啟動服務器解壓該war包,另外還需要增加幾個jar包:
apache-solr-dataimporthandler-4.0.0.jar
apache-solr-dataimporthandler-extras-4.0.0.jar
這兩個jar包可以在solr的dist中可以找到
另外還需要相應數據庫的驅動包,比如
mysql-connector-java-5.1.13-bin.jar
二、將apache-solr-4.0.0\example下的solr拷貝至apache-tomcat-6.0.29-solr\bin下
三、在apache-tomcat-6.0.29-solr\bin\solr\collection1\conf下的solrconfig.xml增加以下數據庫配置
[html] view plaincopy
- <requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
- <lst name="defaults">
- <str name="config">data-config.xml</str>
- </lst>
- </requestHandler>
四、將apache-tomcat-6.0.29-solr\bin\solr\collection1\conf下增加data-config.xml文件,內容如下:
[html] view plaincopy
- <dataConfig>
- <dataSource type="JdbcDataSource"
- driver="com.mysql.jdbc.Driver"
- url="jdbc:mysql://localhost:3306/solrdb"
- user="root"
- password="888888"/>
- <document name="content">
- <entity name="node" query="select id,author,title,content from solrdb">
- <field column="id" name="id" />
- <field column="author" name="author" />
- <field column="title" name="title" />
- <field column="content" name="content" />
- </entity>
- </document>
- </dataConfig>
五、增加中文分詞器,ik-analyzer的配置如下:
它的安裝部署十分簡單,將IKAnalyzer2012.jar部署亍項目的lib目錄中;IKAnalyzer.cfg.xml不stopword.dic文件放置在class根目錄(對于web項目,通常是WEB-I NF/classes目彔,同hibernate、log4j等配置文件相同)下即可
solr4.0中schema.xml配置解析器:
六、schema.xml完整配置:[html] view plaincopy
- <schema name="example" version="1.1">
- ……
- <fieldType name="text" class="solr.TextField">
- <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
- </fieldType>
- ……
- </schema>
[html] view plaincopy
- <?xml version="1.0" encoding="UTF-8" ?>
- <schema name="example" version="1.5">
- <types>
- <fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/>
- <fieldType name="string" class="solr.StrField" sortMissingLast="true" />
- <!-- IKAnalyzer 配置 -->
- <fieldType name="text" class="solr.TextField">
- <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
- </fieldType>
- </types>
- <fields>
- <field name="id" type="string" indexed="true" stored="true" required="true" />
- <field name="author" type="text" indexed="true" stored="true" multiValued="false"/>
- <field name="title" type="text" indexed="true" stored="true" multiValued="false"/>
- <field name="content" type="text" indexed="true" stored="true" multiValued="false" />
- <field name="_version_" type="long" indexed="true" stored="true"/>
- </fields>
- <uniqueKey>id</uniqueKey>
- <defaultSearchField>content</defaultSearchField>
- <solrQueryParser defaultOperator="OR"/>
- <copyField source="title" dest="content"/>
- <copyField source="author" dest="content"/>
- </schema>
解析:multiValued的個人理解是配置true則返回單條數據,false則可以返回多條,以后深入理解了再詳解。defaultSearchField配置默認搜索索引,copyField可以講 title、author字段添加至content默認搜索中
七、登錄管理頁面:
中文分詞器分詞的示例:

query示例:

posted on 2013-01-22 14:35 順其自然EVO 閱讀(519) 評論(0) 編輯 收藏 所屬分類: 測試學習專欄