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
  • 相关阅读:
    艾伟_转载:基于.NET平台的Windows编程实战(三)—— 项目的创建及主界面的设计 狼人:
    艾伟_转载:C# Design Patterns (2) Strategy 狼人:
    艾伟_转载:C# Design Patterns (5) Prototype 狼人:
    艾伟_转载:正则表达式30分钟入门教程 狼人:
    艾伟_转载:WCF安全之EndPointIdentity 狼人:
    艾伟_转载:老赵谈IL(3):IL可以看到的东西,其实大都也可以用C#来发现 狼人:
    艾伟_转载:.NET平台上的Memcached客户端介绍 狼人:
    艾伟_转载:关于.NET中的循环引用 狼人:
    艾伟_转载:VS 2008快捷键 狼人:
    艾伟_转载:Regex.Replace 方法的性能! 狼人:
  • 原文地址:https://www.cnblogs.com/HD/p/3981677.html
Copyright © 2011-2022 走看看