全文检索引擎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数据库地址,用户名、密码以及建立索引的数据表
<?
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
- 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文件中:
<?
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
>_id</
uniqueKey
>
<
defaultSearchField
>title</
defaultSearchField
>
<
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
>
- 启动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 ...
随机推荐
- Beta版本冲刺第二天 12.6
一.站立式会议照片: 二.项目燃尽图: 三.项目进展: 成 员 昨天完成任务 今天完成任务 第三天冲刺要做任务 问题困难 心得体会 胡泽善 完成了"记住密码"的的逻辑以及BUG修改 ...
- HD1556Color the ball(树状数组)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- netty 解决TCP粘包与拆包问题(一)
1.什么是TCP粘包与拆包 首先TCP是一个"流"协议,犹如河中水一样连成一片,没有严格的分界线.当我们在发送数据的时候就会出现多发送与少发送问题,也就是TCP粘包与拆包.得不到我 ...
- C#验证子网掩码的正确性
1. IP合法关于IP地址的合法性验证很简单,方法也很多,比如字符串分解.正则表达式等. 2. 子网掩码是否合法简单来讲,子网掩码就类似这样一串数字,前面一段是连续的1, 类似 ...
- BigDecimal类
如果需要精确的计算结果,则必须使用BigDecimal类,而且使用BigDecimal类也可以进行大数的操作. //========================================== ...
- ubuntu sublime text3 lisence
help --> enter lisence ----- BEGIN LICENSE ----- Andrew Weber Single User License EA7E-855605 813 ...
- B1/B2签证拒签
http://www.mcdvisa.com/html/News/USA_visa_news/201529/152917GE.html
- json_encode详解,转义
1.json_encod基本用法:数组转字符串 <?php $arr = array (,,,,); echo json_encode($arr); ?> 以上例程会输出: {,,,,} ...
- 设计模式学习——策略模式(Strategy Pattern)
0. 前言 最近在重构公司的一个项目的时候,在抽取DES加密重复部分代码的时候,突然间想起了策略模式,感觉策略模式好像可以应用上,于是重新学习了下策略模式.注:在DES加密中,有DES和TDES算法, ...
- Django笔记-字符编码相关问题整理
1.添加中文注释后编译出错,提示:Non-ASCII 解决方法: 在Python脚本文件的第一行或第二行添加一句: #coding:gbk或#coding:utf-8或##-*- cod ...