0

Hello I am working on Lucene to index my database records but I am unable to solve this error.

Error:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: Directory cannot be resolved to a type The method open(Path) in the type FSDirectory is not applicable for the arguments (File)

at 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()) {  

//checking for null and allowing it to add

        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 Answer 1

1

The error clearly says that Lucenetest can't be compiled. The reason is FSDirectory expects java.nio.file.Path but receives incompatible java.io.File.
Looks like the code was developed for previous version of Lucene which accepted File https://lucene.apache.org/core/3_0_3/api/core/org/apache/lucene/store/FSDirectory.html. The recent Lucene versions expect Path https://lucene.apache.org/core/5_3_0/core/index.html?org/apache/lucene/store/FSDirectory.html.
The solution is to change line with error to

 Directory indexDir = FSDirectory.open(new File(PATH).toPath());
Sign up to request clarification or add additional context in comments.

9 Comments

Yeah! thanks I didn't notice it, now its working but have a problem with JDBC connection
I see misprint in CONNECTION_URL = "jjdbc:sqlserver://WMDENTW1\\SQLEXPRESS:1433;" Try to remove extra "j".
The code is working but I am getting an error and also I couldn't see the indexed files using LUKE.. ERROR: Indexing to directory 'MMapDirectory@C:\dbindex lockFactory=org.apache.lucene.store.NativeFSLockFactory@4493d195'... java.lang.IllegalArgumentException: value cannot be null at org.apache.lucene.document.Field.<init>(Field.java:238) at org.apache.lucene.document.Field.<init>(Field.java:960) at lucene.Lucenetest.indexDocs1(Lucenetest.java:80) at lucene.Lucenetest.main(Lucenetest.java:56)
@SravanguptaK. IllegalArgumentException is thrown from org.apache.lucene.document.Field constructor. It contains null check for value passed in value param. So you need to check values that come from DB. Looks like some of them is null.
Yeah true some values are null some how I sorted it out but I am geeting one more exceptional error, as my database values are more than the string limit lucene cannot index so what to do and how?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.