此篇基于上一篇solr4.9.1和中文分词安装完成
1、上述配置好后,在solr_home文件夹中,将包含collection1文件夹,这就是solr的一个实例。collection1中包含conf和data两个子文件夹。data中包含tlog和index(如果没有也没关系,稍后再solr建立索引时,将会被创建)。tlog是记录日志的文件夹,index是存放索引的文件夹。conf中包含lang文件夹和若干文件。lang文件夹中包含的是词库文件,但是solr默认是没有中文词库的,所以之后会将中文词库加入该文件夹中。在conf中,包含了若干xml文件,我们针对solr配置,是需要配置solrconfig.xml和schema.xml即可。
2、先配置solrconfig.xml。solrconfig.xml是solr的核心文件。这里包含了jar包引用,数据库读取路径配置,操作接口配置。
jar包配置如下:
<span style="white-space:pre"> </span><lib dir="../contrib/extraction/lib" regex=".*\.jar" />
<span style="white-space:pre"> </span><lib dir="../dist/" regex="solr-cell-\d.*\.jar" />
<span style="white-space:pre"> </span><lib dir="../contrib/clustering/lib/" regex=".*\.jar" />
<span style="white-space:pre"> </span><lib dir="../dist/" regex="solr-clustering-\d.*\.jar" />
<span style="white-space:pre"> </span><lib dir="../contrib/langid/lib/" regex=".*\.jar" />
<span style="white-space:pre"> </span><lib dir="../dist/" regex="solr-langid-\d.*\.jar" />
<span style="white-space:pre"> </span><lib dir="../contrib/velocity/lib" regex=".*\.jar" />
<span style="white-space:pre"> </span><lib dir="../dist/" regex="solr-velocity-\d.*\.jar" />
<span style="white-space:pre"> </span><lib dir="../contrib/dataimporthandler/lib" regex=".*\.jar" />
<span style="white-space:pre"> </span><lib dir="../dist/" regex="solr-dataimporthandler-\d.*\.jar" />
其中,最后两行是数据导入的handler,这包含了从数据库读取数据所需要的jar包。这些jar的目录都在solr_home\contrib这个文件夹中。
3、配置dataimporthandler
<span style="white-space:pre"> </span><requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<span style="white-space:pre"> </span><lst name="defaults">
<span style="white-space:pre"> </span><str name="config">data-config.xml</str>
<span style="white-space:pre"> </span> </lst>
<span style="white-space:pre"> </span></requestHandler>
4、在放在conf文件夹中创建data-config.xml,内容如下:
<dataConfig>
<span style="white-space:pre"> </span><dataSource type="JdbcDataSource"
<span style="white-space:pre"> </span> driver="com.mysql.jdbc.Driver"
<span style="white-space:pre"> </span> url="jdbc:mysql://localhost/yourDBname"
<span style="white-space:pre"> </span> user="root"
<span style="white-space:pre"> </span> password="root"/>
<span style="white-space:pre"> </span><document>
<span style="white-space:pre"> </span><entity name="question1" query="select Guid,title,QuesBody,QuesParse,QuesType from question1 where Guid is not null">
<span style="white-space:pre"> </span> <span style="white-space:pre"> </span><field column="Guid" name="id"/>
<span style="white-space:pre"> </span> <field column="title" name="question1_title"/>
<span style="white-space:pre"> </span> <field column="QuesBody" name="question1_body"/>
<span style="white-space:pre"> </span><span style="white-space:pre"> </span><field column="QuesParse" name="question1_parse"/>
<span style="white-space:pre"> </span> <field column="QuesType" name="question1_type"/>
<span style="white-space:pre"> </span> </entity>
<span style="white-space:pre"> </span> <entity name="question2" query="select Guid,title,QuesBody,QuesParse,QuesType from question2 where Guid is not null">
<span style="white-space:pre"> </span> <field column="Guid" name="id"/>
<span style="white-space:pre"> </span> <field column="title" name="question2_title"/>
<span style="white-space:pre"> </span><field column="QuesBody" name="question2_body"/>
<span style="white-space:pre"> </span> <span style="white-space:pre"> </span><field column="QuesParse" name="question2_parse"/>
<span style="white-space:pre"> </span><field column="QuesType" name="question2_type"/>
<span style="white-space:pre"> </span> </entity>
<span style="white-space:pre"> </span> </document>
</dataConfig>
如上,包含了datasource和document两个大标签。datasource正如其名,包含了数据库的配置信息。document包含了entity。entity就是一个从数据库读取数据的动作。
query就是读取数据所用的sql,field是数据库中的字段与schma中的字段进行匹配的列表。稍后在schma.xml的介绍中,将会详细说明。
我们回到solrconfig.xml中,requestHandler这里定义了相应http请求的接口。如之前配置的name为/dataimport接口,在中间件启动后,访问http://localhost:8080/solr/collection1/dataimport即可查看数据导入的状态。若执行命令,便可执行http://localhost:8080/solr/collection1/dataimport?command=full-import 即可(这句的含义是全部重新索引,之前的索引将被删除),其他命令,请参考http://www.cnblogs.com/llz5023/archive/2012/11/15/2772154.html。同理,通过相同的形式,即可实现对solr的增删改查。这里还能对requestHandler进行一些高级配置,感兴趣的同学可以到apache-solr-ref-guide-4.8中阅读。
5.schma.xml配置
schma.xml完成了对索引数据的类型配置和索引一些相关动作的配置(如分词方法配置)。
solr需要为每条索引定义一个id作为主键,而且在查询中必须要有字段与主键id进行对应,否则将会报错。如在data-config中的Guid与id进行匹配,将guid作为主键。
field为solr索引的基本类型,type的值与fieldType对应,即通过type为每个field指定一个fieldType,而fieldType将为field规定如何进行索引。