[编程实例] Lucene Demo 小实例

来源:互联网 发布:世界银行人均gdp数据 编辑:程序博客网 时间:2024/05/18 18:04
首先是文件索引:

先用爬虫爬几个页面放在特定目录下,我挑取了 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
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");
    }


}