[编程实例] Lucene Demo 小实例
来源:互联网 发布:世界银行人均gdp数据 编辑:程序博客网 时间:2024/05/18 18:04
首先是文件索引:
先用爬虫爬几个页面放在特定目录下,我挑取了 google,baidu,yahoo 的代码来做例子:)
Writer.java
Searcher.java
以下是一个 DB 索引的例子,大家可以看看:
数据库环境:SQL Server 2005
数据库名称:mydb
数据库表:users
表结构:
Table users
PK id name pass updatetime
DBIndexer.java
DBSearcher.java
先用爬虫爬几个页面放在特定目录下,我挑取了 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());
}
}
{
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());
}
}
}
{
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
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();
}
}
{
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");
}
}
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 小实例
- ofbiz小Demo实例
- EasyUI实例小Demo
- Spring aop 小实例demo
- EasyUI实例小Demo(一)
- EasyUI实例小Demo(二)
- shell编程小实例
- lucene实例
- ViewAnimator实例源码小Demo+Tab例子
- c# 经典小编程实例
- Socket网络编程简单实例Demo,文件传输。
- AOP编程-面向行为编程小实例
- lucene的简单实例
- lucene的简单实例
- lucene 搜索入门实例
- lucene简单实例
- lucene的简单实例
- lucene实例使用
- eqOgre已经被加入到Equalizer的工程库
- win2000/xp中删除oracle服务的方法
- 40种网站设计常用技巧
- C#中TreeView组件使用方法初步
- 序列化的学习!
- [编程实例] Lucene Demo 小实例
- 使用JAVA中的动态代理实现数据库连接池(载自:http://www.ibm.com/developerworks/cn/java/l-connpoolproxy/)
- 在JSP(JSTL)中使用存储过程
- 基于RSA实现面向服务的体系架构
- 经典正则表达式
- 打印WinForm窗体,复制窗体。
- XML与DataSet的相互转换类
- GPU通用可编程技术中的scatter与gather
- 数据库设计方法、规范与技巧