Notes when using javase2.4 (changes to the new version)

Source: Internet
Author: User

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

  1. Package com. Lucene. index;
  2. Import java. Io. file;
  3. Import java. Io. filereader;
  4. Import java. Io. ioexception;
  5. Import org. Apache. Lucene. analysis. Standard. standardanalyzer;
  6. Import org.apache.e.doc ument. Document;
  7. Import org.apache.e.doc ument. field;
  8. Import org. Apache. Lucene. Index. indexwriter;
  9. Public class indexer {
  10. Public static void main (string [] ARGs) throws ioexception {
  11. File indexdir = new file ("C: // test // Index ");
  12. File datadir = new file ("C: // test // data ");
  13. Int numindexed = index (indexdir, datadir );
  14. System. Out. println (numindexed );
  15. }
  16. Public static int index (File indexdir, file datadir) throws ioexception {
  17. If (! Indexdir. exists () |! Datadir. isdirectory ()){
  18. Throw new ioexception ();
  19. }
  20. Indexwriter writer = new indexwriter (indexdir, new standardanalyzer (), true, Indexwriter. maxfieldlength. Limited);
  21. Writer. setusecompoundfile (false );
  22. Indexdirectory (writer, datadir );
  23. Int numindexed =Writer. maxdoc ();
  24. Writer. Optimize ();
  25. Writer. Close ();
  26. Return numindexed;
  27. }
  28. Private Static void indexdirectory (indexwriter writer, file DIR)
  29. Throws ioexception {
  30. File [] files = dir. listfiles ();
  31. For (INT I = 0; I <files. length; I ++ ){
  32. File F = files [I];
  33. If (F. isdirectory ()){
  34. Indexdirectory (writer, F );
  35. } Else if (F. getname (). tolowercase (). endswith (". txt ")){
  36. Indexfile (writer, F );
  37. }
  38. }
  39. }
  40. Public static void indexfile (indexwriter writer, file F) throws ioexception {
  41. If (F. ishidden () |! F. exists () |! F. Canread ()){
  42. Return;
  43. }
  44. System. Out. println ("Indexing" + F. GetCanonicalPath ());
  45. Document Doc = new document ();
  46. Doc. Add (new field ("FILENAME", F. GetCanonicalPath (), field. Store. Yes,
  47. Field. Index. not_analyzed));
  48. Doc. Add (new field ("contents", new filereader (F )));
  49. Writer. adddocument (DOC );
  50. }
  51. }

Searcher. Java

  1. Package com. Lucene. search;
  2. Import java. Io. file;
  3. Import java. Io. ioexception;
  4. Import org. Apache. Lucene. analysis. Standard. standardanalyzer;
  5. Import org.apache.e.doc ument. Document;
  6. Import org. Apache. Lucene. queryparser. queryparser;
  7. Import org. Apache. Lucene. Search. indexsearcher;
  8. Import org. Apache. Lucene. Search. query;
  9. Import org. Apache. Lucene. Search. scoredoc;
  10. Import org. Apache. Lucene. Search. topdocs;
  11. Import org. Apache. Lucene. Store. Directory;
  12. Import org. Apache. Lucene. Store. fsdirectory;
  13. Public class searcher {
  14. Public static void main (string [] ARGs) throws exception {
  15. File indexdir = new file ("C: // test // Index ");
  16. String q = "111 ";
  17. If (! Indexdir. exists () |! Indexdir. isdirectory ()){
  18. Throw new ioexception ();
  19. }
  20. Search (indexdir, q );
  21. }
  22. Public static void search (File indexdir, string q) throws exception {
  23. Directory fsdir = fsdirectory. getdirectory (indexdir );
  24. Indexsearcher searcher = new indexsearcher (fsdir );
  25. Queryparser parser = new queryparser ("contents", new standardanalyzer ());
  26. Query query = parser. parse (Q );
  27. Topdocs = searcher. Search (query,100); // 100 indicates the size of the queue.
  28. Scoredoc [] hits = topdocs. scoredocs;
  29. System. Out. println ("Total" + searcher. maxdoc () + "index, hit" + hits. Length + "entry ");
  30. For (INT I = 0; I
  31. Int docid = hitspolici2.16.doc;
  32. Document document = searcher.doc (docid );
  33. System. Out. println (docid + ":" + document. Get ("FILENAME "));
  34. }
  35. }
  36. }

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.