1010import org .apache .commons .io .FileUtils ;
1111import org .apache .commons .lang3 .StringUtils ;
1212import org .apache .lucene .analysis .Analyzer ;
13- import org .apache .lucene .analysis .standard .StandardAnalyzer ;
1413import org .apache .lucene .document .Document ;
1514import org .apache .lucene .document .Field ;
1615import org .apache .lucene .document .StringField ;
@@ -47,14 +46,14 @@ public class FileSearchToolService {
4746
4847 private static IndexWriter indexWriter = null ;
4948
50- // private static RAMDirectory directory;
49+ private static Directory directory ;
5150 private static int commonIndex = 0 ;
5251
5352 static {
5453 File searchIndexDirFile = new File (searchIndexDir );
5554 try {
5655 FileUtils .forceMkdir (searchIndexDirFile );
57- Directory directory = FSDirectory .open (searchIndexDirFile .toPath ());
56+ directory = FSDirectory .open (searchIndexDirFile .toPath ());
5857 getIndexWriter (directory );
5958 if (!DirectoryReader .indexExists (directory )) {
6059 indexWriter .close ();
@@ -66,7 +65,13 @@ public class FileSearchToolService {
6665 }
6766
6867 public static void getIndexWriter (Directory directory ) throws IOException {
69- Analyzer analyzer = new StandardAnalyzer (); // 标准分词器,适用于英文
68+ // Analyzer analyzer = new StandardAnalyzer(); // 标准分词器,适用于英文
69+ Analyzer analyzer = new Analyzer () {
70+ @ Override
71+ protected TokenStreamComponents createComponents (String fieldName ) {
72+ return null ;
73+ }
74+ }; // 标准分词器,适用于英文
7075 //创建索引写入配置
7176 IndexWriterConfig indexWriterConfig = new IndexWriterConfig (analyzer );
7277 indexWriterConfig .setRAMBufferSizeMB (Runtime .getRuntime ().totalMemory () / 1024 / 1024 / 4 );
@@ -86,7 +91,6 @@ public static void getIndexWriter(Directory directory) throws IOException {
8691 public IndexSearcher getIndexSearcher () {
8792 IndexSearcher indexSearcher = null ;
8893 try {
89- Directory directory = FSDirectory .open (Paths .get (searchIndexDir ));
9094 // 创建索引的读取器
9195 IndexReader indexReader = DirectoryReader .open (directory );
9296 // 创建一个索引的查找器,来检索索引库
0 commit comments