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