-lucene Foundation of Lucene full-text Search

Source: Internet
Author: User

Lucene is the full-text search engine

First, before learning Lucene, let's think about the meaning of Lucene's existence.

1. In our previous scenario, database-based retrieval is done using the like syntax. But inefficient based on like syntax. We do not have access to our application requirements.

and using Lucene to index our data efficiently and quickly, to meet enterprise requirements.

We use Lucene to create an index library of the entire structure and then to find what we need to match based on the index, which is fast and efficient, so that we can quickly retrieve information-which is the purpose of Lucene.

2. Some search databases do not, for example, we want to find the matching of the contents of the attachment. The use of traditional database development is complex and inefficient. And using Lucene can be easily done.

Two. Application areas

Oa,cms and other systems, Internet enterprises

Three. Version

2.9
3.0 comparatively large changes
3.5 Some changes

Four. Lucene Basics

1. Download

http://lucene.apache.org
To the official website of the system download version 3.5.0.
Download version directory: http://archive.apache.org/dist/lucene/java/

The author writes this article when the version has arrived:

5.0.0/                          2015-02-19 08:46    -   
5.1.0/                          2015-04-13 15:08    -  

2.Lucene use

In the full-text Search tool, it is made up of three parts

1> Index Section

2> participle section

3> Search Section

3. Basic examples

1> Creating an Index

1 //Build an index2  Public voidindex () {3     //1. Create a directory-index (memory, hard disk)4Directory directory =NULL;//= new Ramdirectory ();//Built in memory5IndexWriter IndexWriter =NULL;6     Try {7Directory = Fsdirectory.open (NewFile ("/users/apple/downloads/lucene/index"));//built on the hard disk8         //2. Create indexwriter-Write Index9Indexwriterconfig config =NewIndexwriterconfig (version.lucene_35,NewStandardAnalyzer (version.lucene_35));TenIndexWriter =Newindexwriter (directory, config); One         //3. Create a Document Object ADocument doc =NULL; -         //4. Add filed to document -File f =NewFile ("/users/apple/downloads/lucene/lucene/example"); the          for(File file:f.listfiles ()) { -Doc =NewDocument (); -Doc.add (NewField ("Content",Newfilereader (file)); -Doc.add (NewField ("name", File.getname (), field.store.yes,field.index.not_analyzed)); +Doc.add (NewField ("Path", File.getabsolutepath (), field.store.yes,field.index.not_analyzed)); -             //5. Add document to index by IndexWriter + Indexwriter.adddocument (DOC); A         } at}Catch(Exception e) { - e.printstacktrace (); -}finally{ -         if(indexwriter!=NULL){ -             Try { - indexwriter.close (); in}Catch(corruptindexexception e) { - e.printstacktrace (); to}Catch(IOException e) { + e.printstacktrace (); -             } the         } *     } $}

2> Search Index

 Public voidSearch () {Directory directory=NULL;//= new Ramdirectory ();//Built in memoryIndexWriter IndexWriter =NULL; Try {        //1. Create a directoryDirectory = Fsdirectory.open (NewFile ("/users/apple/downloads/lucene/index"));//built on the hard disk//2. Create IndexreaderIndexreader Indexreader =indexreader.open (directory); //3. Create Indexsearcher according to IndexreaderIndexsearcher Indexsearcher =NewIndexsearcher (Indexreader); //4. Create a query for the search//Create Queryparser to search for the contents of a file, the second parameter represents the domain of the searchQueryparser parser =NewQueryparser (version.lucene_35, "content",NewStandardAnalyzer (version.lucene_35)); Query Query= Parser.parse ("Lucene"); //5. Search by Indexsearcher and return topdocs-> first 10 dataTopdocs Topdocs = indexsearcher.search (query, 10); //6. Get an array of Scoredoc objects based on TopdocsScoredoc [] SDS =Topdocs.scoredocs;  for(Scoredoc Sd:sds) {intDocID = Sd.doc;//Document ID//7. Get specific document objects based on Indexsearcher and Scoredoc objectsDocument doc =Indexsearcher.doc (DocID); //8. Get the values you want based on the Document objectSystem.out.println (Doc.get ("name")); }        //9. Close the Searcher objectIndexsearcher.close (); } Catch(Exception e) {e.printstacktrace (); }finally{        if(indexwriter!=NULL){            Try{indexwriter.close (); } Catch(corruptindexexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }        }    }}   

4. System Architecture

-lucene Foundation of Lucene full-text Search

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.