Reputation: 135
I'm trying to upgrade Lucene in my application from version 3.5.0 to 5.0.0. The application has this code:
IndexReader indexReader=IndexReader.open(index);
String[] fields = indexReader.getFieldNames(FieldOption.ALL).toArray(new String[0]);
I need the fieldNames so I use them in a MultiFieldQueryParser. How can I make the change to version 5.0.0 ?
Upvotes: 2
Views: 147
Reputation: 33351
You can get field information from the reader using the LeafReader.getFieldInfos()
method, and in turn, you can get the field name from FieldInfo.name
:
public static String[] getFieldNames(IndexReader reader) {
List<String> fieldNames = new ArrayList<String>();
//For a simple reader over only one index, reader.leaves() should only return one LeafReaderContext
for (LeafReaderContext readerCtx : reader.leaves()) {
FieldInfos fields = readerCtx.reader().getFieldInfos();
for (FieldInfo field : fields) {
//Check whether the field is indexed and searchable, perhaps?
fieldNames.add(field.name);
}
}
return fieldNames.toArray(new String[fieldNames.size()]);
}
Note: It might worth considering using a catch-all field that contains all the content you want to search for, instead. This is a common, and quite useful, pattern (e.g. Elasticsearch does this by default, with their _all field)
Going that route, you can expect the index to become a bit larger, but it makes it simpler to use, and performance should be improved.
Upvotes: 2