Small solution for Lucene instances.

Source: Internet
Author: User

1. Create a folder named S on drive C in windows, and create three TXT files in the folder, named "1.txt ", "2.txt" and" 3.txt"
The content of 1.txt is as follows:

Java code
  1. People's Republic of China
  2. People all over China
  3. 2006

The content of "2.txt" and" 3.txt" can also be written in a few minutes. If you are writing it in a lazy way, just copy the content of a 1.txt file.

 

2. Download The Lucene package and put it in the classpath path.
Index creation:

Java code
  1. Package lighter.javaeye.com;
  2. Import java. Io. bufferedreader;
  3. Import java. Io. file;
  4. Import java. Io. fileinputstream;
  5. Import java. Io. ioexception;
  6. Import java. Io. inputstreamreader;
  7. Import java. util. date;
  8. Import org. Apache. Lucene. analysis. analyzer;
  9. Import org. Apache. Lucene. analysis. Standard. standardanalyzer;
  10. Import org.apache.e.doc ument. Document;
  11. Import org.apache.e.doc ument. field;
  12. Import org. Apache. Lucene. Index. indexwriter;
  13. /**
  14. * Author lighter date 2006-8-7
  15. */
  16. Public class textfileindexer {
  17. Public static void main (string [] ARGs) throws exception {
  18. /* Specify the location of the folder to be indexed. Here is the S folder of drive C */
  19. File filedir = new file ("C: // s ");
  20. /* Place the index file here */
  21. File indexdir = new file ("C: // Index ");
  22. Analyzer luceneanalyzer = new standardanalyzer ();
  23. Indexwriter = new indexwriter (indexdir, luceneanalyzer,
  24. True );
  25. File [] textfiles = filedir. listfiles ();
  26. Long starttime = new date (). gettime ();
  27. // Add document to index
  28. For (INT I = 0; I <textfiles. length; I ++ ){
  29. If (textfiles [I]. isfile ()
  30. & Textfiles [I]. getname (). endswith (". txt ")){
  31. System. Out. println ("file" + textfiles [I]. GetCanonicalPath ()
  32. + "Being indexed ....");
  33. String temp = filereaderall (textfiles [I]. GetCanonicalPath (),
  34. "GBK ");
  35. System. Out. println (temp );
  36. Document document = new document ();
  37. Field fieldpath = new field ("path", textfiles [I]. getpath (),
  38. Field. Store. Yes, field. Index. No );
  39. Field fieldbody = new field ("body", temp, field. Store. Yes,
  40. Field. Index. tokenized,
  41. Field. termvector. with_positions_offsets );
  42. Document. Add (fieldpath );
  43. Document. Add (fieldbody );
  44. Indexwriter. adddocument (document );
  45. }
  46. }
  47. // The optimize () method is used to optimize the index.
  48. Indexwriter. Optimize ();
  49. Indexwriter. Close ();
  50. // Test the index time
  51. Long endtime = new date (). gettime ();
  52. System. Out
  53. . Println ("This cost"
  54. + (Endtime-starttime)
  55. + "Add the document to the index in milliseconds! "
  56. + Filedir. getpath ());
  57. }
  58. Public static string filereaderall (string filename, string charset)
  59. Throws ioexception {
  60. Bufferedreader reader = new bufferedreader (New inputstreamreader (
  61. New fileinputstream (filename), charset ));
  62. String line = new string ();
  63. String temp = new string ();
  64. While (line = reader. Readline ())! = NULL ){
  65. Temp + = line;
  66. }
  67. Reader. Close ();
  68. Return temp;
  69. }
  70. }

Index result: Java code

  1. File C:/S/1.txt being indexed ....
  2. People of the People's Republic of China, 2006
  3. File C:/S/2.txt being indexed ....
  4. People of the People's Republic of China, 2006
  5. File C:/S/3.txt being indexed ....
  6. People of the People's Republic of China, 2006
  7. This takes 297 milliseconds to add the document to the index! C:/s
3. After the index is created, query... java code
  1. Package lighter.javaeye.com;
  2. Import java. Io. ioexception;
  3. Import org. Apache. Lucene. analysis. analyzer;
  4. Import org. Apache. Lucene. analysis. Standard. standardanalyzer;
  5. Import org. Apache. Lucene. queryparser. parseexception;
  6. Import org. Apache. Lucene. queryparser. queryparser;
  7. Import org. Apache. Lucene. Search. Hits;
  8. Import org. Apache. Lucene. Search. indexsearcher;
  9. Import org. Apache. Lucene. Search. query;
  10. Public class testquery {
  11. Public static void main (string [] ARGs) throws ioexception, parseexception {
  12. Hits hits = NULL;
  13. String querystring = "China ";
  14. Query query = NULL;
  15. Indexsearcher searcher = new indexsearcher ("C: // Index ");
  16. Analyzer analyzer = new standardanalyzer ();
  17. Try {
  18. Queryparser QP = new queryparser ("body", analyzer );
  19. Query = QP. parse (querystring );
  20. } Catch (parseexception e ){
  21. }
  22. If (searcher! = NULL ){
  23. Hits = searcher. Search (query );
  24. If (hits. Length ()> 0 ){
  25. System. Out. println ("found:" + hits. Length () + "results! ");
  26. }
  27. }
  28. }
  29. }

Its running result: found by reference: 3 results!

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.