77import lombok .Setter ;
88import lombok .extern .slf4j .Slf4j ;
99import org .apache .commons .io .FileUtils ;
10+ import org .apache .commons .lang3 .StringUtils ;
1011import org .apache .lucene .analysis .Analyzer ;
1112import org .apache .lucene .analysis .standard .StandardAnalyzer ;
1213import org .apache .lucene .document .DateTools ;
1314import org .apache .lucene .document .Document ;
1415import org .apache .lucene .document .Field ;
15- import org .apache .lucene .document .TextField ;
16+ import org .apache .lucene .document .StringField ;
1617import org .apache .lucene .index .*;
1718import org .apache .lucene .search .*;
1819import org .apache .lucene .store .Directory ;
@@ -44,54 +45,112 @@ public class FileSearchToolService {
4445 private static final String searchIndexDir = ConfigureUtil .getConfigurePath ("searchIndexDir/" );
4546
4647 private static IndexWriter indexWriter = null ;
47- private static IndexSearcher indexSearcher = null ;
48+ // private static IndexSearcher indexSearcher = null;
4849
4950 static {
5051 File searchIndexDirFile = new File (searchIndexDir );
5152 try {
5253 FileUtils .forceMkdir (searchIndexDirFile );
5354 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 );
55+ // Analyzer analyzer = new StandardAnalyzer(); // 标准分词器,适用于英文
56+ // //创建索引写入配置
57+ // IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
58+ // //创建索引写入对象
59+ // indexWriter = new IndexWriter(directory, indexWriterConfig);
60+ getIndexWriter (directory );
61+ if (!DirectoryReader .indexExists (directory )) {
62+ indexWriter .close ();
63+ getIndexWriter (directory );
64+ }
65+ } catch (Exception e ) {
66+ log .error ("创建文件失败!" , e );
67+ }
68+ }
5969
70+ public static void getIndexWriter (Directory directory ) throws IOException {
71+ Analyzer analyzer = new StandardAnalyzer (); // 标准分词器,适用于英文
72+ //创建索引写入配置
73+ IndexWriterConfig indexWriterConfig = new IndexWriterConfig (analyzer );
74+ //创建索引写入对象
75+ indexWriter = new IndexWriter (directory , indexWriterConfig );
76+ }
77+
78+ public IndexSearcher getIndexSearcher () {
79+ IndexSearcher indexSearcher = null ;
80+ // if (indexSearcher == null) {
81+ try {
82+ Directory directory = FSDirectory .open (Paths .get (searchIndexDir ));
6083 // 创建索引的读取器
6184 IndexReader indexReader = DirectoryReader .open (directory );
6285 // 创建一个索引的查找器,来检索索引库
6386 indexSearcher = new IndexSearcher (indexReader );
64- } catch (IOException e ) {
65- log .error ("创建文件失败! " , e );
87+ } catch (Exception e ) {
88+ log .error ("创建索引读取器失败: " , e );
6689 }
90+ // }
91+ return indexSearcher ;
6792 }
6893
6994 public FileSearchToolService (FileSearchToolController fileSearchToolController ) {
7095 this .fileSearchToolController = fileSearchToolController ;
7196 }
7297
7398 public void searchContentAction () throws Exception {
74- String queryText = fileSearchToolController .getSearchContentTextField ().getText ();
75- if (!fileSearchToolController .getFullTextMatchingCheckBox ().isSelected ()) {
76- queryText = "*" + queryText + "*" ;
99+ String queryText = fileSearchToolController .getSearchContentTextField ().getText ().trim ();
100+ String path = fileSearchToolController .getSearchDirectoryTextField ().getText ().trim ();
101+ BooleanQuery .Builder builder = new BooleanQuery .Builder ();
102+ String fileName = "fileName" ;
103+ if (fileSearchToolController .getMatchCaseCheckBox ().isSelected ()) {
104+ fileName = "fileNameLowerCase" ;
105+ queryText = queryText .toLowerCase ();
106+ }
107+ if (fileSearchToolController .getRegularCheckBox ().isSelected ()) {
108+ Query fileNameQuery = new RegexpQuery (new Term (fileName , queryText ));//正则搜索
109+ builder .add (fileNameQuery , BooleanClause .Occur .MUST );
110+ } else {
111+ if (fileSearchToolController .getFullTextMatchingCheckBox ().isSelected ()) {
112+ Query fileNameQuery = new TermQuery (new Term (fileName , queryText ));
113+ builder .add (fileNameQuery , BooleanClause .Occur .MUST );
114+ } else {
115+ Query fileNameQuery = new WildcardQuery (new Term (fileName , "*" + queryText + "*" ));// 通配符
116+ builder .add (fileNameQuery , BooleanClause .Occur .MUST );
117+ }
118+ }
119+ if (fileSearchToolController .getShowHideFileChoice ().getSelectionModel ().getSelectedIndex () == 1 ) {
120+ TermQuery isHiddenQuery = new TermQuery (new Term ("isHidden" , "false" ));
121+ builder .add (isHiddenQuery , BooleanClause .Occur .MUST );
122+ } else if (fileSearchToolController .getShowHideFileChoice ().getSelectionModel ().getSelectedIndex () == 2 ) {
123+ TermQuery isHiddenQuery = new TermQuery (new Term ("isHidden" , "true" ));
124+ builder .add (isHiddenQuery , BooleanClause .Occur .MUST );
125+ }
126+ if (fileSearchToolController .getFileTypeChoiceBox ().getSelectionModel ().getSelectedIndex () == 1 ) {
127+ TermQuery isDirectoryQuery = new TermQuery (new Term ("isDirectory" , "false" ));
128+ builder .add (isDirectoryQuery , BooleanClause .Occur .MUST );
129+ } else if (fileSearchToolController .getFileTypeChoiceBox ().getSelectionModel ().getSelectedIndex () == 2 ) {
130+ TermQuery isDirectoryQuery = new TermQuery (new Term ("isDirectory" , "true" ));
131+ builder .add (isDirectoryQuery , BooleanClause .Occur .MUST );
132+ }
133+ if (StringUtils .isNotEmpty (path )) {
134+ PrefixQuery pathQuery = new PrefixQuery (new Term ("absolutePath" , path ));// 字段搜索 Field:Keyword,自动在结尾添加 *
135+ builder .add (pathQuery , BooleanClause .Occur .MUST );
77136 }
78- Query query = new WildcardQuery (new Term ("fileName" , queryText ));
79- TopDocs topDocs = indexSearcher .search (query , 100 );
80- //打印查询到的记录数
81- // System.out.println("总共查询到" + topDocs.totalHits + "个文档");
137+ BooleanQuery query = builder .build ();
138+ IndexSearcher indexSearcher = getIndexSearcher ();
139+ TopDocs topDocs = indexSearcher .search (query , Integer .MAX_VALUE );
82140 fileSearchToolController .getSearchResultTableData ().clear ();
83141 for (ScoreDoc scoreDoc : topDocs .scoreDocs ) {
84142 //取得对应的文档对象
85143 Document document = indexSearcher .doc (scoreDoc .doc );
86144// System.out.println("fileName:" + document.get("fileName"));
87- System .out .println ("absolutePath:" + document .get ("absolutePath" ));
145+ // System.out.println("absolutePath:" + document.get("absolutePath"));
88146 Map map = new HashMap ();
89147 map .put ("fileName" , document .get ("fileName" ));
90148 map .put ("absolutePath" , document .get ("absolutePath" ));
91149 map .put ("fileSize" , document .get ("fileSize" ));
92150 map .put ("lastModified" , document .get ("lastModified" ));
93151 fileSearchToolController .getSearchResultTableData ().add (map );
94152 }
153+ fileSearchToolController .getSearchTextLabel ().setText ("总共查询到" + topDocs .totalHits + "个文档" );
95154 }
96155
97156 public void refreshIndexAction () throws Exception {
@@ -106,26 +165,28 @@ public void addSearchIndexFile(String path) {
106165 Iterator <Path > pathIterator = stream .iterator ();
107166 while (pathIterator .hasNext ()) {
108167 Path curPath = pathIterator .next ();
168+ addIndexDocument (curPath .toFile ());
109169 if (Files .isDirectory (curPath )) {
110170 addSearchIndexFile (curPath .toString ());
111- } else {
112- // System.out.println(curPath.toString());
113- addIndexDocument (curPath .toFile ());
114171 }
115172 }
116173 } catch (Exception e ) {
117- log .error ("获取失败:" , e );
174+ log .warn ("获取失败:" , e );
118175 }
119176 });
120177 }
121178
122179 public void addIndexDocument (File file ) throws Exception {
123180 Document doc = new Document ();
124- doc .add (new TextField ("fileName" , file .getName (), Field .Store .YES ));
125- doc .add (new TextField ("absolutePath" , file .getAbsolutePath (), Field .Store .YES ));
126- // doc.add(new LongPoint("fileSize", file.length()));
127- doc .add (new TextField ("fileSize" , String .valueOf (file .length ()), Field .Store .YES ));
128- doc .add (new TextField ("lastModified" , DateTools .timeToString (file .lastModified (), DateTools .Resolution .MILLISECOND ), Field .Store .YES ));
181+ doc .add (new StringField ("fileName" , file .getName (), Field .Store .YES ));
182+ doc .add (new StringField ("fileNameLowerCase" , file .getName ().toLowerCase (), Field .Store .YES ));
183+ doc .add (new StringField ("absolutePath" , file .getAbsolutePath (), Field .Store .YES ));
184+ // doc.add(new DoubleDocValuesField("fileSize", file.length()));
185+ // doc.add(new DoubleDocValuesField("lastModified", file.lastModified()));
186+ 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 ));
129190 indexWriter .updateDocument (new Term ("absolutePath" , file .getAbsolutePath ()), doc );
130191// indexWriter.addDocument(doc);
131192 indexWriter .commit ();
0 commit comments