0

大家好,我正在尝试在我的日志解析器项目中使用 Apache Lucene。现在我使用了下面的代码,但我无法导入 org.apache.lucene.analysis.SimpleAnalyzer 我已经包含了 lucene4.1 jar

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package logsearchengine;

import java.io.File;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.FSDirectory;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.FSDirectory;

/**
 *
 * @author maclean
 */
public class LogSearchEngine {


    private int index(File indexDir, File dataDir, String suffix) throws Exception {

        IndexWriter indexWriter = new IndexWriter(FSDirectory.open(indexDir),  new SimpleAnalyzer(),true,IndexWriter.MaxFieldLength.LIMITED);
        indexWriter.setUseCompoundFile(false);

        indexDirectory(indexWriter, dataDir, suffix);

        int numIndexed = indexWriter.maxDoc();
        indexWriter.optimize();
        indexWriter.close();

        return numIndexed;

    }

    private void indexDirectory(IndexWriter indexWriter, File dataDir, 
           String suffix) throws IOException {

        File[] files = dataDir.listFiles();
        for (int i = 0; i < files.length; i++) {
            File f = files[i];
            if (f.isDirectory()) {
                indexDirectory(indexWriter, f, suffix);
            }
            else {
                indexFileWithIndexWriter(indexWriter, f, suffix);
            }
        }

    }

    private void indexFileWithIndexWriter(IndexWriter indexWriter, File f, 
            String suffix) throws IOException {

        if (f.isHidden() || f.isDirectory() || !f.canRead() || !f.exists()) {
            return;
        }
        if (suffix!=null && !f.getName().endsWith(suffix)) {
            return;
        }
        System.out.println("Indexing file " + 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.ANALYZED));

        indexWriter.addDocument(doc);

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
         File indexDir = new File("C:/index/");
        File dataDir = new File("C:/programs/eclipse/workspace/");
        String suffix = "java";

        SimpleFileIndexer indexer = new SimpleFileIndexer();

        int numIndex = indexer.index(indexDir, dataDir, suffix);

        System.out.println("Total files indexed " + numIndex);
    }
}

我只添加了一个 jar 文件,即 lucene-core-4.1.0.jar。我无法导入样本分析器因此我无法创建索引编写器。我错过了什么。我是 lucene 的新手

4

1 回答 1

1

您还需要lucene-analyzers-common-4.1.0.jar

于 2013-03-07T12:35:37.030 回答