Lucene2.4 has been modified in many places, probably to welcome the coming of lucene3.0.
The previously written blog (http://blog.csdn.net/kunshan_shenbin/archive/2008/06/02/2503388.aspx) passed the test in version 2.3.2, but must be modified under version 2.4.
The Code is as follows:
Indexer. Java
- Package com. Lucene. index;
- Import java. Io. file;
- Import java. Io. filereader;
- Import java. Io. ioexception;
- 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;
- Public class indexer {
- Public static void main (string [] ARGs) throws ioexception {
- File indexdir = new file ("C: // test // Index ");
- File datadir = new file ("C: // test // data ");
- Int numindexed = index (indexdir, datadir );
- System. Out. println (numindexed );
- }
- Public static int index (File indexdir, file datadir) throws ioexception {
- If (! Indexdir. exists () |! Datadir. isdirectory ()){
- Throw new ioexception ();
- }
- Indexwriter writer = new indexwriter (indexdir, new standardanalyzer (), true, Indexwriter. maxfieldlength. Limited);
- Writer. setusecompoundfile (false );
- Indexdirectory (writer, datadir );
- Int numindexed =Writer. maxdoc ();
- Writer. Optimize ();
- Writer. Close ();
- Return numindexed;
- }
- Private Static void indexdirectory (indexwriter writer, file DIR)
- Throws ioexception {
- File [] files = dir. listfiles ();
- For (INT I = 0; I <files. length; I ++ ){
- File F = files [I];
- If (F. isdirectory ()){
- Indexdirectory (writer, F );
- } Else if (F. getname (). tolowercase (). endswith (". txt ")){
- Indexfile (writer, F );
- }
- }
- }
- Public static void indexfile (indexwriter writer, file F) throws ioexception {
- If (F. ishidden () |! F. exists () |! F. Canread ()){
- Return;
- }
- System. Out. println ("Indexing" + F. GetCanonicalPath ());
- Document Doc = new document ();
- Doc. Add (new field ("FILENAME", F. GetCanonicalPath (), field. Store. Yes,
- Field. Index. not_analyzed));
- Doc. Add (new field ("contents", new filereader (F )));
- Writer. adddocument (DOC );
- }
- }
Searcher. Java
- Package com. Lucene. search;
- Import java. Io. file;
- Import java. Io. ioexception;
- Import org. Apache. Lucene. analysis. Standard. standardanalyzer;
- Import org.apache.e.doc ument. Document;
- Import org. Apache. Lucene. queryparser. queryparser;
- Import org. Apache. Lucene. Search. indexsearcher;
- Import org. Apache. Lucene. Search. query;
- Import org. Apache. Lucene. Search. scoredoc;
- Import org. Apache. Lucene. Search. topdocs;
- Import org. Apache. Lucene. Store. Directory;
- Import org. Apache. Lucene. Store. fsdirectory;
- Public class searcher {
- Public static void main (string [] ARGs) throws exception {
- File indexdir = new file ("C: // test // Index ");
- String q = "111 ";
- If (! Indexdir. exists () |! Indexdir. isdirectory ()){
- Throw new ioexception ();
- }
- Search (indexdir, q );
- }
- Public static void search (File indexdir, string q) throws exception {
- Directory fsdir = fsdirectory. getdirectory (indexdir );
- Indexsearcher searcher = new indexsearcher (fsdir );
- Queryparser parser = new queryparser ("contents", new standardanalyzer ());
- Query query = parser. parse (Q );
- Topdocs = searcher. Search (query,100); // 100 indicates the size of the queue.
- Scoredoc [] hits = topdocs. scoredocs;
- System. Out. println ("Total" + searcher. maxdoc () + "index, hit" + hits. Length + "entry ");
- For (INT I = 0; I
- Int docid = hitspolici2.16.doc;
- Document document = searcher.doc (docid );
- System. Out. println (docid + ":" + document. Get ("FILENAME "));
- }
- }
- }