zoukankan      html  css  js  c++  java
  • [solr]

    这里使用的是mysql测试。

    1、先在mysql中建一个表:solr_test

    2、插入几条测试数据:

    3、用记事本打solrconfig.xml文件,在solrhome文件夹中。E:solrhomemycoreconfsolrconfig.xml

    (solrhome文件夹是什么,参见:http://www.cnblogs.com/HD/p/3977799.html)

    加入这个节点:

     <requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
            <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 id, subject, content, last_update_time FROM solr_test WHERE id >= ${dataimporter.request.id}">
                <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-INFlib目录下。

    当然,也包括mysql的jdbc jar包:mysql-connector-java-5.1.7-bin.jar

    (还有一种方法是在solrconfig.xml中加入lib节点,然后把jar包放到solrhome下,这样可以不在WEB-INFlib中加入jar包)

    6、用记事本打开schema.xml,在在solrhome文件夹中(同第3点)。内容为:

    <?xml version="1.0" ?>
    <schema name="my core" version="1.1">
    
        <fieldtype name="string"  class="solr.StrField" sortMissingLast="true" omitNorms="true"/>
        <fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/>
        <fieldType name="date" class="solr.TrieDateField" precisionStep="0" positionIncrementGap="0"/>
        <fieldType name="text_cn" class="solr.TextField">
            <analyzer type="index" class="org.wltea.analyzer.lucene.IKAnalyzer" />
            <analyzer type="query" class="org.wltea.analyzer.lucene.IKAnalyzer" />
        </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

  • 相关阅读:
    python爬虫-selenium八大定位笔记
    git pull : error: cannot lock ref 'refs/remotes/origin/*' (unable to update local ref) 解决方案
    Lua table.sort()原理和使用的坑
    Unity---有关游戏物体角度的两种赋值方法这件事
    C# #if、#endif和预处理指令
    Unity 4大坐标系 和 屏幕坐标与UI坐标的转换问题
    第2次参加游戏开发比赛
    Unity Text添加空格导致换行问题的两种解决方法(还有lua的解决方法)
    MySQL是如何处理千万级数据
    PHP使用守护进程处理队列
  • 原文地址:https://www.cnblogs.com/faunjoe88/p/7131150.html
Copyright © 2011-2022 走看看