Package com. ljq. one; import java. io. bufferedReader; import java. io. file; import java. io. fileInputStream; import java. io. inputStreamReader; 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.e.doc ument. numberTools; import org.apache.w.e.doc ument. F Ield. index; import org.apache.e.doc ument. field. store; import org. apache. lucene. index. indexWriter; import org. apache. lucene. index. indexWriter. maxFieldLength; import org. apache. lucene. queryParser. multiFieldQueryParser; import org. apache. lucene. queryParser. queryParser; import org. apache. lucene. search. filter; 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; import org. apache. lucene. store. RAMDirectory; import org. junit. test; public class DirectoryTest {// data source path String dspath = "E:/workspace/mylucene/lucenes/IndexWriter addDocument's a javadoc. txt "; // the location where the index file is stored, that is, the index database String indexpath =" E:/workspace/my Lucene/luceneIndex "; // Analyzer analyzer Analyzer = new StandardAnalyzer ();/*** when creating an index, an exception is thrown, because the index database is not saved ** IndexWriter is used to operate (add, delete, and modify) The */@ Test public void createIndex () throws Exception {// Directory dir = FSDirectory. getDirectory (indexpath); // memory storage: the advantage is fast. However, the data will be lost when the program exits, so remember to save the index database when the program exits, FSDirectory used in combination // because it is only temporarily saved in the memory, and the index database is not saved when the program exits, the program reports the error Directory dir = new RAMDirectory () during the search (); file file = new File (dspath ); // Document stores the data source after the organization. Only the data source converted to the Document object can be indexed and searched for Document doc = new Document (); // file name doc. add (new Field ("name", file. getName (), Store. YES, Index. ANALYZED); // The retrieved content doc. add (new Field ("content", readFileContent (file), Store. YES, Index. ANALYZED); // file size doc. add (new Field ("size", NumberTools. longToString (file. length (), Store. YES, Index. NOT_ANALYZED); // The retrieved File Location doc. add (new Field ("path", file. get AbsolutePath (), Store. YES, Index. NOT_ANALYZED); // create an index // The first method // IndexWriter indexWriter = new IndexWriter (indexpath, analyzer, MaxFieldLength. LIMITED); // method 2 IndexWriter indexWriter = new IndexWriter (dir, analyzer, MaxFieldLength. LIMITED); indexWriter. addDocument (doc); indexWriter. close ();}/*** create index (recommended) ** IndexWriter is used to operate (add, delete, and modify) The index library */@ Test public void createIndex2 () throws Exception {Directory fsDir = FSDirectory. getDirectory (indexpath); // 1. Read Directory ramDir = new RAMDirectory (fsDir) at startup; // run ramDir IndexWriter ramIndexWriter = new IndexWriter (ramDir, analyzer, MaxFieldLength. LIMITED); // data source File file = new File (dspath); // Add Document doc = new Document (); // File name doc. add (new Field ("name", file. getName (), Store. YES, Index. ANALYZED); // The retrieved content doc. add (new Field ("Content", readFileContent (file), Store. YES, Index. ANALYZED); // file size doc. add (new Field ("size", NumberTools. longToString (file. length (), Store. YES, Index. NOT_ANALYZED); // The retrieved File Location doc. add (new Field ("path", file. getAbsolutePath (), Store. YES, Index. NOT_ANALYZED); ramIndexWriter. addDocument (doc); ramIndexWriter. close (); // 2. Save IndexWriter fsIndexWriter = new IndexWriter (fsDir, analyzer, true, M AxFieldLength. LIMITED); fsIndexWriter. addIndexesNoOptimize (new Directory [] {ramDir}); // optimize the fsIndexWriter operation. commit (); fsIndexWriter. optimize (); fsIndexWriter. close ();}/*** Optimization Operation ** @ throws Exception */@ Test public void createIndex3 () throws Exception {Directory fsDir = FSDirectory. getDirectory (indexpath); IndexWriter fsIndexWriter = new IndexWriter (fsDir, analyzer, MaxFieldLength. LIMITED); fsIn DexWriter. optimize (); fsIndexWriter. close ();}/*** search ** IndexSearcher is used to query the index database */@ Test public void search () throws Exception {// request field // String queryString = "document"; String queryString = "adddocument"; // 1, parse the text to be searched into Query String [] fields = {"name", "content"}; QueryParser queryParser = new MultiFieldQueryParser (fields, analyzer); Query query Query = queryParser. parse (queryString); // 2 Query: Search for IndexSearcher indexSearcher = new IndexSearcher (indexpath), Filter filter = null, and TopDocs topDocs = indexSearcher in the index database. search (query, filter, 10000); System. out. println ("total [" + topDocs. totalHits + "] matching results"); // 3. Print the result for (ScoreDoc scoreDoc: topDocs. scoreDocs) {// Document internal No. int index = scoreDoc.doc; // retrieve the Document doc = indexSearcher.doc (index) according to the serial number; System. out. println ("------------------ ------------ "); System. out. println ("name =" + doc. get ("name"); System. out. println ("content =" + doc. get ("content"); System. out. println ("size =" + NumberTools. stringToLong (doc. get ("size"); System. out. println ("path =" + doc. get ("path") ;}}/*** Read File Content */public static String readFileContent (file File) {try {BufferedReader reader = new BufferedReader (new InputStreamReader (new FileInputSt Ream (file); StringBuffer content = new StringBuffer (); for (String line = null; (line = reader. readLine ())! = Null;) {content. append (line ). append ("\ n");} reader. close (); return content. toString () ;}catch (Exception e) {throw new RuntimeException (e );}}}