Package com.highcom.hcgip.lucenesearch;
Import javax.servlet. *; import javax.servlet.http. *; import java.io. *; import java.util. *; import com.highcom.hcgip.cm.deal.admin. *; import com.highcom. hcgip.cm.dbmap.admin. *; import com.highcom.hcgip.cm.model. *; import com.highcom.hcgip.cm.util. *; import com.highcom.hcgip.basic.common. *; import com.highcom.hcgip.cm.constant. *;
/ ** *
Title: p> *
Description: p> *
Copyright: Copyright (c) 2004 p> *
company: highcom p> * @Author Li Xinbo * @version 1.0 * /
Public class searcher extends httpservlet {private static final string content_type = "text / html; charSet = GBK"; // initialize global variables public void infit () throws servletexception {}
// Process the HTTP Post request public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {PrintWriter out = response.getWriter (); String id = request.getParameter ( "classid"); String q = request.getParameter ( "searchkeys"); Q = uncode.exChinese (Q); categorydbinterface category = new categorydbmap (); if (id! = null && id.equals ("0")) {Date start = new date (); vector v = Category.getcategory (); string [] indexdir = new string [v.size ()]; categoryBean Tempbean = null; for (int i = 0; i currentPageIndex ", new Integer (currentPageIndex)); temp = ph.getPage (currentPageIndex); query request.setAttribute (after // Page" result ", temp); request.setAttribute (" searchfor "," All Categories ") Request.setttribute ("Time", T); Request.setttribute ("SearchKeys", Q); Request.SetAttribute ("ClassID", ID);} else {date start = new date (); Vector v = category. Getcategory (INTEGER.PARSEINT (ID)); categoryBean Tempbean = (categorybean) v.get (0); Vector it = search.getsearch (TempBean.GetIndexpath (); Date End = new date (); long time = End.gettime () - start.gettime (); string t = time ""; // paging parameter int pageCounter = 0; int currentpageIndex = 0; string pageIndex = (Request.getParameter ("pageindex") == null) ? "0": Request.getParameter ("pageindex"); currentpageindex = integer.parseint (pageindex); int totalRecord = 0; // Page End Vector TE mp = new Vector (); SplitPage ph = new SplitPage (); ph.setPage (it, 10, currentPageIndex); pageCounter = ph.getTotalPages (); totalRecord = ph.getTotalLines (); request.setAttribute ( "totalRecord", new Integer (totalRecord)); request.setAttribute ( "pageCounter", new Integer (pageCounter)); request.setAttribute ( "currentPageIndex", new Integer (currentPageIndex)); temp = ph.getPage (currentPageIndex); // paginated Query Request.setttribute ("Result", Temp); Request.SetAttribute (" Searchfor ", TempBean.getcategory ()); Request.setttribute (" Time ", T); Request.setttribute (" SearchKeys ", Q); Request.SetAttribute (" ClassID ", ID);} DISPATCH (Request, Response, Constantlist.page_basic_searchresult); protected void dispatch (HttpServletRequest request, HttpServletResponse response, String page) throws javax.servlet.ServletException, IOException {RequestDispatcher dispatcher = getServletContext () getRequestDispatcher (page);. dispatcher.forward (request, response);} // Clean Up Resources Public Void Destroy () {}} Package com.highcom.hcgip.lucenesearch; import java.io.IOException; import java.util *;. import java.io.BufferedReader; import java.io.InputStreamReader; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.cn. *; import org.apache.lucene.document.Document; import org.apache.lucene.search.Searcher; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache .lucene.search.hits; import org.apache.lulentne.QueryParser.QueryParser; import org.apache.lucene.Analysis.cjk. *; Import com.highcom.hcgip.basic.common. *; import org.apache.lucene .queryParser. *; import org.apache.lucene.search. *; import java.io. *; Public class search { Public static vector getsearch (string indexdir, string {hits hits = null; vector it = new vector (); file segments = new file (indexdir file.seParetor "segments"); if (segments.exists ()) {Try {Searcher Searcher = new indexsearcher (indexdir); // Build a searcher, and point to Index Directory Analyzer Analyzer = new cjkanalyzer (); // Build language analyzer query contentsQuery = queryParser.Parse (Q, "Contents", Analyzer ); // Query parser: Use and index the same language analyzer Standardanalyzer Query ProposalQuery = queryParser.Parse (Q, "Proposal", Analyzer; BooleanQuery ComboQuery = new booleanQuery (); ComboQuery.Add (Contentsquery, False, False ComboQuery.Add (ProposalQuery, False, False); Hits = Searcher.Search (ComboQuery); // Search results Using HITS storage, similar to the database returned record set for (INT i = 0; i Public static vector getsearch (string [] indexdir, string {hits hits = null; vector it = new vector (); analyzer analyzer = new cjkanalyzer (); // Build language analyzer try {for (int J = 0; J Vector it = search.getSearch ("D: // BB", "Request"); LuceneBean LuceneBean = new LuceneBean (); system.out.println (it.size ()); if (it! = Null && it. Size ()> 0) {for (int i = 0; i } Package com.highcom.hcgip.lucenesearch; / ** * Title: p> * Description: p> * Copyright: Copyright (c) 2004 p> * company: highcom p> * @Author Li Xinbo * @version 1.0 * / Public class lucenebean { private String path; private String title; private String date; private String type; private String realpath; public LuceneBean () {} public String getPath () {return path;} public void setPath (String path) {this.path = path; PUBLIC STRING GETTITLE () {Return Title;} PUBLIC VOID SETLE () {this.title = Title;} public string getdate () {returnate;} public void setdate (String Date) {this.date = date } public String getType () {return type;} public void setType (String type) {this.type = type;} public String getRealpath () {return realpath;} public void setRealpath (String realpath) {this.realpath = realpath; } Package com.highcom.hcgip.lucenesearch; Import org.apache.lucene.Analysis.cn. *; import org.apache.lucene.index.indexwriter; import java.io.file; import java.io.reader; import java.io.fileinputstream; import java.io. BufferedReader; import java.io.InputStreamReader; import java.util.Date; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.DateField; import Org.apache.lucene.Analysis.cjk. *; import java.io. *; import com.highcom.hcgip.basic.common. *; import com.highcom.hcgip.cm.util. *; import Org.htmlParser. Beans. *; / ** * Establish index files * Title: p> * Description: p> * Copyright: CopyRight (C) 2004 P> * Company: Highcom p> * @Author Li Xinbo * @version 1.0 * / public class indexrunner {/ ** * Create an index file * @Param filepath To create an origin of the index file, or a folder * @Param indexdir index file Saved Directory * @Param ShowPath Retrieved Path * @Param It Boolean Type, Have Added Index * / Public Static Synchronized Void CreateIndex (String Filepath, String Indexdir, String Showpath, String Type, Boole An it) {indexwriter Writer = null; Try {Writer = New Indexwriter (IndexDir, New Cjkanalyzer (), IT); Indexdocs (Writer, New File (File (FilePath), ShowPath, Type); // Data Source, File Formworthy. Optimize (); Writer.close ();} catch (ioException ex) {log.debug ("public static void create" ());}} Private Static Void Indexdocs (IndexWriter Writer, File File, String Type {if (file.Indirectory ()) {string [] files = file.list (); for (int i = 0; i } Catch (ioException ex) {log.debug ("private static void indexdocs ==" ex. TString ());}}}} private static Document Document (File f, String showPath, String type) {Document doc = null; try {doc = null; doc = new Document (); // Create a data format accepted lucene Document doc.add (Field.UnIndexed ( "realpath", f.getPath ())))); // Create a field name called Path, does not index, only stored, doc.add (Field.unIndexed ("Path", showpath f.getname ()))); // Create a field name called path, not index, store, doc.add ("Date", DateFormat.DateFormat2 (new Date ()))); doc.add ("Type ", TYPE)); doc.add (Field.Text (" Title ", F.GetName (). Split (" _ ") [0])); Doc.Add (Field.Text (" Proposal ", PropositionParser. GetProposition (f.getPath ())))))))))))); /// fileinputstream is = new fileinputstream (f); // reader reader = new bufferedreader (New InputStreamReader (IS)); //doc.add(field.Text( "in" ", reader); // Creating a field name to contents to save the contents of the index StringBean SB = new stringBean (); sb.setlinks (false); sb.setCollapse (true); sb.seturl (f. GetPath ()); sb.setreplacenonbreakingspaces (true); doc.add (Field.Text ("Conten TS ", sb.getstrings ())));} catch (exception ex) {log.debug (" private static document Document == " EX.TOSTRING ());} Return Doc;}}