Lucene3.6 Summary article

Source: Internet
Author: User
Tags ssh

1. Merging index library fragment files
IndexWriter's optimize () method is obsolete because the method is inefficient. Merging files is primarily using the IndexWriter setmergefactor (int) method, but in the Lucene3.6 version, the method is obsolete and is replaced directly with the Logmergepolicy.setmergefactor (int) method.
When the parameter value of setmergefactor (int) is small, the index is created slowly. When the parameter value is large, the index is created faster. Greater than 10 is suitable for batch creation of indexes.


Sample code

Merge index file @Test public void Testmergefactor () {try {String path = ' d:\\luceneex\\day03 ';
			File File = new file (path);
			Create a directory under the current path called Indexdir//File Indexdir = new File ("./indexdir");

			Directory mdirectory = fsdirectory.open (file);
			Analyzer Manalyzer = new Ikanalyzer ();

			Indexwriterconfig config = new Indexwriterconfig (version.lucene_36, Manalyzer);

			Logbytesizemergepolicy mergepolicy = new Logbytesizemergepolicy ();
			Mergepolicy.setmaxmergedocs (Maxmergedocs);
			MERGEPOLICY.SETMAXMERGEMB (MB); Mergepolicy.setmergefactor (3);

			Reach 3 Documents and merge Config.setmergepolicy (Mergepolicy);

			IndexWriter mindexwriter = new IndexWriter (mdirectory, config);
			Book Book1 = Createbook ("Android Kernel Secret", "ABC", "2010-07", "Android mobile Development", 8.9f);

			Document Doc1 = createdocument (Book1);
			Book Book2 = Createbook ("Android Multimedia Development", "BCD", "2011-07", "Android Multimedia", 8.5f);

			Document doc2 = createdocument (BOOK2); Book BOOK3 = Createbook ("Android Enterprise appDevelopment "," QAB "," 2012-05 "," Android Enterprise Application ", 8.2f);
			Document doc3 = createdocument (BOOK3); Doc3.setboost (1.5F); 
			Boost: Set the score, 2F on the basis of the current score, make the score increase book BOOK4 = Createbook ("Android Kernel Anatomy", "WPS", "2012-09", "Android Drive Development", 9.8f);

			Document doc4 = createdocument (BOOK4);
			Mindexwriter.adddocument (Doc1);
			Mindexwriter.adddocument (DOC2);
			Mindexwriter.adddocument (DOC3);

			Mindexwriter.adddocument (DOC4);

		Mindexwriter.close ();
		} catch (Corruptindexexception e) {e.printstacktrace ();
		} catch (Lockobtainfailedexception e) {e.printstacktrace ();
		} catch (IOException e) {e.printstacktrace (); }}/** * Tool method for creating document objects * * @param book * @return */public Document createdocument (book book) {document D

		OC = new Document ();
		Field id = new Field ("id", Book.getid () + "", Store.yes, index.analyzed);
		Field title = new Field ("title", Book.gettitle (), Store.yes, index.analyzed); Field Author = new Field ("Author", Book.getauthor (), Store.yes, index.analyzed);
		Field publishtime = new Field ("Publishtime", Book.getpublishtime (), Store.yes, index.analyzed);
		Field Source = new Field ("Source", Book.getsource (), Store.yes, index.analyzed);
		Field category = new Field ("Category", Book.getcategory (), Store.yes, index.analyzed);

		Field reputation = new Field ("Reputation", book.getreputation () + "", Store.yes, index.analyzed);
		Doc.add (ID);
		Doc.add (title);
		Doc.add (author);
		Doc.add (Publishtime);
		Doc.add (source);
		Doc.add (category);

		Doc.add (reputation);
	return doc; }/** * Create book Object * * @param title * @param author * @param publishtime * @param category * @param reputat ION * @return * * Public book Createbook (string title, string author, String publishtime, string category, float re
		Putation) {Random r = new Random ();

		int id = r.nextint (10000);
		Book book = new book ();
		Book.setid (ID);
		Book.setauthor (author);
		Book.settitle (title); Book.setcategory (category);
		Book.setpublishtime (Publishtime);
		Book.setreputation (reputation);

		Book.setsource ("Tsinghua University Press");
	return book; }



2, Memory index directory and file system index directory used together
The Memory index directory operates very fast, so we can load the index library from the file system into memory when we manipulate the index, and then write back to the filesystem when the operation is completed.
When the index file in memory is written back to the text-building system, we need to rebuild the index directory. For example, the original file system index directory has 10 files, loaded into the memory directory is a copy of 10 files to memory, then we added an index file, the number of indexed directory files in memory becomes 11, write to the file system, memory index directory file number (11) Add the original file system index directory of the number of files (10) becomes 21, 10 files are duplicated, so we need to delete the original file system in the index directory re-created.
However, if the index library is huge, it is not recommended because the required memory is large.


Sample code

@Test public void Testramfscombination () {try {String path = ' d:\\luceneex\\day03 ';
			File File = new file (path);

			Directory mdirectory = fsdirectory.open (file);
			
			Analyzer Manalyzer = new Ikanalyzer ();
			Create a memory index directory to load the index library in the file system ramdirectory Mramdirectory = new Ramdirectory (mdirectory);
			
			Indexwriterconfig config = new Indexwriterconfig (version.lucene_36, Manalyzer);

			IndexWriter ramindexwriter = new IndexWriter (mramdirectory, config);
			Book Book1 = Createbook ("Spring Enterprise Development", "Qwa", "2012-07", "Spring Web Development", 8.5f);

			Document Doc1 = createdocument (Book1);
			Book Book2 = Createbook ("Master ssh Three Frames", "ssh", "2012-11", "Web development ssh", 9.1f);
			
			Document doc2 = createdocument (BOOK2);
			Ramindexwriter.adddocument (Doc1);
			
			Ramindexwriter.adddocument (DOC2);
			
			Ramindexwriter.close ();
			Write back to the file indexwriterconfig fsindexwriterconfig = new Indexwriterconfig (Version.lucene_36,manalyzer); Create a new index directory or overwrite the original index directory Fsindexwriterconfig.setOpenMode (openmode.create);
			
			IndexWriter fsindexwriter = new IndexWriter (mdirectory, fsindexwriterconfig);
			Writes the In-memory index library to the file system fsindexwriter.addindexes (mramdirectory);
			
		Fsindexwriter.close ();
		} catch (IOException e) {e.printstacktrace (); }
	}





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.