1)在solrconfig.xml增加
<!--新增加的配置-->
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requestHandler>
2)然后在solrconfig.xml同一个目录下,即是solr home目录下增加 data-config.xml 文件
<dataConfig>
<dataSource driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/test" user="root" password="123456"/>
<document name="movieDoc">
<entity name="movie" transformer="RegexTransformer,DataToIntTransformer" query="select * from movie" >
<field column="actors" splitBy="," sourceColName="actors"/>
<field column="director" splitBy="," sourceColName="director"/>
<entity name="type" query="select id as tid from movie_type where mid='${movie.mid}'">
<field name="tid" column="tid" />
</entity>
<entity name="language" query="select id as lid from movie_language where mid='${movie.mid}'">
<field name="lid" column="lid" />
</entity>
<entity name="zone" query="select id as zid from movie_zone where mid='${movie.mid}'">
<field name="zid" column="zid" />
</entity>
<entity name="detail" query="select sub_index,title,vid from movie_detail where mid='${movie.mid}' and chk_yn='y'">
<field name="sub_index" column="sub_index" />
<field name="title" column="title" />
<field name="vid" column="vid" />
</entity>
<!--
<entity name="item_category" query="select CATEGORY_ID from item_category where item_id='${item.ID}'">
<entity name="category" query="select description from category where id = '${item_category.CATEGORY_ID}'">
<field column="description" name="cat" />
</entity>
</entity>
-->
</entity>
</document>
</dataConfig>
3)加入相关的jar包,apache-solr-dataimporthandler-1.4.0.jar,apache-solr-dataimporthandler-extras-1.4.0.jar,mysql-connector-java-5.1.10.jar
启动tomcat,访问http://localhost:8080/solr/dataimport?command=full-import 将数据全部导入solr服务器进行索引
访问http://localhost:8080/solr/dataimport?command=status可以查看运行状态
当修改data-config.xml 文件配置时运行http://localhost:8080/solr/dataimport?command=reload-config可以进行重新加载配置文件
如果想终止运行http://localhost:8080/solr/dataimport?command=abort
要建立自己的全文检索,一般都需要从数据库导入数据,在原来配置的基础上,增加导入的功能
1、D:\apache-tomcat-7.0.27\solr\conf\solrconfig.xml中增加
- <requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
- <lst name="defaults">
- <str name="config">data-config.xml</str>
- </lst>
- </requestHandler>
2、增加D:\apache-tomcat-7.0.27\solr\conf\data-config.xml,内容为数据库的连接信息
- <?xml version="1.0" encoding="UTF-8"?>
- <dataConfig>
- <dataSource type="JdbcDataSource" driver="oracle.jdbc.driver.OracleDriver"
- url="jdbc:oracle:thin:@10.74.8.206:1521:orcl"
- user="uname"
- password="pwd"/>
- <document name="zpxx">
- <entity name="zpxx" query="select * from vw_zp_fullindex" transformer="ClobTransformer">
- <field column="GANG_WEI_BH" name="GANG_WEI_BH" />
- <field column="GANG_WEI_MC" name="GANG_WEI_MC" />
- <field column="GANG_WEI_MS" name="GANG_WEI_MS" clob="true"/>
- <field column="GONG_ZU_DD_ZW" name="GONG_ZU_DD_ZW" />
- <field column="QI_TA" name="QI_TA" />
- <field column="YUE_XIN" name="YUE_XIN" />
- <field column="ZHI_CHENG" name="ZHI_CHENG" />
- <field column="GANGWEILB" name="GANGWEILB" />
- <field column="COMPID" name="COMPID" />
- <field column="DAN_WEI_MC" name="DAN_WEI_MC" />
- <field column="DANWEIXZ" name="DANWEIXZ" />
- <field column="JING_YING_FW" name="JING_YING_FW" />
- <field column="DAN_WEI_JJ" name="DAN_WEI_JJ" clob="true"/>
- <field column="DAN_WEI_DZ" name="DAN_WEI_DZ" />
- <field column="HANGYELB" name="HANGYELB" />
- </entity>
- </document>
- </dataConfig>
因为有Clob字段,所以需要加上ClobTransformer
3、D:\apache-tomcat-7.0.27\solr\conf\schema.xml文件中增加
- <!--自定义Field开始 -->
- <field name="GANG_WEI_BH" type="string" indexed="true" stored="true" required="true"/>
- <field name="GANG_WEI_MC" type="text" indexed="true" stored="true" />
- <field name="GANG_WEI_MS" type="text" indexed="true" stored="true" />
- <field name="GONG_ZU_DD_ZW" type="text" indexed="true" stored="true" />
- <field name="QI_TA" type="text" indexed="true" stored="true" />
- <field name="YUE_XIN" type="text" indexed="true" stored="true" />
- <field name="ZHI_CHENG" type="text" indexed="true" stored="true" />
- <field name="GANGWEILB" type="text" indexed="true" stored="true" />
- <field name="COMPID" type="string" indexed="true" stored="true" />
- <field name="DAN_WEI_MC" type="text" indexed="true" stored="true" />
- <field name="DANWEIXZ" type="text" indexed="true" stored="true" />
- <field name="JING_YING_FW" type="text" indexed="true" stored="true" />
- <field name="DAN_WEI_JJ" type="text" indexed="true" stored="true" />
- <field name="DAN_WEI_DZ" type="text" indexed="true" stored="true" />
- <field name="HANGYELB" type="text" indexed="true" stored="true" />
- <!--自定义Field结束 -->
- <copyField source="DAN_WEI_JJ" dest="text"/><!--clob字段 -->
- <copyField source="GANG_WEI_MS" dest="text"/>
把原有文件中id字段的required="true"去掉,否则导入的时候,会验证id字段,其实自己在做的时候,如果其他字段没有的话,可以删除
3、还要把oracle的jdbc驱动放到D:\apache-tomcat-7.0.27\webapps\solr\WEB-INF\lib
4、http://localhost:8080/solr/dataimport?command=full-import即可以进行导入数据
5、查询则通过http://localhost:8080/solr/admin/进行查询,输入查询字符串(Query String)"DAN_WEI_JJ:计算机"即可以查看全文检索结果