在我的 Webmethods 应用程序中,我需要实现一个搜索功能,并且我已经使用 Lucene 完成了它。但是,当我搜索标题以 alpabet.for 以外的文件结尾的文件时,搜索不会检索结果,例如:- doc1.txt 或 new$.txt
在下面的代码中,当我尝试打印 queryCmbd时,它的打印搜索结果>> >>>>>title:"doc1 txt" (contents:doc1 contents:txt)。当我搜索像 doc.txt 这样的字符串时,结果是 Search Results>>>>>>>title:"doc.txt"内容:doc.txt。为了解析这些类型的字符串(如 doc1.txt、new$.txt)应该怎么做?
public java.util.ArrayList<DocNames> searchIndex(String querystr,
String path, StandardAnalyzer analyzer) {
String FIELD_CONTENTS = "contents";
String FIELD_TITLE = "title";
String queryStringCmbd = null;
queryStringCmbd = new String();
String queryFinal = new String(querystr.replaceAll(" ", " AND "));
queryStringCmbd = FIELD_TITLE + ":\"" + queryFinal + "\" OR "
+ queryFinal;
try {
FSDirectory directory = FSDirectory.open(new File(path));
Query q = new QueryParser(Version.LUCENE_36, FIELD_CONTENTS,
analyzer).parse(querystr);
Query queryCmbd = new QueryParser(Version.LUCENE_36,
FIELD_CONTENTS, analyzer).parse(queryStringCmbd);
int hitsPerPage = 10;
IndexReader indexReader = IndexReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
TopScoreDocCollector collector = TopScoreDocCollector.create(
hitsPerPage, true);
indexSearcher.search(queryCmbd, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
System.out
.println("Search Results>>>>>>>>>>>>"
+ queryCmbd);
docNames = new ArrayList<DocNames>();
for (int i = 0; i < hits.length; ++i) {
int docId = hits[i].doc;
Document d = indexSearcher.doc(docId);
DocNames doc = new DocNames();
doc.setIndex(i + 1);
doc.setDocName(d.get("title"));
doc.setDocPath(d.get("path"));
if (!(d.get("path").contains("indexDirectory"))) {
docNames.add(doc);
}
}
indexReader.flush();
indexReader.close();
indexSearcher.close();
return docNames;
} catch (CorruptIndexException e) {
closeIndex(analyzer);
e.printStackTrace();
return null;
} catch (IOException e) {
closeIndex(analyzer);
e.printStackTrace();
return null;
} catch (ParseException e) {
closeIndex(analyzer);
e.printStackTrace();
return null;
}
}