Make a full-text search for your own system

zhaozj2021-02-11  192

In this article, I mentioned Lucene, in the Java industry, mentioned the full-text search, almost no one knows it. Search with Google, the world is relevant. Representative is the "Java-based full-text index engine lucene" in the car. I want to write only the simplest three-ax ax, and then support Chinese ChineseAlyzer and sorted by time sorting. These can find relevant information elsewhere, I just put them out, as a hassle solution that is often encountered in Lucence applications. Last year, I had a friend who mentioned me to I hope to build a website with Lucene. I felt very simple. I didn't have any problems. However, he mentioned a requirement to install time sorting, I check Some information, found that Lucene does not provide user-defined sorting methods, but only in accordance with their own correlation algorithm. Later I found indexordersearcher in the WEBLUCENE project in the car. Solved the results sorted regular needs. IndexOrderSearcher with the average IndexSearch use almost exclusively when building one more object parameter IndexOrderSearcher.ORDER_BY_DOCID_DESCIndexOrderSearcher indexsearcher = new IndexOrderSearcher ( "/ home / lucenetest / index", IndexOrderSearcher.ORDER_BY_DOCID_DESC); lucene new version also offers a MultiFieldQueryParser, can at the same time Retrieve multiple fields, before queryparser is more troublesome. private static ChineseAnalyzer chineseAnalyzer = new ChineseAnalyzer (); public Hits search (String queryText) {if (queryText == null) {return null;} Query query; try {query = MultiFieldQueryParser.parse (queryText, new String [] { "title "}, chineseanalyzer; return indexsearcher.search (query);} catch (Exception e) {return null;}} The following is to build an index, take out the data index from the database, completed the completion time, I am writing time A TXT file. Package com.test.search;

import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.cn *;. import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document *;. import org .apache.lucene.index. *;

Import java.io. *; import java.sql. *; import java.util.date;

Import com.test.db. *; import com.test.utility. *;

/ ** * Title: SearchIndexer * Description: full-text index * Copyright: Copyright (c) 2001 * Company: test * @author Sean * @version 1.0 * / public class SearchIndexer {private String indexPath = null; protected Analyzer analyzer = new ChineseAnalyzer (); public searchindexer (string s) {this.indexpath = s;} / ** * All documents before index * @Param fromDate * @return * / public final void updateIndex (String fromDate) {connection conn = dButil .getCon (); IndexWriter indexWriter = null; try {indexWriter = getWriter (false); // index distribution system internal file PreparedStatement pstm = conn.prepareStatement ( "select title, body, creationtime from document where creationtime> '" fromdate "'Order By CreationTime"; ResultSet RS = PSTM.ExecuteQuery (); while (rs.next ()) {string cree = rs.getstring ("creationtime"); string title = rs.getstring ("title"); String body = rs.getstring ("body");

IF (title == null || body == null) {Continue;} Try {adddocstoIndex (Title, Body, CreationTime, IndexWriter);} catch (exception ex) {EX.PrintStackTrace ();}} indexwriter.Optimize () } Catch (exception ex) {ex.printstacktrace ();} finally {type (); conn.close (); catch (exception e) {E.PrintStackTrace ();}}} / ** * Check if the index file exists * @Param s * @return index exists * / private bolean indexexists (String s) {file file = new file (s file.separator "segments"); return file.exists (); } / ** * adding a set of index * @param title * @param body * @param creationtime * @param indexwriter * @return * / private final void addNewsToIndex (String docid, String url, String title, String body, String ptime, Indexwriter indexwriter) THROWS IOEXCEPTION {IF (IndexWriter == Null) {Return;} elocument = new document (); document.add ("title", title); Document.Add (Field.Text ("Body", Body); document. Add ("CreationTime", True, True, False); IndexWriter.addDocument (Document);} Catch (Exception EX) {EX.PrintStackTrace ();} return;}} / ** * acquired indexwriter * @param flag whether a new index * @return IndexWriter * / private IndexWriter getWriter (boolean flag) throws IOException {String s = indexPath; if (s == null) {throw new IOException ( "index file path setting error.");

} IndexPath = s File.separator "search"; IndexWriter indexwriter = null; if (flag) {try {indexwriter = new IndexWriter (indexPath, analyzer, true);} catch (Exception exception) {System.err.println ( "ERROR: Failed to create a new index writer."); exception.printStackTrace ();}} else {if (indexExists (indexPath)) {try {indexwriter = new IndexWriter (indexPath, analyzer, false);} catch (Exception exception1) {System.err.println ( "ERROR:. Failed to open an index writer"); exception1.printStackTrace ();}} else {try {indexwriter = new IndexWriter (indexPath, analyzer, true);} catch (Exception exception2) {System.err.println ( "ERROR:. Failed to create a new index writer"); exception2.printStackTrace ();}}} return indexwriter;} public static void main (String [] args) {String lastUpdate = "/Home/lucenetest/lastupdate.txt"; SearchIndexer Searchindexer = New Searchindexer ("/ Home / Lucenetest / Index"); // Remove the last update time str = util.readtxtFile (LastUpdate); if (str == null || Str.Length () == 0) {str = new java.util.date (). toString ();} searchendexer.UpdateIndex (STR); // Write the current time Util.writetXTFile (Lastupdate, New Java. Util.date (), false;}} Write a CMD or SH to perform SearchIndexer in the appropriate operating system.

Download Lucene Package

转载请注明原文地址:https://www.9cbs.com/read-5787.html

New Post(0)