全文检索引擎Solr系列——整合MySQL、MongoDB
MySQL
- 拷贝mysql-connector-java-5.1.25-bin.jar到E:\solr-4.8.0\example\solr-webapp\webapp\WEB-INF\lib目录下面
- 配置E:\solr-4.8.0\example\solr\collection1\conf\solrconfig.xml
|
1
2
3
4
5
6
|
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"> <lst name="defaults"> <str name="config">data-config.xml</str> </lst> </requestHandler> |
- 导入依赖库文件:
<pre class="brush: xml; gutter: false; first-line: 1; highlight: []; html-script: false"><lib dir="../../../dist/" regex="solr-dataimporthandler-\d.*\.jar"/></pre>
加在
<pre class="brush: xml; gutter: false; first-line: 1; highlight: []; html-script: false"> <lib dir="../../../dist/" regex="solr-cell-\d.*\.jar" /></pre>
前面。
- 创建E:\solr-4.8.0\example\solr\collection1\conf\data-config.xml,指定MySQL数据库地址,用户名、密码以及建立索引的数据表
<?xmlversion="1.0"encoding="UTF-8"?><dataConfig><dataSourcetype="JdbcDataSource"driver="com.mysql.jdbc.Driver"url="jdbc:mysql://localhost:3306/django_blog"user="root"password=""/><documentname="blog"><entityname="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"><fieldcolumn="id"name="id"/><fieldcolumn="title"name="title"/><fieldcolumn="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
- query 用于初次导入到索引的sql语句。
为数据库表字段建立域(field),编辑E:\solr-4.8.0\example\solr\collection1\conf\schema.xml:
<!-- mysql --> <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"/> <!-- mysql --> |
- 配置增量索引更新文件
参考:
- http://josh-persistence.iteye.com/blog/2017155
- http://wiki.apache.org/solr/DataImportHandler#Using_delta-import_command
Mongodb
- 安装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数据库更新,索引这边没法同时更新,或者在命令行中可以指定是否自动提交,不过我现在还没发现。
- 配置schema.xml,把mongodb中需要加上索引的字段配置到schema.xml文件中:
<?xmlversion="1.0"encoding="UTF-8"?><schemaname="example"version="1.5"><fieldname="_version_"type="long"indexed="true"stored="true"/><fieldname="_id"type="string"indexed="true"stored="true"required="true"multiValued="false"/><fieldname="body"type="string"indexed="true"stored="true"/><fieldname="title"type="string"indexed="true"stored="true"multiValued="true"/><fieldname="text"type="text_general"indexed="true"stored="false"multiValued="true"/><uniqueKey>_id</uniqueKey><defaultSearchField>title</defaultSearchField><solrQueryParserdefaultOperator="OR"/><fieldTypename="string"class="solr.StrField"sortMissingLast="true"/><fieldTypename="long"class="solr.TrieLongField"precisionStep="0"positionIncrementGap="0"/><fieldTypename="text_general"class="solr.TextField"positionIncrementGap="100"><analyzertype="index"><tokenizerclass="solr.StandardTokenizerFactory"/><filterclass="solr.StopFilterFactory"ignoreCase="true"words="stopwords.txt"/><filterclass="solr.LowerCaseFilterFactory"/></analyzer><analyzertype="query"><tokenizerclass="solr.StandardTokenizerFactory"/><filterclass="solr.StopFilterFactory"ignoreCase="true"words="stopwords.txt"/><filterclass="solr.SynonymFilterFactory"synonyms="synonyms.txt"ignoreCase="true"expand="true"/><filterclass="solr.LowerCaseFilterFactory"/></analyzer></fieldType></schema> - 启动Mongod:
mongod --replSet myDevReplSet --smallfiles
初始化:rs.initiate()
- 启动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,如果不做处理,索引文件时将会失败,有两种方式来处理这个问题:- 指定参数
--unique-key=id到mongo-connector,Mongo Connector 就可以翻译把_id转换到id。 - 把schema.xml文件中的:
<uniqueKey>id<uniqueKey>
替换成
<uniqueKey>_id</uniqueKey>
同时还要定义一个
_id的字段:<field name="_id" type="string" indexed="true" stored="true" />
- 启动时如果报错:
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中的内容,需要删除索引目录下的文件重新启动
测试
mongodb中的数据变化都会同步到solr中去。
全文检索引擎Solr系列——整合MySQL、MongoDB的更多相关文章
- 全文检索引擎Solr系列——整合中文分词组件mmseg4j
默认Solr提供的分词组件对中文的支持是不友好的,比如:“VIM比作是编辑器之神”这个句子在索引的的时候,选择FieldType为”text_general”作为分词依据时,分词效果是: 它把每一个词 ...
- 全文检索引擎Solr系列——整合中文分词组件IKAnalyzer
IK Analyzer是一款结合了词典和文法分析算法的中文分词组件,基于字符串匹配,支持用户词典扩展定义,支持细粒度和智能切分,比如: 张三说的确实在理 智能分词的结果是: 张三 | 说的 | 确实 ...
- [摘]全文检索引擎Solr系列—–全文检索基本原理
原文链接--http://www.importnew.com/12707.html 全文检索引擎Solr系列—–全文检索基本原理 2014/08/18 | 分类: 基础技术, 教程 | 2 条评论 | ...
- 全文检索引擎Solr系列—–全文检索基本原理
场景:小时候我们都使用过新华字典,妈妈叫你翻开第38页,找到“坑爹”所在的位置,此时你会怎么查呢?毫无疑问,你的眼睛会从38页的第一个字开始从头至尾地扫描,直到找到“坑爹”二字为止.这种搜索方法叫做顺 ...
- 全文检索引擎Solr系列——Solr核心概念、配置文件
Document Document是Solr索引(动词,indexing)和搜索的最基本单元,它类似于关系数据库表中的一条记录,可以包含一个或多个字段(Field),每个字段包含一个name和文本值. ...
- 全文检索引擎Solr系列——solr入门
下载4.8.0版本,下载地址:http://archive.apache.org/dist/lucene/solr/4.8.0/ 解压后,得到文件夹视图如下: 解压缩solr,在example目录有s ...
- 全文检索引擎 Solr 部署与基本原理
全文检索引擎 Solr 部署与基本原理 搜索引擎Solr环境搭建实例 关于 solr , schema.xml 的配置说明 全文检索引擎Solr系列-–全文检索基本原理 一.搜索引擎Solr环境搭建实 ...
- 全文检索引擎Solr 指南
全文检索引擎Solr系列:第一篇:http://t.cn/RP004gl.第二篇:http://t.cn/RPHDjk7 .第三篇:http://t.cn/RPuJt3T
- 全文检索引擎Solr的配置
描述: 在Linux环境下实现高速的全文检索 一.当前环境: CentOS (Linux) 6.3 64 bit 二.所需软件 1.Java的JDK Java jdk 1.7.0[注意:solr5.x ...
随机推荐
- JAVA线程池的分析和使用
1. 引言 合理利用线程池能够带来三个好处.第一:降低资源消耗.通过重复利用已创建的线程降低线程创建和销毁造成的消耗.第二:提高响应速度.当任务到达时,任务可以不需要等到线程创建就能立即执行.第三:提 ...
- 用DOS命令配置服务开机自启动
2016-08-19 15:01 Create 使用命令 sc config 参考博客:http://blog.csdn.net/it1988888/article/details/7992626 ...
- Gated Recurrent Unit (GRU)公式简介
update gate $z_t$: defines how much of the previous memory to keep around. \[z_t = \sigma ( W^z x_t+ ...
- HTML5学习总结-10 Android 控件WebView显示网页
WebView可以使得网页轻松的内嵌到app里,还可以直接跟js相互调用. webview有两个方法:setWebChromeClient 和 setWebClient 1)setWebClient: ...
- photoshop工具使用的简单介绍
photoshop工具使用的简单介绍 我所用PhotoShop版本号是cs6,这里对其主要功能做一个简单介绍. 第一部分: 首先,ps的界面主要分为了6部分: 一.最上面的一行的菜单栏,菜单中有:文件 ...
- IOS VFL屏幕自适应
-(void)fun1{ //注意使用VFL,不用设置视图的frame UIView *view = [[UIView alloc] init]; view.backgroundColor = [UI ...
- wpf 前台获取资源文件路径问题
1 <ImageBrush ImageSource="YT.CM.CommonUI;component/Resource/FloadwindowImage/middle.png&quo ...
- 9.25 DOM作业
一<style type="text/css">*{margin:0px auto; padding:0px; font-family:微软雅黑; font-size: ...
- UILabel UISwitch UISegmentedControl UIAlertView
基础小控件 /***************************************UIlabel*************************************/ UILabel ...
- Java基本语法笔记
1. 基本格式: public class HelloWprdApp { public static void main(String[] args) { // TODO Auto-ge ...