Solr 3.6.2索引MySQL数据库配置过程

2019-03-27 01:16|来源: 网路

前言

     下面的步骤开起来比较多,其实总结下来不外乎以下几点
  • 给solr配置mysql数据库驱动(步骤2.1)
  • 告诉solr,要从一个地方导入数据。(步骤2.2)
  • 告诉solr,mysql数据库的 地址,用户名,密码,数据库名等等。(步骤2.3)
  • 告诉solr,要为mysql数据库建立那些索引域。(步骤2.4)
  • 从mysql数据库中导入数据。(步骤2.6)
        后面添加中文分词部分
  • 为solr新建一个可分词的数据类型 “text_cn”
  • 导入IKAnalyzer分词包
  • 将步骤2.4中的数据类型改为“text_cn”。

一、      试运行solr

cmd 进入solr下的example目录:cd  /d  apache-solr-3.6.2\example

执行java命令:java  -jar start.jar

测试是否成功运行solr:访问URLhttp://localhost:8983/solr/admin/

二、      配置Solr索引MySQL数据库表

准备工作

在本地的MySQL数据库中执行:

SQL语句

DROP TABLE IF EXISTS `documents`; 
CREATE TABLE `documents` ( 
  `id` int(11) NOT NULL auto_increment, 
  `date_added` datetime NOT NULL, 
  `title` varchar(255) NOT NULL, 
  `content` text NOT NULL, 
  PRIMARY KEY  (`id`) 
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; 
-- ---------------------------- 
-- Records of documents 
-- ---------------------------- 
INSERT INTO `documents` VALUES ('1', '2012-01-11 23:15:59', 'world', 'test1'); 
INSERT INTO `documents` VALUES ('2', '2012-01-11 23:16:30', 'hello', 'test'); 
INSERT INTO `documents` VALUES ('3', now(), 'hello12', 'test'); 
INSERT INTO `documents` VALUES ('4', now(), ‘我们’, 'test');

 

2.1.  复制mysql-connector-java-5.1.25-bin.jar(去网上下载)文件到目录apache-solr-3.6.2\example\lib。它是mysql的驱动。

 

2.2.  配置apache-solr-3.6.2\example\solr\conf\solrconfig.xml。在文件中加入:

apache-solr-3.6.2\example\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>

<lib dir="../../dist/" regex="apache-solr-cell-\d.*\.jar" />前面加入

<lib dir="../../dist/" regex="apache-solr-dataimporthandler-\d.*\.jar" />

 

2.3.  apache-solr-3.6.2\example\solr\conf\目录下创建data-config.xml文件,其内容如下。其目的是指定了MySQL数据库的地址、用户名、密码和建立索引的数据表。

apache-solr-3.6.2\example\solr\conf\data-config.xml

<dataConfig> 
<dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" 
    url="jdbc:mysql://localhost/italk " user="username" password="password"/>
  <document name="documents1" >
        <entity name="documents" 
          query="SELECT id, content, 
date_added, title FROM documents"  > 
<field column="id" name="id" /> <field column="content" name="content" /> <field column="title" name="hashcode" /> <field column="date_added" name="updatetime" /> </entity> </document> </dataConfig>

 

2.4.  solr中为数据库表字段建立域,编辑apache-solr-3.6.2\example\solr\conf\schema.xml

² 删除<fields></fields>节点间的所有内容

² 删除 <uniqueKey>id</uniqueKey> </schema>之间所有内容

apache-solr-3.6.2\example\solr\conf\schema.xml:在<fields></fields>节点间插入

<field name="id" type="string" indexed="true" stored="true" required="true" /> 
<field name="title" type="text_general" indexed="true" stored="true" termVectors="true" termPositions="true" termOffsets="true"/> 
<field name="content" type="text_general" indexed="true" stored="true" termVectors="true" termPositions="true" termOffsets="true"/> 
<field name="date_added" type="date" indexed="false" stored="true"/> 


 

2.5.  重新执行java  -jar start.jar

 

2.6.  执行所用数据库命令:http://localhost:8983/solr/dataimport?command=full-import

 

2.7.  再次访问:http://localhost:8983/solr/admin/ Query string是默认的 *:*。意思是列出所有数据来。

点击“search”按钮,查看索引的全部数据。

 

三、      加入中文分词工具IKAnalyzer

solr分词工具设为IKAnalyzer,IKAnalyzer下载地址:http://code.google.com/p/ik-analyzer/downloads/list

 

3.1.  编辑apache-solr-3.6.2\example\solr\conf\schema.xml,添加自定义的中文类型text_cn,并配置其分词器。

apache-solr-3.6.2\example\solr\conf\schema.xml文件的<types></types>节点间插入

<fieldType name="text_cn" class="solr.TextField" positionIncrementGap="100">
            <analyzer type="index">
                <tokenizer class="org.wltea.analyzer.solr.IKTokenizerFactory"
                          isMaxWordLength="false"/>
                <filter class="solr.StopFilterFactory"
                    ignoreCase="true" words="stopwords.txt"/>
                <filter class="solr.WordDelimiterFilterFactory"
                    generateWordParts="1" generateNumberParts="1"
                    catenateWords="1" catenateNumbers="1" catenateAll="0"
                    splitOnCaseChange="1"/>
                <filter class="solr.LowerCaseFilterFactory"/>
                <filter class="solr.EnglishPorterFilterFactory"
                    protected="protwords.txt"/>
                <filter class="solr.RemoveDuplicatesTokenFilterFactory"/>
            </analyzer>
            <analyzer type="query">
                <tokenizer class="org.wltea.analyzer.solr.IKTokenizerFactory"
                          isMaxWordLength="true"/>
                <filter class="solr.StopFilterFactory"
                    ignoreCase="true" words="stopwords.txt"/>
                <filter class="solr.WordDelimiterFilterFactory"
                    generateWordParts="1" generateNumberParts="1"
                    catenateWords="1" catenateNumbers="1" catenateAll="0"
                    splitOnCaseChange="1"/>
                <filter class="solr.LowerCaseFilterFactory"/>
                <filter class="solr.EnglishPorterFilterFactory"
                    protected="protwords.txt"/>
                <filter class="solr.RemoveDuplicatesTokenFilterFactory"/>
            </analyzer>
        </fieldType>

 

3.2.  添加IKAnalyzer分词工具包IKAnalyzer2012_u6.jar到目录apache-solr-3.6.2\example\solr\lib

 

3.3.  修改schema.xml文件中的content title域的数据类型。从”text_general”改为”text_cn”


3.4 测试分词工具,重新运行solr,进入:http://localhost:8983/solr/admin/analysis.jsp 

         如下配置,测试分词是否成功。                 

        

参考:http://blog.csdn.net/fover717/article/details/7551867 (向其致敬)

 


转自:http://blog.csdn.net/zhanghui_hn/article/details/9269453

相关问答

更多
  • QQ比微信功能多,都是用来聊天,微信有的功能QQ基本都有,而且功能更多,为什么还是有那么多人用微信呢
  • tomcat配置数据源solr使用数据源 1、tomcat中配置数据源(注:需要拷贝jdbc相关jar包到tomcat中。tomcat6.x/lib目录下,注意tomcat6.0以下的版本在tomcat5.x/common/lib/目录下) 在server.xml文件,找到“Engine-> Host -> Context”,在其下面配置主要针对某一项目的数据源使用。 在context.xml文件,找到“Context”,在其下面配置可以让所有项目使用。 在Context下面配置数据源如下: 数据源相关参数 ...
  • solr不安全,没事务 ,没有表关系
  • tomcat配置数据源solr使用数据源 1、tomcat中配置数据源(注:需要拷贝jdbc相关jar包到tomcat中。tomcat6.x/lib目录下,注意tomcat6.0以下的版本在tomcat5.x/common/lib/目录下) 在server.xml文件,找到“Engine-> Host -> Context”,在其下面配置主要针对某一项目的数据源使用。 在context.xml文件,找到“Context”,在其下面配置可以让所有项目使用。 在Context下面配置数据源如下: & ...
  • DIH提供执行Delta导入的功能 。 DIH将保留上次成功构建的时间戳。 因此,如果您的数据库正在维护更新记录的时间戳,则delta查询将考虑在最后一个构建时间戳之后更新的所有记录。 它是为指数增量建立的一种方式。 DIH provides the ability to perform Delta imports. DIH will maintain the timestamp for the last successful builds. SO if your database is maintaini ...
  • 这些DB服务器的用途不同,它在很大程度上取决于您的应用程序(以及您存储的数据类型)是否应该仅使用Solr或MySQL。 MySQL可以很好地存储具有大量关系和表格的数据(彼此相关的表格)。 Solr很适合文本搜索(正如你所说:快速索引),如果你没有很多“相关数据”,你确实可以将这些数据存储在相同的文档中。 有些人确实只使用Solr来存储他们的数据库...但我仍然认为RDBM可以很好地用于某些类型的数据。 例如:如果您想允许快速搜索系统用户并存储他们的完整个人资料,以及一些信息详细信息......最好使用So ...
  • 您在每个查询中都给出了参数rows=2147483647 。 该参数的含义是(取自参考文献) 您可以使用rows参数对查询中的结果进行分页。 该参数指定Solr应一次返回客户端的完整结果集中的最大文档数。 默认值为10.也就是说,默认情况下,Solr一次返回10个文档以响应查询。 因此,您告诉Solr生效,在单个响应中发送查询的所有匹配。 这是你糟糕表现的原因。 当查询“java”时 ,谷歌是否会向您发送所有500.000.000次匹配,不会。 为什么不,性能。 我知道的每一个IR应用程序都会为您提供一个带 ...
  • 除了新功能之外,Solr 3.6和Solr 4.0之间是否有任何重大差异? 我发现这个问题很奇怪,至少可以说。 错误修复和新功能是发布的全部内容! 您可以在此处查看 Solr版本的完整更新日志。 不要忘记Solr和Lucene是一致发布的,所以你还需要在两个项目中寻找相关的变化。 我可以安全地使用我在Solr 4.0中的现有查询(在Solr 3.6中工作的查询)吗? 查询应该没问题,但索引 - 可能不是。 引用另一篇SO帖子中的 javanna: 索引格式已更改,但Solr将负责升级索引。 一旦用旧索引启动 ...
  • 是的,可以将已爬网数据索引到Solr。 我以前做过这个。 您需要创建一个实现IPipelineStep的自定义管道步骤,并将其添加到您的NCrawler实现中。 我使用SolrNet作为连接Solr的客户端。 这里有一些代码可以帮助您入门。 SolrNet.Startup.Init("http://localhost:8983/solr"); using(Crawler c = new Crawler("http://ncrawler.codeplex.com/", n ...
  • 这里有一篇很好的文章将帮助您完成PHP和SOLR的集成: http://www.ibm.com/developerworks/opensource/library/os-php-apachesolr/ SOLR有许多PHP接口,该文章引用了PHP SOLR客户端: http://code.google.com/p/solr-php-client/ 但也有这个: http://pecl.php.net/package/solr There's a good article here that will hel ...