Skip to content

Commit bd5fc00

Browse files
committed
1、优化文件搜索;2、添加图标显示
1 parent c5b92c0 commit bd5fc00

2 files changed

Lines changed: 101 additions & 28 deletions

File tree

src/main/java/com/xwintop/xJavaFxTool/controller/littleTools/FileSearchToolController.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,24 @@
44
import com.xwintop.xJavaFxTool.utils.JavaFxViewUtil;
55
import com.xwintop.xJavaFxTool.view.littleTools.FileSearchToolView;
66
import com.xwintop.xcore.util.javafx.FileChooserUtil;
7+
import javafx.application.Platform;
78
import javafx.collections.FXCollections;
89
import javafx.collections.ObservableList;
10+
import javafx.embed.swing.SwingFXUtils;
911
import javafx.event.ActionEvent;
1012
import javafx.fxml.FXML;
13+
import javafx.scene.control.TableCell;
14+
import javafx.scene.control.TableColumn;
15+
import javafx.scene.image.Image;
16+
import javafx.scene.image.ImageView;
17+
import javafx.util.Callback;
1118
import lombok.Getter;
1219
import lombok.Setter;
1320
import lombok.extern.slf4j.Slf4j;
1421

22+
import javax.swing.*;
23+
import javax.swing.filechooser.FileSystemView;
24+
import java.awt.image.BufferedImage;
1525
import java.io.File;
1626
import java.net.URL;
1727
import java.util.Map;
@@ -40,6 +50,43 @@ public void initialize(URL location, ResourceBundle resources) {
4050

4151
private void initView() {
4252
JavaFxViewUtil.setTableColumnMapValueFactory(fileNameTableColumn, "fileName", false);
53+
fileNameTableColumn.setCellFactory(new Callback<TableColumn<Map<String, String>, String>, TableCell<Map<String, String>, String>>() {
54+
@Override
55+
public TableCell<Map<String, String>, String> call(TableColumn<Map<String, String>, String> param) {
56+
TableCell<Map<String, String>, String> cell = new TableCell<Map<String, String>, String>() {
57+
@Override
58+
protected void updateItem(String item, boolean empty) {
59+
super.updateItem(item, empty);
60+
this.setText(item);
61+
if (item != null) {
62+
try {
63+
String absolutePath = searchResultTableData.get(this.getIndex()).get("absolutePath");
64+
File file = new File(absolutePath);
65+
if (!file.exists()) {
66+
this.setText(null);
67+
this.setGraphic(null);
68+
Platform.runLater(() -> {
69+
fileSearchToolService.deleteDocument(absolutePath);
70+
searchResultTableData.remove(this.getIndex());
71+
});
72+
return;
73+
}
74+
ImageIcon icon = (ImageIcon) FileSystemView.getFileSystemView().getSystemIcon(file);
75+
Image fxImage = SwingFXUtils.toFXImage((BufferedImage) icon.getImage(), null);
76+
ImageView imageView = new ImageView(fxImage);
77+
this.setGraphic(imageView);
78+
} catch (Exception e) {
79+
log.warn("设置图标失败:" + item, e);
80+
this.setGraphic(null);
81+
}
82+
} else {
83+
this.setGraphic(null);
84+
}
85+
}
86+
};
87+
return cell;
88+
}
89+
});
4390
JavaFxViewUtil.setTableColumnMapValueFactory(absolutePathTableColumn, "absolutePath", false);
4491
JavaFxViewUtil.setTableColumnMapValueFactory(fileSizeTableColumn, "fileSize", false);
4592
JavaFxViewUtil.setTableColumnMapValueFactory(lastModifiedTableColumn, "lastModified", false);

src/main/java/com/xwintop/xJavaFxTool/services/littleTools/FileSearchToolService.java

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@
1010
import org.apache.commons.io.FileUtils;
1111
import org.apache.commons.lang3.StringUtils;
1212
import 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.*;
1614
import org.apache.lucene.index.*;
1715
import org.apache.lucene.search.*;
1816
import org.apache.lucene.store.Directory;
1917
import org.apache.lucene.store.FSDirectory;
18+
import org.apache.lucene.store.NoLockFactory;
2019

2120
import java.io.File;
2221
import 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

Comments
 (0)