热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

全文检索引擎Solr系列——整合MySQL、MongoDB

MySQL拷贝mysql-connector-java-5.1.25-bin.jar到E:\solr-4.8.0\example\solr-webapp\webapp\WEB-INF\lib目录下面配

MySQL

  1. 拷贝mysql-connector-java-5.1.25-bin.jar到E:\solr-4.8.0\example\solr-webapp\webapp\WEB-INF\lib目录下面
  2. 配置E:\solr-4.8.0\example\solr\collection1\conf\solrconfig.xml
123456 <requestHandler name="/dataimport"     class="org.apache.solr.handler.dataimport.DataImportHandler">        <lst name="defaults">           <str name="config">data-config.xmlstr>        lst> requestHandler>
  1. 导入依赖库文件:
      

    加在

      

    前面。

  2. 创建E:\solr-4.8.0\example\solr\collection1\conf\data-config.xml,指定MySQL数据库地址,用户名、密码以及建立索引的数据表
    xml version="1.0" encoding="UTF-8" ?>      <dataConfig              <dataSource type="JdbcDataSource"                          driver="com.mysql.jdbc.Driver"                          url="jdbc:mysql://localhost:3306/django_blog"                          user="root"                          password=""/>                    <document name="blog"                          <entity name="blog_blog" pk="id"                                  query="select id,title,content from blog_blog"                                  deltaImportQuery="select id,title,content from blog_blog where ID='${dataimporter.delta.id}'"                                   deltaQuery="select id  from blog_blog where add_time > '${dataimporter.last_index_time}'"                                    deletedPkQuery="select id  from blog_blog where id=0">                                 <field column="id" name="id" />                                 <field column="title" name="title" />                                 <field column="content" name="content"/>                            entity                 document>       dataConfig>
    • query 用于初次导入到索引的sql语句。
      • 考虑到数据表中的数据量非常大,比如千万级,不可能一次索引完,因此需要分批次完成,那么查询语句query要设置两个参数:${dataimporter.request.length} ${dataimporter.request.offset}
      • query=”select id,title,content from blog_blog limit ${dataimporter.request.length} offset ${dataimporter.request.offset}”
      • 请求:http://localhost:8983/solr/collection2/dataimport?command=full-import&commit=true&clean=false&offset=0&length=10000
    • deltaImportQuery 根据ID取得需要进入的索引的单条数据。
    • deltaQuery 用于增量索引的sql语句,用于取得需要增量索引的ID。
    • deletedPkQuery 用于取出需要从索引中删除文档的的ID
  3. 为数据库表字段建立域(field),编辑E:\solr-4.8.0\example\solr\collection1\conf\schema.xml:

   <field name="id" type="string" indexed="true" stored="true" required="true" />    <field name="title" type="text_cn" indexed="true" stored="true" termVectors="true" termPositions="true" termOffsets="true"/>    <field name="content" type="text_cn" indexed="true" stored="true" termVectors="true" termPositions="true" termOffsets="true"/>
  1. 配置增量索引更新文件

参考:

  • http://josh-persistence.iteye.com/blog/2017155
  • http://wiki.apache.org/solr/DataImportHandler#Using_delta-import_command
Mongodb
    1. 安装mongo-connector,最好使用手动安装方式:
      git clone https://github.com/10gen-labs/mongo-connector.git
      cd mongo-connector
      #安装前修改mongo_connector/constants.py的变量:设置DEFAULT_COMMIT_INTERVAL = 0
      python setup.py install

      默认是不会自动提交了,这里设置成自动提交,否则mongodb数据库更新,索引这边没法同时更新,或者在命令行中可以指定是否自动提交,不过我现在还没发现。

    2. 配置schema.xml,把mongodb中需要加上索引的字段配置到schema.xml文件中:
      xml version="1.0" encoding="UTF-8" ?> <schema name="example" version="1.5">     <field name="_version_" type="long" indexed="true" stored="true"/>     <field name="_id" type="string" indexed="true" stored="true" required="true" multiValued="false" />      <field name="body" type="string" indexed="true" stored="true"/>     <field name="title" type="string" indexed="true" stored="true" multiValued="true"/>     <field name="text" type="text_general" indexed="true" stored="false" multiValued="true"/>        <uniqueKey>_iduniqueKey>     <defaultSearchField>titledefaultSearchField>     <solrQueryParser defaultOperator="OR"/>      <fieldType name="string" class="solr.StrField" sortMissingLast="true" />     <fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/>     <fieldType name="text_general" class="solr.TextField" positionIncrementGap="100">       <analyzer type="index">         <tokenizer class="solr.StandardTokenizerFactory"/>         <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />         <filter class="solr.LowerCaseFilterFactory"/>       analyzer>       <analyzer type="query">         <tokenizer class="solr.StandardTokenizerFactory"/>         <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />         <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/>         <filter class="solr.LowerCaseFilterFactory"/>       analyzer>     fieldType> schema>
    3. 启动Mongod:
      mongod --replSet myDevReplSet --smallfiles  

      初始化:rs.initiate()

    4. 启动mongo-connector:
      E:\Users\liuzhijun\workspace\mongo-connector\mongo_connector\doc_managers>mongo-connector -m localhost:27017 -t http://localhost:8983/solr/collection2 -n s_soccer.person -u id -d ./solr_doc_manager.py
      • -m:mongod服务
      • -t:solr服务
      • -n:mongodb命名空间,监听database.collection,多个命名空间逗号分隔
      • -u:uniquekey
      • -d:处理文档的manager文件

      注意:mongodb通常使用_id作为uniquekey,而Solrmore使用id作为uniquekey,如果不做处理,索引文件时将会失败,有两种方式来处理这个问题:

      1. 指定参数--unique-key=id到mongo-connector,Mongo Connector 就可以翻译把_id转换到id
      2. 把schema.xml文件中的:
        id

        替换成

        _id

        同时还要定义一个_id的字段:


      3. 启动时如果报错:
        2014-06-18 12:30:36,648 - ERROR - OplogThread: Last entry no longer in oplog cannot recover! Collection(Database(MongoClient('localhost', 27017), u'local'), u'oplog.rs')

        清空E:\Users\liuzhijun\workspace\mongo-connector\mongo_connector\doc_managers\config.txt中的内容,需要删除索引目录下的文件重新启动

    5. 测试
      mongodb中的数据变化都会同步到solr中去。


推荐阅读
  • 本文介绍了使用AJAX的POST请求实现数据修改功能的方法。通过ajax-post技术,可以实现在输入某个id后,通过ajax技术调用post.jsp修改具有该id记录的姓名的值。文章还提到了AJAX的概念和作用,以及使用async参数和open()方法的注意事项。同时强调了不推荐使用async=false的情况,并解释了JavaScript等待服务器响应的机制。 ... [详细]
  • 本文介绍了使用postman进行接口测试的方法,以测试用户管理模块为例。首先需要下载并安装postman,然后创建基本的请求并填写用户名密码进行登录测试。接下来可以进行用户查询和新增的测试。在新增时,可以进行异常测试,包括用户名超长和输入特殊字符的情况。通过测试发现后台没有对参数长度和特殊字符进行检查和过滤。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • Imtryingtofigureoutawaytogeneratetorrentfilesfromabucket,usingtheAWSSDKforGo.我正 ... [详细]
  • 网络请求模块选择——axios框架的基本使用和封装
    本文介绍了选择网络请求模块axios的原因,以及axios框架的基本使用和封装方法。包括发送并发请求的演示,全局配置的设置,创建axios实例的方法,拦截器的使用,以及如何封装和请求响应劫持等内容。 ... [详细]
  • Spring常用注解(绝对经典),全靠这份Java知识点PDF大全
    本文介绍了Spring常用注解和注入bean的注解,包括@Bean、@Autowired、@Inject等,同时提供了一个Java知识点PDF大全的资源链接。其中详细介绍了ColorFactoryBean的使用,以及@Autowired和@Inject的区别和用法。此外,还提到了@Required属性的配置和使用。 ... [详细]
  • Servlet多用户登录时HttpSession会话信息覆盖问题的解决方案
    本文讨论了在Servlet多用户登录时可能出现的HttpSession会话信息覆盖问题,并提供了解决方案。通过分析JSESSIONID的作用机制和编码方式,我们可以得出每个HttpSession对象都是通过客户端发送的唯一JSESSIONID来识别的,因此无需担心会话信息被覆盖的问题。需要注意的是,本文讨论的是多个客户端级别上的多用户登录,而非同一个浏览器级别上的多用户登录。 ... [详细]
  • ShiftLeft:将静态防护与运行时防护结合的持续性安全防护解决方案
    ShiftLeft公司是一家致力于将应用的静态防护和运行时防护与应用开发自动化工作流相结合以提升软件开发生命周期中的安全性的公司。传统的安全防护方式存在误报率高、人工成本高、耗时长等问题,而ShiftLeft提供的持续性安全防护解决方案能够解决这些问题。通过将下一代静态代码分析与应用开发自动化工作流中涉及的安全工具相结合,ShiftLeft帮助企业实现DevSecOps的安全部分,提供高效、准确的安全能力。 ... [详细]
  • MySQL多表数据库操作方法及子查询详解
    本文详细介绍了MySQL数据库的多表操作方法,包括增删改和单表查询,同时还解释了子查询的概念和用法。文章通过示例和步骤说明了如何进行数据的插入、删除和更新操作,以及如何执行单表查询和使用聚合函数进行统计。对于需要对MySQL数据库进行操作的读者来说,本文是一个非常实用的参考资料。 ... [详细]
  • 我将SpringMVC升级到Spring3.2.5.我的一些剩余调用即使存在,也会返回无法识别的字段异常.这是错误.Resolvingexceptionfrom ... [详细]
  • 一、前言在数据库中,慢查询日志通常是用来进行优化数据库,MySQL中存在慢查询,Mongodb中也是如此。在Mongo中的慢查询属于Mon ... [详细]
  • 今天我们学习,数据库mongodb的使用,最下面有mongodb的下载链接。pipinstallpymongo首先安装pymongo,然后在需要用到的地方importpymongo ... [详细]
  •     系统采用jeeplus框架(ssm+redis+shiro+mongodb+redis),默认是做了JSP未做前后端分离,由于业务需要已经多终端使用的需求(H5、小程序等) ... [详细]
  • 前面刚有AWS开战MongoDB,双方“隔空互呛”,这厢又曝出2亿+简历信息泄露——MongoDB的这场开年似乎“充实”得过分了些。长期以来,作为“最受欢迎的NoSQL数据库”,M ... [详细]
  • mongodb match多条件_《MongoDB》
    1.mongodb正则查询2.mongodb聚合查询3.mongodb管道操作4.mongodb字符串操作5.mongodb算术运算6.mongodb日期处理7.Java连击Mon ... [详细]
author-avatar
az旺成电料
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有