简体   繁体   中英

Lucene: building an index

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.

I'm getting a problem in the Indexer.java , more specifically in buildIndex() .

Inside of Main.java i have defined the place with the data directory and where the index is going to be and started creating the index by sending the path to buildIndex() where it must be built:

File dataDirectory = new File("C:\\datalocation");
File indexDirectory = new File("C:\\indexlocation");
(...)

IndexWriter index = Indexer.createIndex(indexDirectory);
Indexer.buildIndex(index, dataDirectory, indexDirectory);
Indexer.closeIndex(index);
System.out.println("Index built");

Inside of Indexer.java :

static void buildIndex(IndexWriter index, File dataDirectory,
            File IndexDirectory) throws IOException {
        File[] files = dataDirectory.listFiles();
        for (int i = 0; i < files.length; i++) {
            Document document = new Document();

            Reader reader = new FileReader(files[i]);
//the following line is where error 1 appears:
            document.add(new Field("contents", reader));

            String path = files[i].getCanonicalPath();
//the following line is where error 2 appears:
            document.add(new Field("path", path, Field.Store.YES,Field.Index.NOT_ANALYZED));

            index.addDocument(document);
        }
    }

The problems I get are:

  1. The constructor Field(String, Reader) is undefined.

  2. Index cannot be resolved or is not a field

How can I fix this?

Cast argument 'reader' to 'IndexableFieldType'

OR

Change type of 'reader' to 'IndexableFieldType' are not an option.

To note: The data directory path has in there a .txt file just with "Maven" written in it.

Both of these constructors of Field are marked as deprecated in Lucene 4, and were removed later on.

The recommended classes are TextField in both cases, or also StringField for the second one.

So the first one could look like :

document.add(new TextField("contents", reader));

And the second one :

document.add(new StringField("path", path, Field.Store.YES));

Note that I couldn't find a clear equivalent of the Field.Index.NOT_ANALYZED parameter, though ( StringField is not tokenized, TextField is, don't know if it is directly related to this).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM