Lucene 2.4.0 을 사용 하여 색인+검색 결과 하 이 라이트 만 들 기(1)

3089 단어 luceneJavaApacheCC++
더 읽 기
색인 만 들 기:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;

public class TextFileIndexer
{
    public static void main(String[] args) throws Exception
    {
        /**//*            ,   C  S     */
        File fileDir = new File("c:\\s");

        /**//*            */
        File indexDir = new File("index");
        Analyzer luceneAnalyzer = new StandardAnalyzer();
        
        
        IndexWriter indexWriter = new IndexWriter(indexDir, luceneAnalyzer, true, MaxFieldLength.UNLIMITED);
        File[] textFiles = fileDir.listFiles();
        long startTime = new Date().getTime();

        //   document    
        for (int i = 0; i < textFiles.length; i++)
        {
            if (textFiles[i].isFile() && textFiles[i].getName().endsWith(".txt"))
            {
                System.out.println(" File  " + textFiles[i].getCanonicalPath() + "       . ");
                String temp = FileReaderAll(textFiles[i].getCanonicalPath(), "GBK");
                System.out.println(temp);
                Document document = new Document();
                Field FieldPath = new Field("path", textFiles[i].getPath(), Field.Store.YES, Field.Index.NO);
                Field FieldBody = new Field("body", temp, Field.Store.YES, Field.Index.ANALYZED,
                        Field.TermVector.WITH_POSITIONS_OFFSETS);
                document.add(FieldPath);
                document.add(FieldBody);
                indexWriter.addDocument(document);
            }
        }
        // optimize()          
        indexWriter.optimize();
        indexWriter.close();

        //          
        long endTime = new Date().getTime();
        System.out.println("      " + (endTime - startTime) + "                ! " + fileDir.getPath());
    }

    public static String FileReaderAll(String FileName, String charset) throws IOException
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(FileName), charset));
        String line = new String();
        String temp = new String();

        while ((line = reader.readLine()) != null)
        {
            temp += line;
        }
        reader.close();
        return temp;
    }
}

좋은 웹페이지 즐겨찾기