diaoyudao 发表于 2017-12-18 21:22:53

耐心等待

  这里使用的是mysql测试。
  1、先在mysql中建一个表:solr_test

  2、插入几条测试数据:

  3、用记事本打solrconfig.xml文件,在solrhome文件夹中。E:\solrhome\mycore\conf\solrconfig.xml
  (solrhome文件夹是什么,参见:http://www.cnblogs.com/HD/p/3977799.html)
  加入这个节点:
  

<requestHandler name="/dataimport">  <lst name="defaults">
  <str name="config">data-config.xml</str>
  </lst>
  
</requestHandler>
  

  4、新建一个data-config.xml文件,与solrconfig.xml同一个目录下。内容为
  

<dataConfig>  <dataSource type="JdbcDataSource"
  driver="com.mysql.jdbc.Driver"
  url="jdbc:mysql://localhost/test"
  user="root"
  password="root" />
  <document>
  <entity name="solr_test" transformer="DateFormatTransformer"

  query="SELECT>  <field column='last_update_time' dateTimeFormat='yyyy-MM-dd HH:mm:ss' />
  </entity>
  </document>
  
</dataConfig>
  

  说明:这里使用了一个${dataimporter.request.id},这个是参数,后面在做数据导入时,会使用到,以此条件为基准读数据。
  5、复制解压出的solr jar包solr-dataimporthandler-4.10.0.jar和solr-dataimporthandler-extras-4.10.0.jar到tomcat solr webapp的WEB-INF\lib目录下。
  当然,也包括mysql的jdbc jar包:mysql-connector-java-5.1.7-bin.jar
  (还有一种方法是在solrconfig.xml中加入lib节点,然后把jar包放到solrhome下,这样可以不在WEB-INF\lib中加入jar包)
  6、用记事本打开schema.xml,在在solrhome文件夹中(同第3点)。内容为:
  

<?xml version="1.0" ?>  
<schema name="my core" version="1.1">
  

  <fieldtype name="string"sortMissingLast="true" omitNorms="true"/>
  <fieldType name="long" precisionStep="0" positionIncrementGap="0"/>
  <fieldType name="date" precisionStep="0" positionIncrementGap="0"/>
  <fieldType name="text_cn">
  <analyzer type="index" />
  <analyzer type="query" />
  </fieldType>
  <!-- general -->
  <field name="id" type="string" indexed="true" stored="true" multiValued="false" required="true"/>
  <field name="subject" type="text_cn" indexed="true" stored="true" />
  <field name="content" type="text_cn" indexed="true" stored="true" />
  <field name="last_update_time" type="date" indexed="true" stored="true" />
  <field name="_version_" type="long" indexed="true" stored="true"/>
  

  <!-- field to use to determine and enforce document uniqueness. -->
  <uniqueKey>id</uniqueKey>
  

  <!-- field for the QueryParser to use when an explicit fieldname is absent -->
  <defaultSearchField>subject</defaultSearchField>
  

  <!-- SolrQueryParser configuration: defaultOperator="AND|OR" -->
  <solrQueryParser defaultOperator="OR"/>
  
</schema>
  

  7、打开solr web:

  说明:
  Custom Parameters填入id=1,这是在第4点中设置的参数。
  Clean选项,是指是否删除未匹配到的数据。也就是在数据库select结果中没有,而solr索引库中存在,则删除。
  也可以使用这个地址直接访问:
  

http://localhost:8899/solr/mycore/dataimport?command=full-import&clean=true&commit=true&wt=json&indent=true&entity=solr_test&verbose=false&optimize=false&debug=false&id=1  

  将返回结果:

  配置好后,之后我们只需要使用这个url地址,就可以不段的去导入数据做索引了。(就这么简单)
  8、测试查询:
  
  当然,dataimport可以加入参数命令,让其重新加载data-config.xml
  http://localhost:8899/solr/#/mycore/dataimport/command=reload-config
页: [1]
查看完整版本: 耐心等待