线程“main”中的异常java.lang.Error LUCENE

时间:2016-03-21 09:22:47

标签: java sql-server apache solr lucene

您好我正在使用Lucene索引我的数据库记录,但我无法解决此错误。

错误:

  

线程“main”中的异常java.lang.Error:未解决的编译问题:       目录无法解析为某种类型       FSDirectory类型中的open(Path)方法不适用于参数(File)   

      在lucene.Lucenetest.main(Lucenetest.java:32)

CODE:

    package lucene;

    import java.io.File;
    import java.nio.file.FileSystems;
    import java.nio.file.Path;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;

    import org.apache.lucene.analysis.core.SimpleAnalyzer;
    import org.apache.lucene.analysis.standard.StandardAnalyzer;
    import org.apache.lucene.document.DateTools;
    import org.apache.lucene.document.DateTools.Resolution;
    import org.apache.lucene.document.Document;
    import org.apache.lucene.document.Field;
    import org.apache.lucene.document.StringField;
    import org.apache.lucene.index.IndexWriter;
    import org.apache.lucene.index.IndexWriterConfig;
    import org.apache.lucene.store.FSDirectory;
    import org.apache.lucene.util.Version;
    public class Lucenetest {
//database connection
    public static final String PATH = "C:/dbindex/index.txt";
    private static final String JDBC_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private static final String CONNECTION_URL = "jjdbc:sqlserver://WMDENTW1\\SQLEXPRESS:1433;" + 
                        "database=FullTextDB;" + 
                        "user=root;" + 
                        "password=root123";
    private static final String QUERY = "select FTID, ID, CLASSID, TEXT, PUBNOTICECONTENT, DOCUMENTCONTENT, contentSum_DE from METADATA_FULLTEXT";
    public static void main(String[] args) throws Exception {
    Lucenetest indexer = new Lucenetest();

//error here

    ***Directory indexDir = FSDirectory.open(new File(PATH));***

    try{  
//index writer
       Class.forName(JDBC_DRIVER).newInstance();  

       Connection conn = DriverManager.getConnection(CONNECTION_URL); 

       StandardAnalyzer analyzer = new StandardAnalyzer();  

       IndexWriterConfig Config = new IndexWriterConfig(analyzer);

       IndexWriter indexWriter = new IndexWriter(indexDir, Config);  

       System.out.println("Indexing to directory '" + indexDir + "'...");  

       int indexedDocumentCount = indexer.indexDocs1(indexWriter, conn);  

       indexWriter.close();  

       System.out.println(indexedDocumentCount + " records have been indexed successfully");

    } catch (Exception e) {  
       e.printStackTrace();  
    } 
    }

    @SuppressWarnings("deprecation")
    int indexDocs1(IndexWriter writer, Connection conn) throws Exception {  
      String sql = QUERY;  
      Statement stmt = conn.createStatement();  
      ResultSet rs = stmt.executeQuery(sql);  
      int i=0;
      while (rs.next()) {  

//检查null并允许它添加

        String FTID = resultSet.getString("FTID"); //!= null ? resultSet.getString("FTID"): " ";
        String ID = resultSet.getString("ID")!= null ? resultSet.getString("ID"): " ";

        String CLASSID = resultSet.getString("CLASSID")!= null ? resultSet.getString("CLASSID"): " ";

        String TEXT = resultSet.getString("TEXT")!= null ? resultSet.getString("TEXT"): " ";

        String PUBNOTICECONTENT = resultSet.getString("PUBNOTICECONTENT")!= null ? resultSet.getString("PUBNOTICECONTENT"): " ";
        String DOCUMENTCONTENT = resultSet.getString("DOCUMENTCONTENT")!= null ? resultSet.getString("DOCUMENTCONTENT"): " ";

                String contentSum_DE = resultSet.getString("contentSum_DE")!= null ? resultSet.getString("contentSum_DE"): " ";

         Document d = new Document();  
         d.add(new Field("FTID", rs.getString("FTID"), Field.Store.YES, Field.Index.NOT_ANALYZED));
         d.add(new Field("ID", rs.getString("ID"), Field.Store.YES, Field.Index.NOT_ANALYZED ));
         d.add(new Field("CLASSID", rs.getString("CLASSID"), Field.Store.YES, Field.Index.ANALYZED));

           if (writer.getConfig().getOpenMode() == OpenMode.CREATE) {
        // New index, so we just add the document (no old document can be there):
        System.out.println("adding " + FTID + " ---- " + ID + "---- " + CLASSID);
          writer.addDocument(doc);

        }

        }

} catch (Exception e) {
  System.out.println(e);
}

1 个答案:

答案 0 :(得分:1)

错误清楚地表明Lucenetest无法编译。 原因是FSDirectory期望java.nio.file.Path但收到不兼容的java.io.File
看起来代码是为之前版本的Lucene开发的,它接受了文件https://lucene.apache.org/core/3_0_3/api/core/org/apache/lucene/store/FSDirectory.html。最近的Lucene版本期望Path https://lucene.apache.org/core/5_3_0/core/index.html?org/apache/lucene/store/FSDirectory.html
解决方案是将错误更改为

 Directory indexDir = FSDirectory.open(new File(PATH).toPath());