多线程索引

package cn.ac.ict.LuceneBean; import java.io.File; import java.io.StringReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.Iterator; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.MultiReader; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.Hits; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.Sort; import org.apache.lucene.search.SortField; import org.apache.lucene.search.highlight.Highlighter; import org.apache.lucene.search.highlight.QueryScorer; import org.apache.lucene.search.highlight.SimpleFragmenter; import org.apache.lucene.search.highlight.SimpleHTMLFormatter; public class MainSearcher { public static ArrayList<FullTextShow> SearcherResults( boolean timeSensitive, String field, String keyWord, String requestPage) throws Exception { String indexFolder = "D:/QiBaoyuan/MYEclipsePRJ/lucene/"; File file = new File(indexFolder); ArrayList<String> index_list_folder = new ArrayList<String>(); if (file.isDirectory()) { String[] files = file.list(); // list its files Arrays.sort(files); // sort the files for (int i = 0; i < files.length; i++) { System.out.println(files[i]); if (files[i].length() < 4) continue; if (files[i].substring(0, 5).equalsIgnoreCase("index")) { index_list_folder.add(files[i]); } } } IndexReader[] ir_array = new IndexReader[index_list_folder.size()]; for (int i = 0; i < index_list_folder.size(); i++) { System.out.println("in:" + index_list_folder.get(i)); ir_array[i] = IndexReader.open(indexFolder + index_list_folder.get(i)); } IndexSearcher searcher = null; // the searcher used to open/search the // index MultiReader reader = new MultiReader(ir_array); Query query = null; // the Query created by the QueryParser Hits hits = null; // the search results // String queryString = ""; boolean error = false; int int_requestPage = 0;// /传递过来的page int currentPage = 1;// /当前页面 int iPage = 20;// //每页的记录数 int allPages = 0;// /总的页数 Date start = new Date(); Analyzer analyzer = new StandardAnalyzer(); // construct our usual // analyzer ArrayList<FullTextShow> text = new ArrayList<FullTextShow>(); FullTextShow fts = new FullTextShow(); ArrayList<ShowText> stList = new ArrayList<ShowText>(); // /排序,按时间 Sort sort = null; if (timeSensitive == true) { sort = new Sort(new SortField[] { new SortField("modified", SortField.STRING, true) }); } searcher = new IndexSearcher(reader); if (error == false) {// /没有发生错误 // queryString = request.getParameter("keyword"); // if (request.getParameter("dr") != null) { // queryString = chi.codeToString(queryString); // } System.out.print("queryString:" + keyWord); if (requestPage == null) {// /确定请求的页面 int_requestPage = 1; } else { int_requestPage = Integer.parseInt(requestPage); } if (keyWord == "") {// /若没有输入查询内容,则回到查询出使界面 // response.sendRedirect("index.jsp"); } // /检索contens域,并解析 QueryParser qp = new QueryParser(field, analyzer); query = qp.parse(keyWord); // parse the } if (error == false && searcher != null) {// /未出错且有searcher if (timeSensitive == true) hits = searcher.search(query, sort); // run the query else hits = searcher.search(query); // run the query Date end = new Date(); // //总页数 allPages = (int) Math.ceil((hits.length() + iPage - 1) / iPage); // 判断当前页 if (int_requestPage > allPages || int_requestPage == 0) { currentPage = 1; } else { currentPage = int_requestPage; } fts.setCount(hits.length()); System.out.print("时间:" + (end.getTime() - start.getTime()) + "毫秒"); ShowText st = new ShowText(); fts.setSearChTime(Long.toString(end.getTime() - start.getTime())); for (int i = (int_requestPage - 1) * iPage; i < int_requestPage * iPage - 1; i++) {// /输出 if (i >= hits.length()) break; SimpleHTMLFormatter sHtmlF = new SimpleHTMLFormatter( "<font color='red'>", "</font>"); Highlighter highlighter = new Highlighter(sHtmlF, new QueryScorer(query)); highlighter.setTextFragmenter(new SimpleFragmenter(600)); Document doc = hits.doc(i); // get the next document String doctitle = doc.get("title"); // get its title String url = doc.get("path"); // get its path field url = EntityBean.UrlUtility(url, "D:/News/"); if (url != null && url.startsWith("../webapps/")) { // strip off // ../webapps prefix // if present url = url.substring(10); } st.setUrl(url); if ((doctitle == null) || doctitle.equals("")) // use the path // if it // has no title doctitle = url; String strTitle = ""; if (doctitle != null) { TokenStream tokenStream = analyzer.tokenStream("title", new StringReader(doctitle)); strTitle = highlighter.getBestFragment(tokenStream, doctitle); } if (strTitle == null) strTitle = doctitle; st.setTitle(strTitle); String summary = doc.get("summary"); String result = ""; if (summary != null) { TokenStream tokenStream = analyzer.tokenStream("summary", new StringReader(summary)); result = highlighter.getBestFragment(tokenStream, summary); } if (result == null) result = summary; st.setSummary(result); if (url.length() >= 50) url = url.substring(0, 50) + "..."; st.setShowUrl(url); st.setSize(doc.get("size")); st.setTime(doc.get("modified")); stList.add(st); st = new ShowText(); } fts.setList(stList); // /out.print("总页数:" + allPages); // out.print("当前页:" + currentPage); String pageShow = ""; if (int_requestPage > 1) { pageShow += "<a href="results.jsp?dr=6&keyword=" + keyWord + "&requestPage=" + (currentPage - 1) + "" mce_href="results.jsp?dr=6&keyword=" + keyWord + "&requestPage=" + (currentPage - 1) + "">上一页</a>"; } if (currentPage >= 6) { for (int i = currentPage - 5; i <= currentPage + 5; i++) { if (i > allPages) break; if (i != int_requestPage) { pageShow += "<a href="results.jsp?dr=6&keyword=" + keyWord + "&requestPage=" + i + "" mce_href="results.jsp?dr=6&keyword=" + keyWord + "&requestPage=" + i + ""> [" + i + "] </a>"; } else { pageShow += currentPage; } } } else { for (int i = 1; i <= currentPage + 9; i++) { if (i > allPages) break; if (i != int_requestPage) { pageShow += "<a href="results.jsp?dr=6&keyword=" + keyWord + "&requestPage=" + i + "" mce_href="results.jsp?dr=6&keyword=" + keyWord + "&requestPage=" + i + ""> [" + i + "] </a>"; } else { pageShow += currentPage; } } } if (int_requestPage < allPages) { pageShow += "<a href="results.jsp?dr=6&keyword=" + keyWord + "&requestPage=" + (currentPage + 1) + "" mce_href="results.jsp?dr=6&keyword=" + keyWord + "&requestPage=" + (currentPage + 1) + "">下一页</a>"; } fts.setPageInfo(pageShow); text.add(fts); } System.out.println("size:" + text.size()); if (searcher != null) searcher.close(); return text; } /** * @param args */ public static void main(String[] args) throws Exception { ArrayList<FullTextShow> text = new ArrayList<FullTextShow>(); text = SearcherResults(true, "contents", "新闻", "1"); Iterator<FullTextShow> it = text.iterator(); while (it.hasNext()) { FullTextShow st = it.next(); System.out.println("title:" + st.getSearChTime()); ArrayList<ShowText> st_list = new ArrayList<ShowText>(); System.out.println("info:" + st.getPageInfo()); } } }

 

你可能感兴趣的:(多线程,exception,list,String,null,query)