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
- People's Republic of China
- People all over China
- 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
- Package lighter.javaeye.com;
- Import java. Io. bufferedreader;
- Import java. Io. file;
- Import java. Io. fileinputstream;
- Import java. Io. ioexception;
- Import java. Io. inputstreamreader;
- 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;
- /**
- * Author lighter date 2006-8-7
- */
- Public class textfileindexer {
- Public static void main (string [] ARGs) throws exception {
- /* Specify the location of the folder to be indexed. Here is the S folder of drive C */
- File filedir = new file ("C: // s ");
- /* Place the index file here */
- File indexdir = new file ("C: // Index ");
- Analyzer luceneanalyzer = new standardanalyzer ();
- Indexwriter = new indexwriter (indexdir, luceneanalyzer,
- True );
- File [] textfiles = filedir. listfiles ();
- Long starttime = new date (). gettime ();
- // Add document to index
- For (INT I = 0; I <textfiles. length; I ++ ){
- If (textfiles [I]. isfile ()
- & Textfiles [I]. getname (). endswith (". txt ")){
- System. Out. println ("file" + textfiles [I]. GetCanonicalPath ()
- + "Being indexed ....");
- String temp = filereaderall (textfiles [I]. GetCanonicalPath (),
- "GBK ");
- System. Out. println (temp );
- Document document = new document ();
- Field fieldpath = new field ("path", textfiles [I]. getpath (),
- Field. Store. Yes, field. Index. No );
- Field fieldbody = new field ("body", temp, field. Store. Yes,
- Field. Index. tokenized,
- Field. termvector. with_positions_offsets );
- Document. Add (fieldpath );
- Document. Add (fieldbody );
- Indexwriter. adddocument (document );
- }
- }
- // The optimize () method is used to optimize the index.
- Indexwriter. Optimize ();
- Indexwriter. Close ();
- // Test the index time
- Long endtime = new date (). gettime ();
- System. Out
- . Println ("This cost"
- + (Endtime-starttime)
- + "Add the document to the index in milliseconds! "
- + Filedir. getpath ());
- }
- Public static string filereaderall (string filename, string charset)
- Throws ioexception {
- Bufferedreader reader = new bufferedreader (New inputstreamreader (
- New fileinputstream (filename), charset ));
- String line = new string ();
- String temp = new string ();
- While (line = reader. Readline ())! = NULL ){
- Temp + = line;
- }
- Reader. Close ();
- Return temp;
- }
- }
Index result: Java code
- File C:/S/1.txt being indexed ....
- People of the People's Republic of China, 2006
- File C:/S/2.txt being indexed ....
- People of the People's Republic of China, 2006
- File C:/S/3.txt being indexed ....
- People of the People's Republic of China, 2006
- This takes 297 milliseconds to add the document to the index! C:/s
3. After the index is created, query... java code
- Package lighter.javaeye.com;
- Import java. Io. ioexception;
- Import org. Apache. Lucene. analysis. analyzer;
- Import org. Apache. Lucene. analysis. Standard. standardanalyzer;
- Import org. Apache. Lucene. queryparser. parseexception;
- Import org. Apache. Lucene. queryparser. queryparser;
- Import org. Apache. Lucene. Search. Hits;
- Import org. Apache. Lucene. Search. indexsearcher;
- Import org. Apache. Lucene. Search. query;
- Public class testquery {
- Public static void main (string [] ARGs) throws ioexception, parseexception {
- Hits hits = NULL;
- String querystring = "China ";
- Query query = NULL;
- Indexsearcher searcher = new indexsearcher ("C: // Index ");
- Analyzer analyzer = new standardanalyzer ();
- Try {
- Queryparser QP = new queryparser ("body", analyzer );
- Query = QP. parse (querystring );
- } Catch (parseexception e ){
- }
- If (searcher! = NULL ){
- Hits = searcher. Search (query );
- If (hits. Length ()> 0 ){
- System. Out. println ("found:" + hits. Length () + "results! ");
- }
- }
- }
- }
Its running result: found by reference: 3 results!