Lucene is a good API for full-text search. It can be used in combination with Java. However, when using Lucene 2.0, if you refer to some of the current articles, some APIs may have expired, when I was reading some Lucene articles, I encountered this kind of situation. So after searching, I found the following points of attention.
Field. Text (Java. Lang. String, java. Io. Reader)
UseNew field (Java. Lang. String, java. Io. Reader)
Field. Keyword (Java. Lang. String, java. Lang. String)
Use
Field. Keyword (Java. Lang. String, java. Lang. String)
Replace
Query query = queryparser. parse (Q, "contents", new standardanalyzer ());
Use
Queryparser parser = new queryparser ("contents", new standardanalyzer ());
Query query = parser. parse (Q );
Replace
Therefore, extract a simple Lucene program from ibm dw in the command line mode. The result is to index the TXT files in a directory and then sort them.
Procedure for creating an index:
Import java. Io. file;
Import java. Io. filereader;
Import java. Io. reader;
Import java. util. date;
Import org. Apache. Lucene. analysis. analyzer;
Import org. Apache. Lucene. analysis. Standard. standardanalyzer;
Import org.apache.e.doc ument. Document;
Import org.apache.e.doc ument. field;
Import org. Apache. Lucene. Index. indexwriter;
/**
* This class demonstrate the process of creating index with Lucene
* For text files
*/
Public class txtfileindexer {
Public static void main (string [] ARGs) throws exception {
// Indexdir is the directory that hosts Lucene's index files
File indexdir = new file ("D: \ javaseindex ");
// Datadir is the directory that hosts the text files that to be indexed
File datadir = new file ("D: \ lucenedata ");
Analyzer luceneanalyzer = new standardanalyzer ();
File [] datafiles = datadir. listfiles ();
Indexwriter = new indexwriter (indexdir, luceneanalyzer, true );
Long starttime = new date (). gettime ();
For (INT I = 0; I <datafiles. length; I ++ ){
If (datafiles [I]. isfile () & datafiles [I]. getname (). endswith (". txt ")){
System. Out. println ("indexing file" + datafiles [I]. GetCanonicalPath ());
Document document = new document ();
Reader txtreader = new filereader (datafiles [I]);
Document. Add (new field ("path", datafiles [I]. getpath (), field. Store. Yes, field. Index. No ));
Document. Add (new field ("contents", txtreader ));
Indexwriter. adddocument (document );
}
}
Indexwriter. Optimize ();
Indexwriter. Close ();
Long endtime = new date (). gettime ();
System. Out. println ("it takes" + (endtime-starttime)
+ "Milliseconds to create index for the files in directory"
+ Datadir. getpath ());
}
}
Search program
Import java. Io. file;
Import org.apache.e.doc ument. Document;
Import org. Apache. Lucene. Index. term;
Import org. Apache. Lucene. Search. Hits;
Import org. Apache. Lucene. Search. indexsearcher;
Import org. Apache. Lucene. Search. termquery;
Import org. Apache. Lucene. Store. fsdirectory;
/**
* This class is used to demonstrate
* Process of searching on an existing
* Lucene Index
*
*/
Public class txtfilesearcher {
Public static void main (string [] ARGs) throws exception {
String querystr = "I ";
// This is the directory that hosts the Lucene Index
File indexdir = new file ("D: \ javaseindex ");
Fsdirectory directory = fsdirectory. getdirectory (indexdir, false );
Indexsearcher searcher = new indexsearcher (directory );
If (! Indexdir. exists ()){
System. Out. println ("The Lucene index is not exist ");
Return;
}
Term term = new term ("contents", querystr. tolowercase ());
Termquery limit equery = new termquery (TERM );
Hits hits = searcher. Search (elastic equery );
System. Out. println ("His result is" + hits. Length ());
For (INT I = 0; I Document document = hits.doc (I );
System. Out. println ("file:" + document. Get ("path "));
}
}
}