Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1085059
  • 博文数量: 252
  • 博客积分: 4561
  • 博客等级: 上校
  • 技术积分: 2833
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-15 08:23
文章分类

全部博文(252)

文章存档

2015年(2)

2014年(1)

2013年(1)

2012年(16)

2011年(42)

2010年(67)

2009年(87)

2008年(36)

分类:

2009-07-15 14:17:37

import org.apache.lucene.index.*;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import java.io.FileReader;
import java.io.File;
import java.io.Reader;
import java.util.Date;

class Indexer
{
        public static void main(String args[]) throws Exception
        {
                if (args.length != 2)
                {
                        throw new Exception("Usage: java " + Indexer.class.getName()
                        + " ");
                }

                File indexDir = new File(args[0]);
                File dataDir = new File(args[1]);

                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 Exception
        {
                if (!dataDir.exists() || !dataDir.isDirectory())
                {
                        System.out.println("dataDir is not exist or not a directory");
                        return -1;
                }

                IndexWriter writer = new IndexWriter(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 Exception
        {
                File[] files = dir.listFiles();
                for (int i = 0; i < files.length; i++)
                {
                        File f = files[i];
                        if (f.isDirectory())
                                indexDirectory(writer, f); // hell: recursive

                        else if (f.getName().endsWith(".txt"))
                                indexFile(writer, f);
                }
        }

        private static void indexFile(IndexWriter writer, File f) throws Exception
        {
                if (f.isHidden() || !f.exists() || !f.canRead())
                        return;

                System.out.println("Index: " + f.getCanonicalPath());

                Document doc = new Document();
                doc.add(new Field("contents", new FileReader(f)));
                doc.add(new Field("filename", f.getCanonicalPath(), Field.Store.YES, Field.Index.UN_TOKENIZED));
                writer.addDocument(doc);
        }
}

import java.io.File;
import org.apache.lucene.index.*;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import java.io.FileReader;
import java.io.File;
import java.io.Reader;
import java.util.Date;
import org.apache.lucene.store.Directory;
import org.apache.lucene.search.Query;
import org.apache.lucene.queryParser.QueryParser;

import org.apache.lucene.document.Document;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.FSDirectory;

public class Searcher
{
    public static void main(String args[]) throws Exception
    {
        if (args.length != 2)
        {
            System.out.println("Usage: java Search ");
            return;
        }
        
        File indexDir = new File(args[0]);
        String q = args[1];
        
        if (!indexDir.exists() || !indexDir.isDirectory())
        {
            System.out.println("directory file error");
            return;
        }
        
        search(indexDir, q);
    }
    
    public static void search(File indexDir, String q) throws Exception
    {
        Directory fsDir = FSDirectory.getDirectory(indexDir, false);
        IndexSearcher is = new IndexSearcher(fsDir);
        
        QueryParser parser = new QueryParser("contents", new StandardAnalyzer());
        Query query = parser.parse(q);
        
        long start = new Date().getTime();
        Hits hits = is.search(query);
        long end = new Date().getTime();
        
        System.out.println("Found: " + hits.length());
        
        for (int i = 0; i < hits.length(); i++)
        {
            Document doc = hits.doc(i);
            System.out.println(doc.get("filename"));
        }
    }
}



阅读(793) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~