在Apache lucene中导入org.apache.lucene.analysis.SimpleAnalyzer符号未找到错误

时间:2013-03-07 12:00:19

标签: java java-ee lucene

大家好,               我试图在我的日志解析器项目中使用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);
    }
}

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

1 个答案:

答案 0 :(得分:1)