搭建网上商城基础搜索服务

现在正在开发网上商城项目,由于用户要求搜索速度高,采用solr进行建立基础搜索服务,实现关键词,商品属性,商品类型等多重条件搜索。

1.下载solr3.6.1,IKAnalyzer2012_u5,ojdbc6_g.jar

2. 把apache-solr-3.6.2文件夹中apache-solr-3.6.2\apache-solr-3.6.2\example\webapps\solr.war放到tomcat下面的webapp文件中

3. 把apache-solr-3.6.2\apache-solr-3.6.2\example\solr文件夹是存放索引数据的主目录solr_home,可以放到其他目录,新建配置文件Tomcat\conf\Catalina\localhost\solr.xml

添加内容: 

<Context docBase="D:\zwTomcat\webapps\solr.war" debug="0" crossContext="true">

<Environment name="solr/home" type="java.lang.String" value="D:\solr" override="true"/>

</Context>

4. 将oracle jdbc驱动放到tomcat_home/webapps/solr/WEB_INF/lib中

5. 启动tomcat,打开连接http://localhost:8090/solr,如果可以打开连接说明配置成功。修改tomcat

的server.xml,增加字符集为utf-8

6. 配置IK分词器,打开solr_home\conf\schema.xml,<types></types>直接添加中文分词器,

把IKAnalyzer2012.jar拷贝到tomcat\webapps\solr\WEB-INF\lib

<fieldType name="text_ika" class="solr.TextField"> 

<analyzer type="index"> 

<tokenizer class="org.wltea.analyzer.solr.IKTokenizerFactory"  isMaxWordLength="false"/> 

<filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" /> 

<filter class="solr.LowerCaseFilterFactory"/> 

</analyzer> 

<analyzer type="query"> 

<tokenizer class="org.wltea.analyzer.solr.IKTokenizerFactory" isMaxWordLength="true"/> 

<filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" /> 

<filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/> 

<filter class="solr.LowerCaseFilterFactory"/> 

</analyzer> 

</fieldType>

7. 修改solrconfig.xml文件

添加

<requestHandler name="/dataimport"  

     class="org.apache.solr.handler.dataimport.DataImportHandler"> 

    <lst name="defaults"> 

      <str name="config">db-data-config.xml</str> 

    </lst> 

</requestHandler>

修改jar路径

<lib dir="D:/solr/dist/" regex="apache-solr-dataimporthandler-\d.*\.jar" />

8.

建立db-data-config.xml文件

添加导入字段和sql语句,在配置数据库列名和搜索字段名称全部是大写否则有些功能没有实现

<dataConfig> 

  <dataSource type="JdbcDataSource" 

   driver="com.mysql.jdbc.Driver" 

   url="jdbc:mysql://localhost:3306/mytest" 

   user="root" 

   password="root" 

   /> 

 <document name="documents1" > 

        <entity name="documents" 

          query="select id,title,content,date_added from documents" 

          deltaImportQuery="select  id,title,content,date_added  from documents where ID='${dataimporter.delta.id}'" 

          deltaQuery="select id  from documents where date_added > '${dataimporter.last_index_time}'" 

          deletedPkQuery="select id  from documents where id=0"> 

            <field column="ID" name="ID" /> 

            <field column="TITLE" name="TITLE" /> 

            <field column="CONTENT" name="CONTENT" /> 

            <field column="DATE_ADDED" name="DATE_ADDED" /> 

        </entity> 

  </document> 

</dataConfig>

9.

在schema.xml添加需要添加索引字段和配置搜索属性

 

 <fields>

            <field name="id" type="string" indexed="true" stored="true" required="true" /> 

<field name="title" type="text_ika" indexed="true" stored="true" termVectors="true" termPositions="true" termOffsets="true"/> 

<field name="content" type="text_ika" indexed="true" stored="true" termVectors="true" termPositions="true" termOffsets="true"/> 

<field name="date_added" type="date" indexed="false" stored="true"/> 

 </fields>

 <uniqueKey>id</uniqueKey><!--唯一性字段-->

猜你喜欢

转载自zhb01.iteye.com/blog/1866639