Move index file to .index

This commit is contained in:
coolneng 2021-01-11 19:09:02 +01:00
parent 61193050db
commit 6a3087cabd
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
1 changed files with 10 additions and 6 deletions

View File

@ -19,6 +19,7 @@ import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.analysis.en.EnglishAnalyzer;
import org.apache.lucene.analysis.miscellaneous.PerFieldAnalyzerWrapper;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
@ -32,12 +33,14 @@ import com.google.gson.Gson;
public class Indexer {
IndexWriter index;
String folderPath;
String dataFolderPath;
String indexFolderPath;
List<File> files;
PerFieldAnalyzerWrapper customAnalyzer;
Indexer(String folderPath) throws IOException, ParseException {
this.folderPath = folderPath;
Indexer(String dataFolderPath, String indexFolderPath) throws IOException, ParseException {
this.dataFolderPath = dataFolderPath;
this.indexFolderPath = indexFolderPath;
files = readFiles();
customAnalyzer = createAnalyzer();
}
@ -46,13 +49,14 @@ public class Indexer {
Map<String, Analyzer> analyzerPerField = new HashMap<>();
analyzerPerField.put("title", new EnglishAnalyzer());
analyzerPerField.put("abstract", new EnglishAnalyzer());
analyzerPerField.put("authors", new StandardAnalyzer());
PerFieldAnalyzerWrapper customAnalyzer = new PerFieldAnalyzerWrapper(new WhitespaceAnalyzer(),
analyzerPerField);
return customAnalyzer;
}
List<File> readFiles() throws IOException {
List<File> files = Files.walk(Paths.get(folderPath)).filter(Files::isRegularFile).map(Path::toFile)
List<File> files = Files.walk(Paths.get(dataFolderPath)).filter(Files::isRegularFile).map(Path::toFile)
.collect(Collectors.toList());
return files;
}
@ -66,7 +70,7 @@ public class Indexer {
}
void createIndex() throws IOException {
Directory dir = FSDirectory.open(Paths.get(folderPath));
Directory dir = FSDirectory.open(Paths.get(indexFolderPath));
IndexWriterConfig config = new IndexWriterConfig(customAnalyzer);
config.setOpenMode(OpenMode.CREATE);
index = new IndexWriter(dir, config);
@ -108,7 +112,7 @@ public class Indexer {
usage();
}
String dataDirectory = args[0];
Indexer indexer = new Indexer(dataDirectory);
Indexer indexer = new Indexer(dataDirectory, ".index");
indexer.populateIndex();
}
}