package com.sea.index; import java.io.File; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Date; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.LockObtainFailedException; import org.apache.lucene.store.RAMDirectory; import org.apache.lucene.util.Version; public class DataBaseIndex { /** * @param args * @throws SQLException * @throws IOException * @throws LockObtainFailedException * @throws CorruptIndexException * @throws ParseException */ public static void main(String[] args) throws CorruptIndexException, LockObtainFailedException, IOException, SQLException, ParseException { // TODO Auto-generated method stub String url = "jdbc:oracle:thin:@10.1.80.170:1521:NETCOOL"; String user = "uum"; String pwd = "uum"; DataBaseIndex test = new DataBaseIndex(); test.makeIndexForDatabase(test.getConnection(url, user, pwd)); test.findSM("P55A"); } public void findSM(String str) throws IOException, ParseException { File file = new File("d:\\data"); FSDirectory directory = FSDirectory.open(file); IndexReader reader = IndexReader.open(directory); IndexSearcher is = new IndexSearcher(reader); QueryParser query = new QueryParser(Version.LUCENE_36, "NAME", new StandardAnalyzer(Version.LUCENE_36)); Query q1 = query.parse(str); ScoreDoc[] docs = is.search(q1, is.maxDoc()).scoreDocs; for (int i = 0; i < docs.length; i++) { Date start = new Date(); String ip = is.doc(docs[i].doc).get("IP"); String name = is.doc(docs[i].doc).get("NAME"); String id = is.doc(docs[i].doc).get("ID"); Date end = new Date(); System.out.println("===========" + id + " : " +name+" : "+ip+" "+(end.getTime() - start.getTime()) + "ms"); } } public Connection getConnection(String url, String user, String password) { try { Class.forName("oracle.jdbc.OracleDriver"); return DriverManager.getConnection(url, user, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return null; } public ResultSet executeSQL(Connection conn, String sql) { try { Statement smt = conn.createStatement(); return smt.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return null; } public void makeIndexForDatabase(Connection conn) throws CorruptIndexException, LockObtainFailedException, IOException, SQLException { convert(executeSQL(conn, "select serial,name,new_netip from ams_asset_tab where new_netip is not null")); } public void convert(ResultSet rs) throws SQLException, CorruptIndexException, LockObtainFailedException, IOException { while (rs.next()) { File file = new File("d:\\data"); FSDirectory directory = FSDirectory.open(file); IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_36, new StandardAnalyzer(Version.LUCENE_36)); IndexWriter writer = new IndexWriter(directory, conf); Document doc = new Document(); String serial1 = rs.getString("serial"); String name1 = rs.getString("name"); String ip1 = rs.getString("new_netip"); System.out.println("serial:" + serial1 + "\t" + "name:" + name1 + "\t" + "ip:" + ip1); Field serial = new Field("ID", serial1, Field.Store.YES, Field.Index.ANALYZED); doc.add(serial); Field name = new Field("NAME", name1, Field.Store.YES, Field.Index.ANALYZED); doc.add(name); Field ip = new Field("IP", ip1, Field.Store.YES, Field.Index.ANALYZED); doc.add(ip); writer.addDocument(doc); writer.close(); } } }
相关推荐
Luence.Net搜索Demo
用来测试luence运行的结果是否正确。运行直接使用即可。
使用luence索引数据库中的记录,完整的例子
Luence和ElasticSearch面试准备.docx
完整的IKanalyzer2012和luence4.3.0、luence3.6.1的压缩包,内含有IKAnalyzer2012的说明文档
获取全部Luence数据
lucene 原理与分析,底层源码解析,应用场景及实践,相关配置
Luence in Action 中英文 pdf
luence4.6 源代码
利用Luence.net做的搜索,实现搜索高亮,盘古分词,只能匹配,是做搜索的好例子。
下载可运行。 使用前后端分离架构, 使用springMvc 、 spring、 mybatis、 luence等技术开发的开源知识库所有后台代码
首先读取文件,添加数据库记录,然后lucene读取数据库生成索引文件,再通过lucene检索
lucene与sqlserver数据库实现索引的简单实例lucene与sqlserver数据库实现索引的简单实例
基于ajax与luence构建搜索引擎的设计和实现论文.doc
VerticalSearcher基本的垂直搜索引擎,实现了基本的网络爬虫功能以及用Luence实现检索
基于ajax与luence构建搜索引擎的设计和实现_论文.doc
Lucene(这里用到的是Lucene.net版本也成为DotLucene)是一个信息检索的函数库(Library),利用它你可以为你的应用加上索引和搜索的功能. Lucene的使用者不需要深入了解有关全文检索的知识,仅仅学会使用库中的一个类,...
stopword+dic 分词器分析
基本的垂直搜索引擎,实现了基本的网络爬虫功能以及用Luence实现检索
前后端分离项目使用的spring Mvc、 mybatis、 luence、 spring 开发的开源知识库系统的所有后端端源码。 下载可运行。 使用前后端分离架构, 使用springMvc 、 spring、 mybatis、 luence等技术开发的开源知识库所有...