1010import org .apache .commons .io .FileUtils ;
1111import org .apache .commons .lang3 .StringUtils ;
1212import org .apache .lucene .analysis .Analyzer ;
13- import org .apache .lucene .document .Document ;
14- import org .apache .lucene .document .Field ;
15- import org .apache .lucene .document .StringField ;
13+ import org .apache .lucene .document .*;
1614import org .apache .lucene .index .*;
1715import org .apache .lucene .search .*;
1816import org .apache .lucene .store .Directory ;
1917import org .apache .lucene .store .FSDirectory ;
18+ import org .apache .lucene .store .NoLockFactory ;
2019
2120import java .io .File ;
2221import java .io .IOException ;
@@ -44,16 +43,19 @@ public class FileSearchToolService {
4443
4544 private static final String searchIndexDir = ConfigureUtil .getConfigurePath ("searchIndexDir/" );
4645
46+ private static Directory directory ;
4747 private static IndexWriter indexWriter = null ;
48+ private static IndexSearcher indexSearcher = null ;
4849
49- private static Directory directory ;
5050 private static int commonIndex = 0 ;
51+ private static boolean isAddIndex = false ;
5152
5253 static {
5354 File searchIndexDirFile = new File (searchIndexDir );
5455 try {
5556 FileUtils .forceMkdir (searchIndexDirFile );
56- directory = FSDirectory .open (searchIndexDirFile .toPath ());
57+ directory = FSDirectory .open (searchIndexDirFile .toPath (), NoLockFactory .INSTANCE );
58+ // directory = new RAMDirectory();
5759 getIndexWriter (directory );
5860 if (!DirectoryReader .indexExists (directory )) {
5961 indexWriter .close ();
@@ -74,22 +76,27 @@ protected TokenStreamComponents createComponents(String fieldName) {
7476 }; // 标准分词器,适用于英文
7577 //创建索引写入配置
7678 IndexWriterConfig indexWriterConfig = new IndexWriterConfig (analyzer );
77- indexWriterConfig .setRAMBufferSizeMB (Runtime .getRuntime ().totalMemory () / 1024 / 1024 / 4 );
78- // indexWriterConfig.setMaxBufferedDocs(2000);
79- // LogMergePolicy mergePolicy = new LogByteSizeMergePolicy();
79+ // indexWriterConfig.setRAMBufferSizeMB(Runtime.getRuntime().totalMemory() / 1024 / 1024 / 4);
80+ indexWriterConfig .setRAMBufferSizeMB (64 );
81+ // indexWriterConfig.setMaxBufferedDocs(1000);
82+ // LogMergePolicy mergePolicy = new LogDocMergePolicy();
8083// //设置segment添加文档(Document)时的合并频率 //值较小,建立索引的速度就较慢 //值较大,建立索引的速度就较快,>10适合批量建立索引
8184// mergePolicy.setMergeFactor(50);
8285// //设置segment最大合并文档(Document)数
8386// //值较小有利于追加索引的速度
8487// //值较大,适合批量建立索引和更快的搜索
8588// mergePolicy.setMaxMergeDocs(5000);
8689// indexWriterConfig.setMergePolicy(mergePolicy);
90+ // indexWriterConfig.setUseCompoundFile(false);
8791 //创建索引写入对象
8892 indexWriter = new IndexWriter (directory , indexWriterConfig );
8993 }
9094
9195 public IndexSearcher getIndexSearcher () {
92- IndexSearcher indexSearcher = null ;
96+ if (!isAddIndex && indexSearcher != null ) {
97+ return indexSearcher ;
98+ }
99+ // IndexSearcher indexSearcher = null;
93100 try {
94101 // 创建索引的读取器
95102 IndexReader indexReader = DirectoryReader .open (directory );
@@ -98,6 +105,7 @@ public IndexSearcher getIndexSearcher() {
98105 } catch (Exception e ) {
99106 log .error ("创建索引读取器失败:" , e );
100107 }
108+ isAddIndex = false ;
101109 return indexSearcher ;
102110 }
103111
@@ -128,17 +136,17 @@ public void searchContentAction() throws Exception {
128136 }
129137 }
130138 if (fileSearchToolController .getShowHideFileChoice ().getSelectionModel ().getSelectedIndex () == 1 ) {
131- TermQuery isHiddenQuery = new TermQuery ( new Term ( "isHidden" , "false" ) );
139+ Query isHiddenQuery = IntPoint . newExactQuery ( "isHidden" , 0 );
132140 builder .add (isHiddenQuery , BooleanClause .Occur .MUST );
133141 } else if (fileSearchToolController .getShowHideFileChoice ().getSelectionModel ().getSelectedIndex () == 2 ) {
134- TermQuery isHiddenQuery = new TermQuery ( new Term ( "isHidden" , "true" ) );
142+ Query isHiddenQuery = IntPoint . newExactQuery ( "isHidden" , 1 );
135143 builder .add (isHiddenQuery , BooleanClause .Occur .MUST );
136144 }
137145 if (fileSearchToolController .getFileTypeChoiceBox ().getSelectionModel ().getSelectedIndex () == 1 ) {
138- TermQuery isDirectoryQuery = new TermQuery ( new Term ( "isDirectory" , "false" ) );
146+ Query isDirectoryQuery = IntPoint . newExactQuery ( "isDirectory" , 0 );
139147 builder .add (isDirectoryQuery , BooleanClause .Occur .MUST );
140148 } else if (fileSearchToolController .getFileTypeChoiceBox ().getSelectionModel ().getSelectedIndex () == 2 ) {
141- TermQuery isDirectoryQuery = new TermQuery ( new Term ( "isDirectory" , "true" ) );
149+ Query isDirectoryQuery = IntPoint . newExactQuery ( "isDirectory" , 1 );
142150 builder .add (isDirectoryQuery , BooleanClause .Occur .MUST );
143151 }
144152 if (StringUtils .isNotEmpty (path )) {
@@ -152,18 +160,22 @@ public void searchContentAction() throws Exception {
152160 for (ScoreDoc scoreDoc : topDocs .scoreDocs ) {
153161 //取得对应的文档对象
154162 Document document = indexSearcher .doc (scoreDoc .doc );
155- // System.out.println("fileName:" + document.get("fileName"));
156163 Map map = new HashMap ();
157164 map .put ("fileName" , document .get ("fileName" ));
158165 map .put ("absolutePath" , document .get ("absolutePath" ));
159- map .put ("fileSize" , document .get ("fileSize" ));
160- // map.put("lastModified", document.get("lastModified"));
161- map .put ("lastModified" , new Date (Long .parseLong (document .get ("lastModified" ))).toLocaleString ());
166+ // map.put("fileSize", document.get("fileSize"));
167+ map .put ("fileSize" , (int ) Math .ceil (document .getField ("fileSize" ).numericValue ().longValue () / 1024.0 ) + "KB" );
168+ // map.put("lastModified", new Date(Long.parseLong(document.get("lastModified"))).toLocaleString());
169+ map .put ("lastModified" , new Date (document .getField ("lastModified" ).numericValue ().longValue ()).toLocaleString ());
162170 fileSearchToolController .getSearchResultTableData ().add (map );
163171 }
172+ long selectCount = topDocs .totalHits .value ;
164173 int allCount = indexSearcher .count (new MatchAllDocsQuery ());
165174 long consumingTime = System .currentTimeMillis () - startTime ;
166- fileSearchToolController .getSearchTextLabel ().setText ("总共查询到" + topDocs .totalHits .value + "个文档; 当前一共缓存" + allCount + "个文档; 耗时:" + consumingTime + "毫秒" );
175+ if (selectCount == 1001 ) {
176+ selectCount = indexSearcher .count (query );
177+ }
178+ fileSearchToolController .getSearchTextLabel ().setText ("总共查询到" + selectCount + "个文档; 当前一共缓存" + allCount + "个文档; 耗时:" + consumingTime + "毫秒" );
167179 }
168180
169181 public void refreshIndexAction () throws Exception {
@@ -192,6 +204,7 @@ public void addSearchIndexFile(Path path) {
192204 }
193205 }
194206 indexWriter .commit ();
207+ isAddIndex = true ;
195208 } catch (Exception e ) {
196209 log .warn ("获取失败:" , e );
197210 }
@@ -203,18 +216,21 @@ public void addIndexDocument(File file) throws Exception {
203216 doc .add (new StringField ("fileName" , file .getName (), Field .Store .YES ));
204217 doc .add (new StringField ("fileNameLowerCase" , file .getName ().toLowerCase (), Field .Store .NO ));
205218 doc .add (new StringField ("absolutePath" , file .getAbsolutePath (), Field .Store .YES ));
206- // doc.add(new DoubleDocValuesField("fileSize", file.length()));
207- // doc.add(new DoubleDocValuesField("lastModified", file.lastModified()));
208- doc .add (new StringField ("fileSize" , String .valueOf (file .length ()), Field .Store .YES ));
209- doc .add (new StringField ("lastModified" , String .valueOf (file .lastModified ()), Field .Store .YES ));
210- doc .add (new StringField ("isHidden" , String .valueOf (file .isHidden ()), Field .Store .NO ));
211- doc .add (new StringField ("isDirectory" , String .valueOf (file .isDirectory ()), Field .Store .NO ));
212- indexWriter .updateDocument (new Term ("absolutePath" , file .getAbsolutePath ()), doc );
219+ doc .add (new StoredField ("fileSize" , file .length ()));
220+ doc .add (new StoredField ("lastModified" , file .lastModified ()));
221+ doc .add (new IntPoint ("isHidden" , file .isHidden () ? 1 : 0 ));
222+ doc .add (new IntPoint ("isDirectory" , file .isDirectory () ? 1 : 0 ));
223+ // doc.add(new StringField("fileSize", String.valueOf(file.length()), Field.Store.YES));
224+ // doc.add(new StringField("lastModified", String.valueOf(file.lastModified()), Field.Store.YES));
225+ // doc.add(new StringField("isHidden", String.valueOf(file.isHidden()), Field.Store.NO));
226+ // doc.add(new StringField("isDirectory", String.valueOf(file.isDirectory()), Field.Store.NO));
213227
228+ indexWriter .updateDocument (new Term ("absolutePath" , file .getAbsolutePath ()), doc );
214229// indexWriter.addDocument(doc);
215- if (commonIndex ++ % 50 == 0 ) {
216- indexWriter .commit ();
217- }
230+
231+ // if (commonIndex++ % 200 == 0) {
232+ // indexWriter.commit();
233+ // }
218234 }
219235
220236 public void autoRefreshIndexAction () {
@@ -224,4 +240,14 @@ public void autoRefreshIndexAction() {
224240 addSearchIndexFile (listRoot .toPath ());
225241 }
226242 }
243+
244+ public void deleteDocument (String absolutePath ) {
245+ try {
246+ indexWriter .deleteDocuments (new Term ("absolutePath" , absolutePath ));
247+ indexWriter .commit ();
248+ isAddIndex = true ;
249+ } catch (Exception e ) {
250+ log .error ("删除索引失败:" , e );
251+ }
252+ }
227253}
0 commit comments