Create a full-text search reference value for your system: 2)

Source: Internet
Author: User
I mentioned Lucene again in this article. In the Java industry, when it comes to full-text retrieval, almost no one knows about it.
Search by Google. The world is full of related information. The Representative is che Dong's "Java-based full-text indexing engine Lucene Introduction ",
I want to write only the simplest three axes, plus chineseanalyzer that supports Chinese and the method for sorting search results by time.
These materials can be found elsewhere. I just put them forward as a solution to the troubles frequently encountered in the CE application.
Last year, a friend on MSN told me that he wanted to use Lucene to build a full-text search for a website. At that time, I thought it was very simple. It was okay to say it was okay,
However, he mentioned that the search results should be sorted by time. I checked some information and found that Lucene does not provide a custom sorting method,
However, they can only be sorted by their own relevance algorithms. Later, I found indexordersearcher IN THE weblucene project of chelong.
Solved the regular requirements of result sorting.
Indexordersearcher is similar to General indexsearch. You only need to add the indexordersearcher. order_by_docid_desc parameter when constructing an object.
Indexordersearcher indexsearcher = new indexordersearcher ("/home/deleetest/Index", indexordersearcher. order_by_docid_desc );
The new version of Lucene also provides a multifieldqueryparser that can retrieve multiple fields at the same time. Previously, queryparser was troublesome.
Private Static 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 describes how to create an index and regularly retrieve the data index from the database. After the record is completed, I write the time into a TXT file.
Package com. Test. search;

Import org. Apache. Lucene. analysis. analyzer;
Import org.apache.e.analysis.cn .*;
Import org. Apache. Lucene. analysis. Standard. standardanalyzer;
Import org.apache.e.doc ument .*;
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 = new chineseanalyzer ();

Public searchindexer (string s ){
This. indexpath = s;
}
/**
* Index all documents before a certain date
* @ Param fromdate
* @ Return
*/
Public final void updateindex (string fromdate ){
Connection conn = dbutil. getcon ();
Indexwriter = NULL;
Try {
Indexwriter = getwriter (false );
// Index internal system file Publishing
Preparedstatement PSTM = conn. preparestatement (
"Select title, body, creationtime from document where creationtime>'' "+ fromdate +
"''Order by creationtime ");
Resultset rs = p0000.exe cutequery ();
While (Rs. Next ()){
String creationtime = 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 {
Try {
Indexwriter. Close ();
Conn. Close ();
}
Catch (exception e ){
E. printstacktrace ();
}
}
}
/**
* Check whether the index file exists.
* @ Param s
* @ Return indicates whether the index exists.
*/
Private Boolean indexexists (string s ){
File file = new file (s + file. Separator + "segments ");
Return file. exists ();
}
/**
* Add an index group.
* @ Param title
* @ Param body
* @ Param creationtime
* @ Param indexwriter
* @ Return
*/
Private final void addnewstoindex (string docid, string URL, String title, string body,
String ptime, indexwriter) throws
Ioexception {
If (indexwriter = NULL ){
Return;
}
Else {
Try {
Document document = new document ();
Document. Add (field. Text ("title", title ));
Document. Add (field. Text ("body", body ));
Document. Add (new field ("creationtime", creationtime, true, true, false ));
Indexwriter. adddocument (document );
}
Catch (exception ex ){
Ex. printstacktrace ();
}
Return;
}
}
/**
* Get indexwriter
* @ Param flag whether to create an 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 = NULL;
If (FLAG ){
Try {
Indexwriter = new indexwriter (indexpath, analyzer, true );
}
Catch (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 .");
Prediction2.printstacktrace ();
}
}
}
Return indexwriter;
}

Public static void main (string [] ARGs ){
String lastupdate = "/home/lucenetest/lastupdate.txt ";
Searchindexer = new searchindexer ("/home/lucenetest/Index ");
// Retrieve the last update time
String STR = util. readtxtfile (lastupdate );
If (STR = NULL | Str. Length () = 0 ){
STR = new java. util. Date (). tostring ();
}
Searchindexer. updateindex (STR );
// Current write time
Util. writetxtfile (lastupdate, new java. util. Date (), false );
}
}
Write a cmd or sh command to regularly execute searchindexer in the corresponding operating system.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.