"Lucene" detailed information writing and reading of Lucene full-text retrieval

Source: Internet
Author: User
Tags create index

General structure diagram of Lucene:



The process of writing information to the index library:



The process of reading information:




Here is an example of writing information and reading information to an index library:

public void Testcreateindex () throws exception{/** * 1, create a student object, and store the information in * 2, call the IndexWriter API to store the data in the index library * 3, Close IndexWriter *///Create a Student object and store the information in student student = new Student (); Student.setid (1L); Student.setname ("Zhang San");// Call IndexWriter's API to store the data in the index library   /*** Create a indexwriter*    parameter three 1, index library, point to Index Library  2, Word breaker *///CREATE INDEX Library directory Directory = Fsdirectory.open (new File ("./indexdir"));//Create word breaker analyzer Analyzer = new StandardAnalyzer (version.lucene_ IndexWriter indexwriter = new IndexWriter (directory, analyzer, maxfieldlength.limited);// Convert a Student object to documentdocument 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);d Ocument.add (IDfield);d Ocument.add ( NameField); indexwriter.adddocument (document);//close Indexwriterindexwriter.close ();}


public void Testsearchindex () throws exception{/** * 1, create a Indexsearch object * 2, call the search method to retrieve * 3, Output *///create a Indexsearch Object Directory directory = Fsdirectory.open (new File ("./indexdir")); Indexsearcher indexsearcher = new Indexsearcher ( directory);//Call the search method to retrieve Analyzer Analyzer = new StandardAnalyzer (version.lucene_30); Queryparser queryparser = new Queryparser (version.lucene_30, "name", analyzer);  Query query = Queryparser.parse ("Zhang");  The keyword to find topdocs topdocs = indexsearcher.search (query, 2);  Top two int count = Topdocs.totalhits; The total number of records queried according to the keyword scoredoc[] scoredocs = Topdocs.scoredocs; list<student> studentlist = new arraylist<student> (); for (Scoredoc Scoredoc:scoredocs) {Float score =  Scoredoc.score;  Keyword Score int index = Scoredoc.doc; Index Subscript Document document = Indexsearcher.doc (index);//Convert 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 ());}}

Description:1, the index library's increase, the deletion, the change is by the IndexWriter to operate2. Within the same time, the same index library can only allow one IndexWriter operation3. When the IndexWriter is created, the index library that IndexWriter points to is occupied, and only when Indexwriter.close can release the lock's resources4, when a new indexwriter want to own the index library, the original IndexWriter must release the lock5, as long as there is Write.lock file in the index library, the description is locked 6, Indexwriter.close has two meanings: 1. Close IO resources; 2. Release Lock
combination of the file index library and the Memory Index library:1, can you set up a number of index librariesyou can set up a number of index libraries2. Can the index library be merged ?If this is a Memory index librarydirectory ramdirectory = new Ramdirectory (directory d);This allows you to put an index library into the memory index libraryMany index libraries can be combined using the Indexwriter.addindexesnooptimize method3. The application can interact with the index library in memory


Author : Gu

Sign : Don't lose to who you used to be




"Lucene" detailed information writing and reading of Lucene full-text retrieval

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.