33import cn .hutool .core .thread .ThreadUtil ;
44import com .xwintop .xJavaFxTool .controller .littleTools .FileSearchToolController ;
55import com .xwintop .xJavaFxTool .utils .ConfigureUtil ;
6+ import com .xwintop .xcore .util .javafx .TooltipUtil ;
67import lombok .Getter ;
78import lombok .Setter ;
89import lombok .extern .slf4j .Slf4j ;
910import org .apache .commons .io .FileUtils ;
1011import org .apache .commons .lang3 .StringUtils ;
1112import org .apache .lucene .analysis .Analyzer ;
1213import org .apache .lucene .analysis .standard .StandardAnalyzer ;
13- import org .apache .lucene .document .DateTools ;
1414import org .apache .lucene .document .Document ;
1515import org .apache .lucene .document .Field ;
1616import org .apache .lucene .document .StringField ;
2525import java .nio .file .Files ;
2626import java .nio .file .Path ;
2727import java .nio .file .Paths ;
28+ import java .util .Date ;
2829import java .util .HashMap ;
2930import java .util .Iterator ;
3031import java .util .Map ;
@@ -45,18 +46,14 @@ public class FileSearchToolService {
4546 private static final String searchIndexDir = ConfigureUtil .getConfigurePath ("searchIndexDir/" );
4647
4748 private static IndexWriter indexWriter = null ;
48- // private static IndexSearcher indexSearcher = null;
49+
50+ // private static RAMDirectory directory;
4951
5052 static {
5153 File searchIndexDirFile = new File (searchIndexDir );
5254 try {
5355 FileUtils .forceMkdir (searchIndexDirFile );
5456 Directory directory = FSDirectory .open (searchIndexDirFile .toPath ());
55- // Analyzer analyzer = new StandardAnalyzer(); // 标准分词器,适用于英文
56- // //创建索引写入配置
57- // IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
58- // //创建索引写入对象
59- // indexWriter = new IndexWriter(directory, indexWriterConfig);
6057 getIndexWriter (directory );
6158 if (!DirectoryReader .indexExists (directory )) {
6259 indexWriter .close ();
@@ -71,13 +68,14 @@ public static void getIndexWriter(Directory directory) throws IOException {
7168 Analyzer analyzer = new StandardAnalyzer (); // 标准分词器,适用于英文
7269 //创建索引写入配置
7370 IndexWriterConfig indexWriterConfig = new IndexWriterConfig (analyzer );
71+ indexWriterConfig .setRAMBufferSizeMB (Runtime .getRuntime ().totalMemory () / 1024 / 1024 / 4 );
72+ // indexWriterConfig.setMaxBufferedDocs(2000);
7473 //创建索引写入对象
7574 indexWriter = new IndexWriter (directory , indexWriterConfig );
7675 }
7776
7877 public IndexSearcher getIndexSearcher () {
7978 IndexSearcher indexSearcher = null ;
80- // if (indexSearcher == null) {
8179 try {
8280 Directory directory = FSDirectory .open (Paths .get (searchIndexDir ));
8381 // 创建索引的读取器
@@ -87,7 +85,6 @@ public IndexSearcher getIndexSearcher() {
8785 } catch (Exception e ) {
8886 log .error ("创建索引读取器失败:" , e );
8987 }
90- // }
9188 return indexSearcher ;
9289 }
9390
@@ -100,7 +97,7 @@ public void searchContentAction() throws Exception {
10097 String path = fileSearchToolController .getSearchDirectoryTextField ().getText ().trim ();
10198 BooleanQuery .Builder builder = new BooleanQuery .Builder ();
10299 String fileName = "fileName" ;
103- if (fileSearchToolController .getMatchCaseCheckBox ().isSelected ()) {
100+ if (! fileSearchToolController .getMatchCaseCheckBox ().isSelected ()) {
104101 fileName = "fileNameLowerCase" ;
105102 queryText = queryText .toLowerCase ();
106103 }
@@ -136,39 +133,50 @@ public void searchContentAction() throws Exception {
136133 }
137134 BooleanQuery query = builder .build ();
138135 IndexSearcher indexSearcher = getIndexSearcher ();
139- TopDocs topDocs = indexSearcher .search (query , Integer . MAX_VALUE );
136+ TopDocs topDocs = indexSearcher .search (query , 100 );
140137 fileSearchToolController .getSearchResultTableData ().clear ();
141138 for (ScoreDoc scoreDoc : topDocs .scoreDocs ) {
142139 //取得对应的文档对象
143140 Document document = indexSearcher .doc (scoreDoc .doc );
144141// System.out.println("fileName:" + document.get("fileName"));
145- // System.out.println("absolutePath:" + document.get("absolutePath"));
146142 Map map = new HashMap ();
147143 map .put ("fileName" , document .get ("fileName" ));
148144 map .put ("absolutePath" , document .get ("absolutePath" ));
149145 map .put ("fileSize" , document .get ("fileSize" ));
150- map .put ("lastModified" , document .get ("lastModified" ));
146+ // map.put("lastModified", document.get("lastModified"));
147+ map .put ("lastModified" , new Date (Long .parseLong (document .get ("lastModified" ))).toLocaleString ());
151148 fileSearchToolController .getSearchResultTableData ().add (map );
152149 }
153- fileSearchToolController .getSearchTextLabel ().setText ("总共查询到" + topDocs .totalHits + "个文档" );
150+ int allCount = indexSearcher .count (new MatchAllDocsQuery ());
151+ fileSearchToolController .getSearchTextLabel ().setText ("总共查询到" + topDocs .totalHits .value + "个文档;当前一共缓存" + allCount + "个文档" );
154152 }
155153
156154 public void refreshIndexAction () throws Exception {
157155 String path = fileSearchToolController .getSearchDirectoryTextField ().getText ();
158- addSearchIndexFile (path );
156+ if (StringUtils .isEmpty (path )) {
157+ TooltipUtil .showToast ("路径不能为空!" );
158+ return ;
159+ }
160+ addSearchIndexFile (Paths .get (path ));
159161 }
160162
161- public void addSearchIndexFile (String path ) {
163+ public void addSearchIndexFile (Path path ) {
162164 ThreadUtil .execute (() -> {
163165 try {
164- DirectoryStream <Path > stream = Files .newDirectoryStream (Paths . get ( path ) );
166+ DirectoryStream <Path > stream = Files .newDirectoryStream (path );
165167 Iterator <Path > pathIterator = stream .iterator ();
166168 while (pathIterator .hasNext ()) {
167169 Path curPath = pathIterator .next ();
168- addIndexDocument (curPath .toFile ());
169- if (Files .isDirectory (curPath )) {
170- addSearchIndexFile (curPath .toString ());
171- }
170+ ThreadUtil .execute (() -> {
171+ try {
172+ addIndexDocument (curPath .toFile ());
173+ if (Files .isDirectory (curPath )) {
174+ addSearchIndexFile (curPath );
175+ }
176+ } catch (Exception e ) {
177+ log .warn ("添加索引失败:" , e );
178+ }
179+ });
172180 }
173181 } catch (Exception e ) {
174182 log .warn ("获取失败:" , e );
@@ -179,16 +187,24 @@ public void addSearchIndexFile(String path) {
179187 public void addIndexDocument (File file ) throws Exception {
180188 Document doc = new Document ();
181189 doc .add (new StringField ("fileName" , file .getName (), Field .Store .YES ));
182- doc .add (new StringField ("fileNameLowerCase" , file .getName ().toLowerCase (), Field .Store .YES ));
190+ doc .add (new StringField ("fileNameLowerCase" , file .getName ().toLowerCase (), Field .Store .NO ));
183191 doc .add (new StringField ("absolutePath" , file .getAbsolutePath (), Field .Store .YES ));
184192// doc.add(new DoubleDocValuesField("fileSize", file.length()));
185193// doc.add(new DoubleDocValuesField("lastModified", file.lastModified()));
186194 doc .add (new StringField ("fileSize" , String .valueOf (file .length ()), Field .Store .YES ));
187- doc .add (new StringField ("lastModified" , DateTools . timeToString (file .lastModified (), DateTools . Resolution . MILLISECOND ), Field .Store .YES ));
188- doc .add (new StringField ("isHidden" , String .valueOf (file .isHidden ()), Field .Store .YES ));
189- doc .add (new StringField ("isDirectory" , String .valueOf (file .isDirectory ()), Field .Store .YES ));
195+ doc .add (new StringField ("lastModified" , String . valueOf (file .lastModified ()), Field .Store .YES ));
196+ doc .add (new StringField ("isHidden" , String .valueOf (file .isHidden ()), Field .Store .NO ));
197+ doc .add (new StringField ("isDirectory" , String .valueOf (file .isDirectory ()), Field .Store .NO ));
190198 indexWriter .updateDocument (new Term ("absolutePath" , file .getAbsolutePath ()), doc );
191199// indexWriter.addDocument(doc);
192200 indexWriter .commit ();
193201 }
202+
203+ public void autoRefreshIndexAction () {
204+ File [] listRoots = File .listRoots ();
205+ for (File listRoot : listRoots ) {
206+ System .out .println ("加载目录: " + listRoot .getAbsolutePath ());
207+ addSearchIndexFile (listRoot .toPath ());
208+ }
209+ }
194210}
0 commit comments