lucene学习记录(1) - 初识神器
2016-07-01 17:35
253 查看
lucene 6.1.0
在内存中建立索引, 90W条数据占用约1.5G内存
刚开始接触, 很多接口和参数还不了解, 若有错误请指出.
在内存中建立索引, 90W条数据占用约1.5G内存
public class AppInst { private static AppInst ourInstance = new AppInst(); public static AppInst getInstance() { return ourInstance; } private AppInst() { } public static void main(String argv[]) throws Exception { AppInst.getInstance().main(); } public void main() throws Exception { RAMDirectory ram = new RAMDirectory(); IndexWriter writer = new IndexWriter(ram, new IndexWriterConfig(new StandardAnalyzer())); createIndex(writer); writer.close(); while (true) { search(ram); Thread.sleep(1000); } } void createIndex(IndexWriter writer) throws Exception { Connection conn = Proxool.getInstance().getConnection(); try { PreparedStatement stmt = conn.prepareStatement("select * from items_bak"); if (stmt.execute()) { ResultSet rs = stmt.getResultSet(); int nCount = 0; while (rs.next()) { int id = rs.getInt("id"); String detail = rs.getString("detail"); createIndexEx(writer, id, detail); nCount++; } System.out.println(String.format("共%d条数据", nCount)); } } finally { conn.close(); } } void createIndexEx(IndexWriter writer, int id, String detail) throws IOException { Document doc = new Document(); doc.add(new Field("id", Integer.toString(id), TextField.TYPE_STORED)); doc.add(new Field("detail", detail, TextField.TYPE_STORED)); writer.addDocument(doc); } void search(Directory dir) throws IOException, ParseException { IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(dir)); QueryParser parser = new QueryParser("detail", new StandardAnalyzer()); long t1 = System.currentTimeMillis(); TopDocs rs = searcher.search(parser.parse("北京"), 100); long t2 = System.currentTimeMillis(); System.out.println("耗时: " + (t2 - t1)); System.out.println(String.format("找到%d条匹配数据", rs.totalHits)); for (ScoreDoc sd : rs.scoreDocs) { Document doc = searcher.doc(sd.doc); String detail = doc.getField("detail").stringValue(); //System.out.println(detail); } } }
共907984条数据 耗时: 53 找到226921条匹配数据
刚开始接触, 很多接口和参数还不了解, 若有错误请指出.
相关文章推荐
- 互联网协议入门
- XEN server创建虚拟机读写速度慢
- Android Studio 快捷键
- [置顶] Faster-RCNN+ZF用自己的数据集训练模型(Matlab版本)
- php相对路径的使用问题
- Python之父谈Python的未来形式
- 1009. Product of Polynomials (25)
- 软件测试实习0701-ant简介
- doracms代码学习一
- redis - Java操作
- 数组循环移位问题
- 图片放大镜效果
- 音频录制上传服务器,调用系统相机
- spring security3 实现踢出在线用户
- linux考题
- 【杭电-oj】-1231-最大连续子序列
- 集合中的子集2(含有重复元素)
- 【转】ORACLE 内置函数之 GREATEST 和 LEAST(求多列的最大值,最小值)
- OPENWRT 打印log消息到logread
- 生成1-N的全排列