[编程实例] Lucene Demo 小实例
2007-12-12 18:17
429 查看
首先是文件索引:
先用爬虫爬几个页面放在特定目录下,我挑取了 google,baidu,yahoo 的代码来做例子:)
Writer.java
public class Writer
...{
public static void main(String[] args) throws Exception
...{
//fileDir is the directory that contains the text files to be indexed
File fileDir = new File("D:/work/source");
//indexDir is the directory that hosts Lucene's index files
File indexDir = new File("D:/work/index");
Analyzer luceneAnalyzer = new StandardAnalyzer();
IndexWriter indexWriter = new IndexWriter(indexDir,luceneAnalyzer,true);
File[] textFiles = fileDir.listFiles();
System.out.println("Total indexed "" + textFiles.length + "" files ! ");
long startTime = new Date().getTime();
//Add documents to the index
for(int i = 0; i < textFiles.length; i++)...{
if (textFiles[i].isFile() && textFiles[i].getName().endsWith(".htm")) ...{
System.out.println("File " + textFiles[i].getCanonicalPath() + " is being indexed");
Reader textReader = new FileReader(textFiles[i]);
Document document = new Document();
document.add(new Field("content",textReader));
document.add(new Field("path",textFiles[i].getPath(),Field.Store.YES,Field.Index.TOKENIZED));
indexWriter.addDocument(document);
}
}
indexWriter.optimize();
indexWriter.close();
long endTime = new Date().getTime();
System.out.println(" It took " + (endTime - startTime)
+ " milliseconds to create an index for the files in the directory "
+ fileDir.getPath());
}
}
Searcher.java
public class Searcher
...{
public static final String path = "D:/work/index";
public static void main(String[] args) throws Exception
...{
IndexSearcher searcher = new IndexSearcher(path);
Hits hits = null;
Query query = null;
QueryParser qp = new QueryParser("content", new StandardAnalyzer());
String searchText = "yahoo job google baidu";
query = qp.parse(searchText);
hits = searcher.search(query);
System.out.println("Search "" + searchText + "" total " + hits.length() + " result ! ");
for (Iterator it = hits.iterator(); it.hasNext(); ) ...{
Hit hit = (Hit) it.next();
System.out.println(hit.getDocument().getField("path").stringValue());
}
}
}
以下是一个 DB 索引的例子,大家可以看看:
数据库环境:SQL Server 2005
数据库名称:mydb
数据库表:users
表结构:
DBIndexer.java
public class DBIndexer
...{
private String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private String url="jdbc:sqlserver://localhost:1433;databaseName=mydb;";
private String user="sa";
private String pass="123456";
private Connection conn=null;
private Statement st=null;
private ResultSet rs=null;
private String indexUrl="D:/work/index/mydb";
private ResultSet getResult() throws Exception...{
try ...{
Class.forName(driver);
conn = DriverManager.getConnection(url, user, pass);
String sql = "select * from users";
st = conn.createStatement();
rs = st.executeQuery(sql);
// while (rs.next()) {
// System.out.print(rs.getInt("id") + " ");
// System.out.print(rs.getString("name") + " ");
// System.out.print(rs.getString("pass") + " ");
// System.out.print(rs.getDate("updatetime") + " ");
// }
}
catch (Exception e) ...{
e.printStackTrace();
}
return rs;
}
private void executeIndex(ResultSet rs, IndexWriter indexWriter) throws Exception...{
int i=0;
while(rs.next())...{
int id = rs.getInt("id");
String name = rs.getString("name");
String time = rs.getString("updatetime");
Document doc = new Document();
Field idField=new Field("id", Integer.toString(id), Field.Store.YES, Field.Index.NO, Field.TermVector.NO);
Field nameField=new Field("name", name, Field.Store.YES, Field.Index.TOKENIZED, Field.TermVector.YES);
Field timeField=new Field("time", time, Field.Store.YES, Field.Index.TOKENIZED, Field.TermVector.YES);
doc.add(idField);
doc.add(nameField);
doc.add(timeField);
indexWriter.addDocument(doc);
i++;
}
this.close();
System.out.println("共处理记录:"+i);
}
private void close() throws Exception...{
this.rs.close();
this.st.close();
this.conn.close();
}
public void createIndex() throws Exception...{
// get data ResultSet
ResultSet rs=this.getResult();
// Analyzer chineseAnalyzer = new ChineseAnalyzer();
Analyzer chineseAnalyzer = new StandardAnalyzer();
IndexWriter indexWriter = new IndexWriter(this.indexUrl, chineseAnalyzer, true);
indexWriter.setMergeFactor(100);
indexWriter.setMaxBufferedDocs(100);
java.util.Date startDate=new java.util.Date();
System.out.println("开始索引时间:" + startDate);
executeIndex(rs, indexWriter);
indexWriter.optimize();
indexWriter.close();
java.util.Date endDate=new java.util.Date();
System.out.println("索引结束时间:" + endDate);
System.out.println("共花费:" + (endDate.getTime()-startDate.getTime()) + "ms");
}
public static void main (String args[]) throws Exception ...{
DBIndexer oIndexer = new DBIndexer();
oIndexer.createIndex();
}
}
DBSearcher.java
public class DBSearcher ...{
private static final String indexUrl="D:/work/index/mydb";
public static void main(String[] args) throws Exception ...{
/**//*建立索引代码,查找时注释*/
//Index index=new Index();
//index.createIndex();
File indexDir = new File(indexUrl);
FSDirectory fdir = FSDirectory.getDirectory(indexDir);
IndexSearcher searcher = new IndexSearcher(fdir);
//对中文建立解析(必须)
// Analyzer chineseAnalyzer = new ChineseAnalyzer();
Analyzer chineseAnalyzer = new StandardAnalyzer();
QueryParser parser = new QueryParser("name", chineseAnalyzer);
Query query = parser.parse("石头");
Date startDate = new Date();
System.out.println("检索开始时间:"+startDate);
Hits result = searcher.search(query);
for(int i=0; i<result.length(); i++)...{
Document doc = result.doc(i);
System.out.println("用户ID:" + doc.get("id") + " 更新时间:" + doc.get("time"));
}
Date endDate=new Date();
System.out.println("共有记录:" + result.length());
System.out.println("共花费:" + (endDate.getTime()-startDate.getTime()) + "ms");
}
}
先用爬虫爬几个页面放在特定目录下,我挑取了 google,baidu,yahoo 的代码来做例子:)
Writer.java
public class Writer
...{
public static void main(String[] args) throws Exception
...{
//fileDir is the directory that contains the text files to be indexed
File fileDir = new File("D:/work/source");
//indexDir is the directory that hosts Lucene's index files
File indexDir = new File("D:/work/index");
Analyzer luceneAnalyzer = new StandardAnalyzer();
IndexWriter indexWriter = new IndexWriter(indexDir,luceneAnalyzer,true);
File[] textFiles = fileDir.listFiles();
System.out.println("Total indexed "" + textFiles.length + "" files ! ");
long startTime = new Date().getTime();
//Add documents to the index
for(int i = 0; i < textFiles.length; i++)...{
if (textFiles[i].isFile() && textFiles[i].getName().endsWith(".htm")) ...{
System.out.println("File " + textFiles[i].getCanonicalPath() + " is being indexed");
Reader textReader = new FileReader(textFiles[i]);
Document document = new Document();
document.add(new Field("content",textReader));
document.add(new Field("path",textFiles[i].getPath(),Field.Store.YES,Field.Index.TOKENIZED));
indexWriter.addDocument(document);
}
}
indexWriter.optimize();
indexWriter.close();
long endTime = new Date().getTime();
System.out.println(" It took " + (endTime - startTime)
+ " milliseconds to create an index for the files in the directory "
+ fileDir.getPath());
}
}
Searcher.java
public class Searcher
...{
public static final String path = "D:/work/index";
public static void main(String[] args) throws Exception
...{
IndexSearcher searcher = new IndexSearcher(path);
Hits hits = null;
Query query = null;
QueryParser qp = new QueryParser("content", new StandardAnalyzer());
String searchText = "yahoo job google baidu";
query = qp.parse(searchText);
hits = searcher.search(query);
System.out.println("Search "" + searchText + "" total " + hits.length() + " result ! ");
for (Iterator it = hits.iterator(); it.hasNext(); ) ...{
Hit hit = (Hit) it.next();
System.out.println(hit.getDocument().getField("path").stringValue());
}
}
}
以下是一个 DB 索引的例子,大家可以看看:
数据库环境:SQL Server 2005
数据库名称:mydb
数据库表:users
表结构:
Table users | |
PK | id |
name | |
pass | |
updatetime |
public class DBIndexer
...{
private String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private String url="jdbc:sqlserver://localhost:1433;databaseName=mydb;";
private String user="sa";
private String pass="123456";
private Connection conn=null;
private Statement st=null;
private ResultSet rs=null;
private String indexUrl="D:/work/index/mydb";
private ResultSet getResult() throws Exception...{
try ...{
Class.forName(driver);
conn = DriverManager.getConnection(url, user, pass);
String sql = "select * from users";
st = conn.createStatement();
rs = st.executeQuery(sql);
// while (rs.next()) {
// System.out.print(rs.getInt("id") + " ");
// System.out.print(rs.getString("name") + " ");
// System.out.print(rs.getString("pass") + " ");
// System.out.print(rs.getDate("updatetime") + " ");
// }
}
catch (Exception e) ...{
e.printStackTrace();
}
return rs;
}
private void executeIndex(ResultSet rs, IndexWriter indexWriter) throws Exception...{
int i=0;
while(rs.next())...{
int id = rs.getInt("id");
String name = rs.getString("name");
String time = rs.getString("updatetime");
Document doc = new Document();
Field idField=new Field("id", Integer.toString(id), Field.Store.YES, Field.Index.NO, Field.TermVector.NO);
Field nameField=new Field("name", name, Field.Store.YES, Field.Index.TOKENIZED, Field.TermVector.YES);
Field timeField=new Field("time", time, Field.Store.YES, Field.Index.TOKENIZED, Field.TermVector.YES);
doc.add(idField);
doc.add(nameField);
doc.add(timeField);
indexWriter.addDocument(doc);
i++;
}
this.close();
System.out.println("共处理记录:"+i);
}
private void close() throws Exception...{
this.rs.close();
this.st.close();
this.conn.close();
}
public void createIndex() throws Exception...{
// get data ResultSet
ResultSet rs=this.getResult();
// Analyzer chineseAnalyzer = new ChineseAnalyzer();
Analyzer chineseAnalyzer = new StandardAnalyzer();
IndexWriter indexWriter = new IndexWriter(this.indexUrl, chineseAnalyzer, true);
indexWriter.setMergeFactor(100);
indexWriter.setMaxBufferedDocs(100);
java.util.Date startDate=new java.util.Date();
System.out.println("开始索引时间:" + startDate);
executeIndex(rs, indexWriter);
indexWriter.optimize();
indexWriter.close();
java.util.Date endDate=new java.util.Date();
System.out.println("索引结束时间:" + endDate);
System.out.println("共花费:" + (endDate.getTime()-startDate.getTime()) + "ms");
}
public static void main (String args[]) throws Exception ...{
DBIndexer oIndexer = new DBIndexer();
oIndexer.createIndex();
}
}
DBSearcher.java
public class DBSearcher ...{
private static final String indexUrl="D:/work/index/mydb";
public static void main(String[] args) throws Exception ...{
/**//*建立索引代码,查找时注释*/
//Index index=new Index();
//index.createIndex();
File indexDir = new File(indexUrl);
FSDirectory fdir = FSDirectory.getDirectory(indexDir);
IndexSearcher searcher = new IndexSearcher(fdir);
//对中文建立解析(必须)
// Analyzer chineseAnalyzer = new ChineseAnalyzer();
Analyzer chineseAnalyzer = new StandardAnalyzer();
QueryParser parser = new QueryParser("name", chineseAnalyzer);
Query query = parser.parse("石头");
Date startDate = new Date();
System.out.println("检索开始时间:"+startDate);
Hits result = searcher.search(query);
for(int i=0; i<result.length(); i++)...{
Document doc = result.doc(i);
System.out.println("用户ID:" + doc.get("id") + " 更新时间:" + doc.get("time"));
}
Date endDate=new Date();
System.out.println("共有记录:" + result.length());
System.out.println("共花费:" + (endDate.getTime()-startDate.getTime()) + "ms");
}
}
相关文章推荐
- [编程实例] Lucene Demo 小实例
- ASP.NET 4.0 Ajax 实例--DataView模板编程 DEMO 下载
- java Swing编程之布局管理器简单实例DEMO
- lucene2.3.1的demo实例
- Java 网络编程实例
- Linux C Socket编程原理及tcp, udp简单实例
- Hadoop入门(八)自定义类型实例-统计手机流量数据Demo
- 【Qt5开发及实例】29、实现服务器端的编程,UDP协议
- 网络游戏demo开发实例:多人在线RPG游戏(MMO RPG)demo的开发记录(第1篇)
- Javascript & DHTML 实例编程(教程)(三)初级实例篇1―上传文件控件实例
- VC++之网络编程五 聊天编程实例
- Bash 实例,第一部分 Bourne again shell (bash) 基本编程
- java分页实例Demo
- 按钮控件的编程实例
- 量化交易----编程实例:爬取标普500指数股票数据
- mapreduce编程实例(7)-求所有用户ID
- Linux文件编程实例
- loadrunner winsocket 编程实例
- socket编程实例
- 面向接口编程详解(二)——编程实例(1)