11package com .xwintop .xJavaFxTool .services .littleTools ;
22
3+ import cn .hutool .core .thread .ThreadUtil ;
34import com .xwintop .xJavaFxTool .controller .littleTools .FileSearchToolController ;
5+ import com .xwintop .xJavaFxTool .utils .ConfigureUtil ;
46import lombok .Getter ;
57import lombok .Setter ;
68import lombok .extern .slf4j .Slf4j ;
7- import org .apache .lucene .document .*;
9+ import org .apache .commons .io .FileUtils ;
10+ import org .apache .lucene .analysis .Analyzer ;
11+ import org .apache .lucene .analysis .standard .StandardAnalyzer ;
12+ import org .apache .lucene .document .DateTools ;
13+ import org .apache .lucene .document .Document ;
14+ import org .apache .lucene .document .Field ;
15+ import org .apache .lucene .document .TextField ;
16+ import org .apache .lucene .index .*;
17+ import org .apache .lucene .search .*;
18+ import org .apache .lucene .store .Directory ;
19+ import org .apache .lucene .store .FSDirectory ;
820
921import java .io .File ;
22+ import java .io .IOException ;
1023import java .nio .file .DirectoryStream ;
1124import java .nio .file .Files ;
1225import java .nio .file .Path ;
1326import java .nio .file .Paths ;
27+ import java .util .HashMap ;
1428import java .util .Iterator ;
29+ import java .util .Map ;
1530
1631/**
1732 * @ClassName: FileSearchToolService
2641public class FileSearchToolService {
2742 private FileSearchToolController fileSearchToolController ;
2843
29- public FileSearchToolService (FileSearchToolController fileSearchToolController ) {
30- this .fileSearchToolController = fileSearchToolController ;
31- }
44+ private static final String searchIndexDir = ConfigureUtil .getConfigurePath ("searchIndexDir/" );
3245
33- public void searchContentAction () {
46+ private static IndexWriter indexWriter = null ;
47+ private static IndexSearcher indexSearcher = null ;
3448
35- }
49+ static {
50+ File searchIndexDirFile = new File (searchIndexDir );
51+ try {
52+ FileUtils .forceMkdir (searchIndexDirFile );
53+ Directory directory = FSDirectory .open (searchIndexDirFile .toPath ());
54+ Analyzer analyzer = new StandardAnalyzer (); // 标准分词器,适用于英文
55+ //创建索引写入配置
56+ IndexWriterConfig indexWriterConfig = new IndexWriterConfig (analyzer );
57+ //创建索引写入对象
58+ indexWriter = new IndexWriter (directory , indexWriterConfig );
3659
37- private void refreshIndexAction () {
60+ // 创建索引的读取器
61+ IndexReader indexReader = DirectoryReader .open (directory );
62+ // 创建一个索引的查找器,来检索索引库
63+ indexSearcher = new IndexSearcher (indexReader );
64+ } catch (IOException e ) {
65+ log .error ("创建文件失败!" , e );
66+ }
67+ }
3868
69+ public FileSearchToolService (FileSearchToolController fileSearchToolController ) {
70+ this .fileSearchToolController = fileSearchToolController ;
3971 }
4072
41- private void searchDirectoryAction () {
73+ public void searchContentAction () throws Exception {
74+ String queryText = fileSearchToolController .getSearchContentTextField ().getText ();
75+ if (!fileSearchToolController .getFullTextMatchingCheckBox ().isSelected ()) {
76+ queryText = "*" + queryText + "*" ;
77+ }
78+ Query query = new WildcardQuery (new Term ("fileName" , queryText ));
79+ TopDocs topDocs = indexSearcher .search (query , 100 );
80+ //打印查询到的记录数
81+ // System.out.println("总共查询到" + topDocs.totalHits + "个文档");
82+ fileSearchToolController .getSearchResultTableData ().clear ();
83+ for (ScoreDoc scoreDoc : topDocs .scoreDocs ) {
84+ //取得对应的文档对象
85+ Document document = indexSearcher .doc (scoreDoc .doc );
86+ // System.out.println("fileName:" + document.get("fileName"));
87+ System .out .println ("absolutePath:" + document .get ("absolutePath" ));
88+ Map map = new HashMap ();
89+ map .put ("fileName" , document .get ("fileName" ));
90+ map .put ("absolutePath" , document .get ("absolutePath" ));
91+ map .put ("fileSize" , document .get ("fileSize" ));
92+ map .put ("lastModified" , document .get ("lastModified" ));
93+ fileSearchToolController .getSearchResultTableData ().add (map );
94+ }
95+ }
4296
97+ public void refreshIndexAction () throws Exception {
98+ String path = fileSearchToolController .getSearchDirectoryTextField ().getText ();
99+ addSearchIndexFile (path );
43100 }
44101
45- public void addSearchIndexFile (String path ) throws Exception {
46- DirectoryStream <Path > stream = Files .newDirectoryStream (Paths .get (path ));
47- Iterator <Path > pathIterator = stream .iterator ();
48- while (pathIterator .hasNext ()) {
49- Path curPath = pathIterator .next ();
50- // System.out.println(curPath.toString());
51- if (Files .isDirectory (curPath )) {
52- addSearchIndexFile (curPath .toString ());
53- } else {
54- // System.out.println(curPath.toString());
102+ public void addSearchIndexFile (String path ) {
103+ ThreadUtil .execute (() -> {
104+ try {
105+ DirectoryStream <Path > stream = Files .newDirectoryStream (Paths .get (path ));
106+ Iterator <Path > pathIterator = stream .iterator ();
107+ while (pathIterator .hasNext ()) {
108+ Path curPath = pathIterator .next ();
109+ if (Files .isDirectory (curPath )) {
110+ addSearchIndexFile (curPath .toString ());
111+ } else {
112+ // System.out.println(curPath.toString());
113+ addIndexDocument (curPath .toFile ());
114+ }
115+ }
116+ } catch (Exception e ) {
117+ log .error ("获取失败:" , e );
55118 }
56- }
119+ });
57120 }
58121
59122 public void addIndexDocument (File file ) throws Exception {
60123 Document doc = new Document ();
61124 doc .add (new TextField ("fileName" , file .getName (), Field .Store .YES ));
62125 doc .add (new TextField ("absolutePath" , file .getAbsolutePath (), Field .Store .YES ));
63- doc .add (new LongPoint ("fileSize" , file .length ()));
126+ // doc.add(new LongPoint("fileSize", file.length()));
127+ doc .add (new TextField ("fileSize" , String .valueOf (file .length ()), Field .Store .YES ));
64128 doc .add (new TextField ("lastModified" , DateTools .timeToString (file .lastModified (), DateTools .Resolution .MILLISECOND ), Field .Store .YES ));
129+ indexWriter .updateDocument (new Term ("absolutePath" , file .getAbsolutePath ()), doc );
130+ // indexWriter.addDocument(doc);
131+ indexWriter .commit ();
65132 }
66133}
0 commit comments