Maven Carvalho
Maven Carvalho

Reputation: 319

Lucene: upgrade old project

I've built a project long time ago using Lucene 4.6. Now currently want to upgrade to 7.3.

This project has three files with one class each (with the same name as the file): Main, Indexer, Search.

The Main class carries the logic and calls the Indexer and Search in a procedural way.

I'm getting a problem while searching.

Inside of Main.java i have defined the place with the data directory and where the index is going to be and give Search term:

File dataDirectory = new File("C:\\datalocation");
File indexDirectory = new File("C:\\indexlocation");
(...)
Search.searchThis(indexDirectory,"Maven");

Inside of Search.java:

package code;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class Search {

    static void searchThis(File indexDirectory, String findme)
            throws IOException, ParseException {

        Directory directory = FSDirectory.open(indexDirectory);
        @SuppressWarnings("deprecation")
        IndexReader indexreader = IndexReader.open(directory);
        IndexSearcher searcher = new IndexSearcher(indexreader);

        QueryParser parser = new QueryParser("contents",
                new StandardAnalyzer());
        Query query = parser.parse(findme);
        TopDocs topDocs = searcher.search(query, 10);

        ScoreDoc[] hits = topDocs.scoreDocs;
        for (int i = 0; i < hits.length; i++) {

            int docId = hits[i].doc;

            Document d = searcher.doc(docId);

            System.out.println(d.get("path"));

        }

        System.out.println("Found: " + topDocs.totalHits);
    }

}

The problems I get are:

  1. The method open(Path) in the type FSDirectory is not applicable for the arguments (File)

  2. The method open(Directory) is undefined for the type IndexReader

How can I fix this?

Changing type of 'indexDirectory' to 'Path' is not an option to consider.

Upvotes: 2

Views: 472

Answers (1)

femtoRgon
femtoRgon

Reputation: 33341

1 - Convert using File.toPath:

File yourFile = indexDirectory;
Path yourPath = yourFile.toPath();
Directory directory = FSDirectory.open(yourPath);

2 - Use DirectoryReader.open:

DirectoryReader indexreader = DirectoryReader.open(directory);

Upvotes: 3

Related Questions