Reputation: 319
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:
The constructor Field(String, Reader) is undefined.
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.
Upvotes: 3
Views: 496
Reputation: 17534
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).
Upvotes: 4