solr搜索引擎的使用(2)--分词器

 

3、分词器的配置过程

    I)在solr_home目录下新建目录dic,将下载的好的词库(本处使用的是sogou的词库)解压,拷贝其中的SogouLabDic.dic到dic目录下。

   II)修改schema.xml(solr-home \collection1\conf)文件,使分词器起到作用。在schema.xml的<types>、<fields>新增以下配置:

      在<types></types>中添加以下部分(dicPath为刚才的词库目录)

 <!--sogou-->

            <fieldType name="text_complex" class="solr.TextField" positionIncrementGap="100">

                     <analyzer>

                            <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="complex" dicPath="D:\solr\solr-tomcat\dic">

                            </tokenizer>

                     </analyzer>

              </fieldType>

              <fieldType name="text_maxWord" class="solr.TextField" positionIncrementGap="100">

                     <analyzer>

                            <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="max-word" dicPath="D:\solr\solr-tomcat\dic">

                            </tokenizer>

                     </analyzer>

              </fieldType>

              <fieldType name="text_simple" class="solr.TextField" positionIncrementGap="100">

                     <analyzer>

                            <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="simple" dicPath="D:\solr\solr-tomcat\dic">

                            </tokenizer>

                     </analyzer>

              </fieldType>

    III)将分词器mmseg4j-1.9.1解压,拷贝mmseg4j-1.9.1\dist下的jar包到solr.war的lib目录下。

    IV)重新启动tomcat进入solr主页,出现以下界面则刚才配置的分词器生效(Analyse Fieldname / FieldType:text_maxWord为刚才配置的types)

 

4、 创建索引

      I)在<fields></fields>中添加如下内容(field和type是配套使用,field name是索引的列名) 其中的copyField放在<fields></fields>的外面,此处就是根据bInfo字段可以匹配bookName、bookDesc以及bookAuth这3列的数据。

<field name="bookName" type="text_maxWord" indexed="true" stored="true"/>
<field name="bookDesc" type="text_maxWord" indexed="true" stored="true"/>
<field name="bookAuth" type="text_maxWord" indexed="true" stored="true"/>
<field name="bInfo" type="text_maxWord" indexed="true" multiValued="true"/>
<copyField source="bookName" dest="bInfo"/>
<copyField source="bookDesc" dest="bInfo"/>
<copyField source="bookAuth" dest="bInfo"/>


 

      II)添加数据

           方式一:通过solr后台手工录入

                      进入solr管理后台,进入collection1,选择Documents,选择Document Type(此处选择xml作为实例),在Documents出添加如下xml数据:

<doc>
<field name="id">id</field>
<field name="bookName">think in java</field>
<field name="bookAuth">john</field>
<field name="bookDesc">一本很不错的介绍java的书</field>
</doc>

                     单击“Submit Document”,提交数据。然后进入Query可以查询到刚才添加的数据,截图如下:

              方式二:从数据库中获取

                             A)数据库创建数据:新建表book_info,并插入几条数据做为测试,语句如下:

create table BOOK_INFO
(
  BOOK_ID   INTEGER,
  BOOK_NAME VARCHAR2(150),
  BOOK_AUTH VARCHAR2(30),
  BOOK_DESC CLOB
);

                           B)提供对应的jdbc驱动:将测试数据使用的驱动jar包拷贝到solr.war的lib目录下。

                           C)在solr-home\collection1\conf目录下新建目录db,在db文件夹中新建db-data-config.xml内容如下(column为表的列表,name为对应的索引名  ):

<?xml version="1.0" encoding="UTF-8"?>
<dataConfig>
       <dataSource type="JdbcDataSource" driver="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@192.168.56.26:1521:gosp" user="newChannel" password="newChannel"/>
             <document name="bookInfo">
         <entity name="bookInfo" transformer="ClobTransformer" query="select * from book_info">
             <field column="BOOK_ID" name="id" />
                                                         <field column="BOOK_NAME" name="bookName"/>
             <field column="BOOK_AUTH"  name="bookAuth" />
             <field column="BOOK_DESC" name="bookDesc" clob="true"/>
         </entity>
     </document>
 </dataConfig>

                         D)修改solr-home\collection1\conf目录下的solrconfg.xml,在相应的位置添加如下代码(告诉solr刚才配置的db-data-config.xml的位置):

<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
     <lst name="defaults">
       <str name="config">D:\solr\solr-tomcat\solr\collection1\conf\db\db-data-config.xml</str>
     </lst>
  </requestHandler>

                         E)将solr-4.4.0.zip解压,然后将/dist中的solr-dataimporthandler-4.4.0.jar以及solr-dataimporthandler-extras-4.4.0.jar拷贝到solr.war的lib目录下。 

                         F)重启启动tomcat,进入collection1/dataimport,command选择full-import,单击Execute,全量将该表的数据导入到索引。然后在Query出查询,存在刚才表中的数据,则索引数据导入成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值