[Lucene] Detailed description of Lucene full-text retrieval information writing and reading
Lucene's general structure:
The process of writing information to the index database:
The process of reading information:
The following is an example of writing and reading information to the index database:
Public void testCreateIndex () throws Exception {/*** 1. Create a student object, and store information in * 2. Call the indexWriter API to store data in the index database * 3. Disable indexWriter * // to create a Student object, and store the information in Student student = new Student (); student. setId (1L); student. setName ("Zhang San"); // call the indexWriter API to store data in the index database./*** create an IndexWriter * parameter three. 1. index database, point to the location of the index library 2. Word divider * // create the index library Directory directory = FSDirectory. open (new File (". /indexDir "); // create Analyzer analyzer Analyzer = new StandardAnalyzer (Version. required e_30); IndexWriter indexWriter = new IndexWriter (directory, analyzer, MaxFieldLength. LIMITED); // convert a student object to documentDocument document Document = new document (); Field idField = new Field ("id", student. getId (). toString (), Store. YES, Index. NOT_ANALYZED); Field nameField = new Field ("name", student. getName (), Store. YES, Index. ANALYZED); document. add (idField); document. add (nameField); indexWriter. addDocument (document); // disable indexWriterindexWriter. close ();}
Public void testSearchIndex () throws Exception {/*** 1. Create an IndexSearch object * 2. Call the search method to search * 3. Output the content * // create an IndexSearch object Directory = FSDirectory. open (new File (". /indexDir "); IndexSearcher indexSearcher = new IndexSearcher (directory); // call the search method to retrieve Analyzer analyzer = new StandardAnalyzer (Version. required e_30); QueryParser queryParser = new QueryParser (Version. required e_30, "name", analyzer); Query query = queryParser. parse ("Zhang"); // The keyword TopDocs topDocs = indexSearcher. search (query, 2); // The first two int count = topDocs. totalHits; // The total number of records queried by keyword. ScoreDoc [] scoreDocs = topDocs. scoreDocs; List
StudentList = new ArrayList
(); For (ScoreDoc scoreDoc: scoreDocs) {float score = scoreDoc. score; // keyword score int index = scoreDoc.doc; // index subscript Document document = indexSearcher.doc (index); // convert the Document to StudentStudent student = new Student (); student. setId (Long. parseLong (document. get ("id"); // document. getField ("id "). stringValue () student. setTitle (document. get ("name"); studentList. add (student) ;}for (Student student: studentList) {System. out. println (student. getId (); System. out. println (student. getName ());}}
Note: 1. The addition, deletion, and modification of the index database are operated by indexWriter. 2. The same index database at the same time, only one indexWriter operation is allowed. 3. After IndexWriter is created, the index library pointed to by indexwriter is occupied, only when indexWriter is created. when the index is closed, the lock resources can be released. 4. When a new indexWriter wants to own an index database, the original indexWriter must release the lock. 5. As long as the index database contains write. locks the file. 6. indexWriter is locked. close has two meanings: 1. disable IO resources; 2. release lock
Combination of file index library and memory index Library: 1. Can I set multiple index libraries? 2. Can I combine the index libraries? If the memory index library Directory ramDirectory = new RamDirectory (Directory d ); in this way, you can put an index library into the memory index library and use IndexWriter. the addIndexesNoOptimize method can merge multiple index libraries. 3. Can applications interact with index libraries in memory?