一:solr启动

  目前solr最高版本为5.5.0版本,很多solr安装都是说将server文件copy到tomcat中,但是solr版本自带有jetty的启动方式

  首先下载solr-5.5.0版本,解压后文件目录:

  

打开readme.txt ,发现有之下的几句话:

告诉你使用命令行启动的方式,本机为windows系统,所以打开cmd(shift+邮件--->在此处打开命令端口),进入bin目录,输入命令solr start

这样就算是启动成功了,可以直接在浏览器中输入http://localhost:8983 查看solr管理后台

 

二:创建solr  core

 

    1.首先进入/server/solr 文件夹,创建一个文件夹,名称为你所建core的名称, 这里我的core取名为mark

    2.在mark目录下创建一个core.properties文件,里面写上:name=mark

    3.首先进入/server/solr/configsets/basic_configs,将conf目录copy到刚才的mark目录下

  4.将managed-schema文件修改为schema.xml 文件,这是solr比较核心的文件,用来定义索引

 

先看看这个schema.xml文件

第一行: ,将这个name改为我们自己的core名称:即mark

第113行定义了id,这个可以任意修改

167行定义了唯一主键,这个主键可以是任意的类型和字段,但必须是唯一非空的,当有重复的时候,solr将会替换掉之前所创建的索引。

这里是一个默认的对文本的处理,其中配置的分词器,过滤器等。

配置上这些其实就算成功创建了一个core,可以打开后台,可以看到新添加了要给core selector.

 

 三:配置solr 数据库导入

     solr有插件可以直接从数据库中导入数据,并创建索引。主要使用solr-dataimporthandler-5.5.0.jar

  首先在solrhome/dist 中找到 solr-dataimporthandler-5.5.0.jar,solr-dataimporthandler-extras-5.5.0.jar,solr-core-5.5.0.jar三个包,一并copy到solrhomt/server/lib中

  在网上下载一个mysql-connector-java.jar copy到solrhomt/server/lib中

  在我们自己的core  mark中,找到solrconfig.xml文件

  在这个文件的根节点下添加

   

 <requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">   
          <lst name="defaults">   
               <str name="config">data-config.xml</str>   
          </lst>   
  </requestHandler>

      这是给solr服务器添加一个severlet,用来接收dataimport请求。其中配置文件data-config.xml与solrconfig.xml放在同一个目录下

  创建一个文件data-config.xml

  配置:

  

<dataConfig>   
   <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/morequ" user="root" password="root"/>

    <document>
        <entity name="mark" query="select id,value,type,datam_id,create_time  from mark where enable =0 " 
        deltaImportQuery="SELECT id,value,type,datam_id,create_time from mark where enable =0  and id='${dih.delta.id}'  " 
        deltaQuery="SELECT  id  from mark where enable =0 and create_time > '${dih.last_index_time}'" >
            <field column="id" name="id" />
            <field column="value" name="value" />
            <field column="type" name="type" />
            <field column="datam_id" name="datam_id" />
            <field column="create_time" name="create_time"/>
        </entity>
    </document>
</dataConfig>

        使用dataimport需要一个配置properties,存放一些变量,新建dataimport.properties 文件放在与data-config.xml文件同目录下

  配置好重启solr

  在后台,我们看到了一个新的菜单:

  

 command可以选择是全量更新,还是增量更新。

  

四:配置增量更新定时任务

  当有了数据导入后,并不能实现我们想要的功能,则需要一个定时任务的东西去定时导入增量数据:

  可以下载apache-solr-dataimportscheduler-1.0.jar 包,

  1.将apache-solr-dataimportscheduler-1.0.jar 放在solrhome/server/solr-webapp/WEB-INF/lib里

  注意,此包有一个问题,加载配置文件找不到

  原因在这个文件中,

这里给他一个指定的文件路径:

   2. 放好jar包之后,需要配置配置文件

  下载jar的时候,如果下的是with source版本,则里面自带一个dataimport.properties 配置文件,

  注意,此时在solrhome/server/solr 中(注意,这里不是jar包的存放位置webapp),创建一个conf文件夹,将dataimport.properties放在conf中

  

  如果没有的这里贴出来配置文件:

  

    #################################################  
    #                                               #  
    #       dataimport scheduler properties         #  
    #                                               #  
    #################################################  
      
    #  to sync or not to sync  
    #  1 - active; anything else - inactive  
    syncEnabled=1
      
    #  which cores to schedule  
    #  in a multi-core environment you can decide which cores you want syncronized  
    #  leave empty or comment it out if using single-core deployment  
    syncCores=mark
      
    #  solr server name or IP address  
    #  [defaults to localhost if empty]  
    server=localhost
      
    #  solr server port  
    #  [defaults to 80 if empty]  
    port=8983
      
    #  application name/context  
    #  [defaults to current ServletContextListener's context (app) name]  
    webapp=solr
      
    #  URL params [mandatory]  
    #  remainder of URL  
    #  增量更新的请求参数  
    params=/dataimport?command=delta-import&clean=false&commit=true
      
    #  schedule interval  
    #  number of minutes between two runs  
    #  [defaults to 30 if empty]  
    #  这里配置的是2min一次  
    interval=2
      
    #  重做索引的时间间隔,单位分钟,默认7200,即5天;   
    #  为空,为0,或者注释掉:表示永不重做索引  
    reBuildIndexInterval=0
      
    #  重做索引的参数  
    reBuildIndexParams=/dataimport?command=full-import&clean=true&commit=true
      
    #  重做索引时间间隔的计时开始时间,第一次真正执行的时间=reBuildIndexBeginTime+reBuildIndexInterval*60*1000;  
    #  两种格式:2012-04-11 03:10:00 或者  03:10:00,后一种会自动补全日期部分为服务启动时的日期  
    reBuildIndexBeginTime=03:10:00

     3.打开solrhome\server\solr-webapp\webapp\WEB-INF\web.xml

    添加一个listener:

    

   <listener>  
       <listener-class>org.apache.solr.handler.dataimport.scheduler.ApplicationListener</listener-class>  
   </listener> 


   4.重新启动solr,定时器任务则生效。

 

参考资料:

  http://www.cnblogs.com/atyou/archive/2013/04/21/3033675.html

  

  

 

posted on 2016-03-08 15:25  大招无限  阅读(638)  评论(0编辑  收藏  举报