课外天地 李树青学习天地信息检索原理课件 → [推荐]一个最简单的Lucene程序


  共有21854人关注过本帖树形打印复制链接

主题:[推荐]一个最简单的Lucene程序

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1940 积分:26616 威望:0 精华:34 注册:2003/12/30 16:34:32
[推荐]一个最简单的Lucene程序  发帖心情 Post By:2008/5/14 10:52:10 [只看该作者]

1、先到网上下载lucene的jar包

 

2、设置classpath或者将其放入JDK/jre/lib/ext目录自动发现

 

3、索引程序功能是将“C:\\j2sdk1.4.2_14\\docs”目录中的所有文本文件建立索引,并将索引存放在“C:\\temp”,这些目录皆可以自己定义:
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;

 

import java.io.File;
import java.io.IOException;
import java.io.FileReader;
import java.util.Date;

 

/**
* This code was originally written for
* Erik's Lucene intro java.net article
*/
public class Indexer
{

 

    public static void main(String[] args) throws Exception
    {
        File indexDir = new File("C:\\temp"); //Create Lucene index in this directory
        File dataDir = new File("C:\\j2sdk1.4.2_14\\docs"); //Index files in this directory

 

        long start = new Date().getTime();
        int numIndexed = index(indexDir, dataDir);
        long end = new Date().getTime();

 

        System.out.println("Indexing " + numIndexed + " files took "
                + (end - start) + " milliseconds");
    }

 

    public static int index(File indexDir, File dataDir) throws IOException
    {
        IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(),
                true);
        //indexDir表示索引的存放地点
        //new StandardAnalyzer()表示分析器
        //true表示如已有索引则覆盖之

 

        writer.setUseCompoundFile(false);
        //表示生成多文件索引文件(默认为复合索引)
       
        indexDirectory(writer, dataDir);
        int numIndexed = writer.docCount();
        writer.optimize();
        writer.close();
        return numIndexed;
    }

 

    private static void indexDirectory(IndexWriter writer, File dir)
            throws IOException
    {
        File[] files = dir.listFiles();

 

        for (int i = 0; i < files.length; i++)
        {
            File f = files;
            if (f.isDirectory())
            {
                indexDirectory(writer, f); // recurse
            }
            else if (f.getName().endsWith(".txt"))
            {
                indexFile(writer, f);
            }
        }
    }

 

    private static void indexFile(IndexWriter writer, File f)
            throws IOException
    {

 

        if (f.isHidden() || !f.exists() || !f.canRead())
        {
            return;
        }

 

        System.out.println("Indexing " + f.getCanonicalPath());
       
        //建立索引关键代码
        Document doc = new Document();
        doc.add(Field.Text("contents", new FileReader(f)));
        doc.add(Field.Keyword("filename", f.getCanonicalPath()));
        writer.addDocument(doc);
    }
}

 

4、搜索程序为:
import org.apache.lucene.document.Document;
import org.apache.lucene.search.*;
import org.apache.lucene.index.*;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.Directory;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import java.io.File;
import java.util.Date;

 

public class SearchExec
{

 

    public static void main(String[] args) throws Exception
    {
        File indexDir = new File("c:\\temp");

 

        Directory fsDir = FSDirectory.getDirectory(indexDir, false);//if true, create, or erase any existing contents
       
        //打开索引
        IndexSearcher is = new IndexSearcher(fsDir);
       
        //解析查询词语
        //Query query = new TermQuery(new Term("contents", "java"));
        //Query query = new TermQuery(new Term("contents", "application"));
        Query query = QueryParser.parse("java AND NOT application", "contents", new StandardAnalyzer());
       
       
        //搜索索引
        Hits hits = is.search(query);
       
        for (int i = 0; i < hits.length(); i++)
        {
            Document doc = hits.doc(i);
            System.out.println(doc.get("filename"));
        }
    }
}

[此贴子已经被作者于2010-12-14 09:27:29编辑过]

 回到顶部