📄 applicationsearcher.java
字号:
package net.java.workeffort.webapp.support;import java.io.File;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import net.java.workeffort.searchengine.CustomQueryParser;import net.java.workeffort.service.domain.PageResult;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.search.Hits;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.Searcher;import org.apache.lucene.store.FSDirectory;import org.springframework.context.ApplicationContext;import org.springframework.core.io.Resource;/** * Application help searcher using Lucene * @author Antony Joseph */public class ApplicationSearcher { private ApplicationContext context; public ApplicationSearcher(ApplicationContext context) { this.context = context; } public List search(HelpSearchQuery query) { List list = new ArrayList(); // if no query entered return empty list. if (query.getQuery() == null || query.getQuery().length() == 0) { return list; } try { Resource resource = context.getResource("classpath:searchindex"); File indexDir = resource.getFile(); Searcher searcher = new IndexSearcher(FSDirectory.getDirectory( indexDir, false)); String[] fields = { "title", "body" }; Hits hits = searcher.search(CustomQueryParser.parse(query .getQuery(), fields, new StandardAnalyzer())); for (int i=0; i < hits.length(); i++) { Document doc = hits.doc(i); Map map = new HashMap(); map.put("path", doc.get("path")); map.put("title", doc.get("title")); map.put("body", doc.get("body")); list.add(map); // System.out.println("title=" + doc.get("title") + "\n body=" // + doc.get("body") + "\n path=" + doc.get("path")); } } catch (Exception e) { throw new RuntimeException("Exception in search()", e); } return list; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -