设为首页 加入收藏

TOP

mongodb全文搜索解决方案(lucene+IKAnalyzer)(一)
2014-11-24 08:09:31 来源: 作者: 【 】 浏览:11
Tags:mongodb 全文 搜索 解决方案 lucene IKAnalyzer

mongodb全文搜索解决方案(lucene+IKAnalyzer)
mongodb 解决 全文搜索是个不小的问题
可以用 正则匹配 但是效率很低 往往到大数据量的搜索的时候就会出现 查询超时等现象
当然也可以用官方的做法(在mongodb的文档类型中加字段,存分词结果,
然后从该字段中匹配) 但是我尝试了 效率比原先的好像还要低
www.2cto.com
http://www.oschina.net/question/200745_61968
后来我尝试了 lucene+IKAnalyzer 发现效率有所提升啊
原理:lucene 把大文本的数据 利用分词器 在新建的索引文件中建立索引
取数据的时候从索引文件中取
取出mongodb 中的数据进行 索引的创建
01
package sample3;
02
03
import java.io.File;
04
05
import org.apache.lucene.analysis.Analyzer;
06 www.2cto.com
import org.apache.lucene.document.Document;
07
import org.apache.lucene.document.Field;
08
import org.apache.lucene.index.IndexWriter;
09
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
10
import org.apache.lucene.store.Directory;
11
import org.apache.lucene.store.FSDirectory;
12
import org.wltea.analyzer.lucene.IKAnalyzer;
13
14
import com.mongodb.DB;
15
import com.mongodb.DBCollection;
16
import com.mongodb.DBCursor;
17
import com.mongodb.Mongo;
18
19
/**
20
* 创建索引
21
* @author zhanghaijun www.2cto.com
22
*
23
*/
24
public class Demo1 {
25
public static void main(String[] args) throws Exception {
26
//先在 数据库中拿到要创建索引的数据
27
Mongo mongo = new Mongo();
28
DB db = mongo.getDB("zhang");
29
DBCollection msg = db.getCollection("test3");
30
DBCursor cursor = msg.find();
31
//是否重新创建索引文件,false:在原有的基础上追加
32
boolean create = true;
33
//创建索引
34
Directory directory = FSDirectory.open(new File("E:\\lucene\\index"));
35 www.2cto.com
Analyzer analyzer = new IKAnalyzer();//IK中文分词器
36
IndexWriter indexWriter = new IndexWriter(directory,analyzer,MaxFieldLength.LIMITED);
37
boolean exist = cursor.hasNext();
38
while(exist){
39
//System.out.println(cursor.next().get("text").toString());
40
Document doc = new Document();
41
Field fieldText = new Field("text",cursor.next().get("text").toString(),Field.Store.YES,
42
Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS);
43
doc.add(fieldText);
44
indexWriter.addDocument(doc);
45
exist = cursor.hasNext();
46
}
47
cursor = null;
48
//optimize()方法是对索引进行优化
49
indexWriter.optimize();
50
//最后关闭索引
51
indexWriter.close();
52 www.2cto.com
}
53
}
数据的查找(直接从索引文件中查找)
01
package sample3;
02
03
import java.io.File;
04
05
import org.apache.lucene.document.Document;
06
import org.apache.lucene.index.IndexReader;
07
import org.apache.lucene.search.IndexSearcher;
08
import org.apache.lucene.search.Query;
09
import org.apache.lucene.search.ScoreDoc;
10
import org.apache.lucene.search.TopDocs;
11
import org.apache.lucene.store.FSDirectory;
12
import org.wltea.analyzer.lucene.IKAnalyzer;
13
import org.wltea.analyzer.lucene.IKQueryParser;
14
import org.wltea.analyzer.lucene.IKSimilarity;
15 www.2cto.com
16
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Sqlite的简单介绍和应用 下一篇Oracle私房菜之数据库基础交流

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C/C++ 类模板与模板 (2025-12-27 01:49:52)
·C语言 模板化<templ (2025-12-27 01:49:49)
·C/C++模板类模板与函 (2025-12-27 01:49:46)
·如何理解c语言指针和 (2025-12-27 01:19:11)
·为什么C标准库没有链 (2025-12-27 01:19:08)