Lucene Learning Note II (array-based Lucene retrieval, index deletion)

Source: Internet
Author: User

To build an array:

Private string[] ids = {"1", "2", "3", "4", "5", "6"};p rivate string[] emails = {"[email protected]", "[email protected]", "[ Email protected] "," [email protected] "," [email protected] "," [Email protected] "};p rivate string[] contents = {" Welcome to Visited the space ', ' hello,my name is SCC ', ' The day is first ', ' tommorw ', ' Hello World ', ' I like football '};p rivate int [] att Achs = {2,3,1,4,5,6};p rivate string[] names = {"Zhang San", "John Doe", "Harry", "Jetty", "Mick", "divID"};

To create a directory:

Private Directory directory;public Indexutil () {try {//create directorydirectory = Fsdirectory.open (New File ("d:/workspace/ Lucene/index02 "). Topath ());} catch (IOException e) {e.printstacktrace ();}}



To create a document, add a field to the document:
public void Index () {IndexWriter IndexWriter = null;try {indexwriter = new IndexWriter (directory, new Indexwriterconfig (NE W StandardAnalyzer ()));D ocument doc = null;for (int i = 0; i < ids.length; i++) {//Create document (document equivalent to each record in database table) Doc = new DOCU ment ();//Add a field to the document (the field is equivalent to each field in the table) Doc.add (new TextField ("id", Ids[i],field.store.yes));d Oc.add (New TextField ("email", Emails[i],field.store.yes)); TextField content = new TextField ("content", Contents[i],field.store.yes); Content.tokenstream (new StandardAnalyzer () , null);d Oc.add (content);d Oc.add (New TextField ("name", Names[i],field.store.yes));// Writes the document to the index indexwriter.adddocument (DOC);}} catch (IOException e) {e.printstacktrace ();} Finally{if (indexwriter!=null) {try {indexwriter.close ();} catch (IOException e) {e.printstacktrace ();}}}}



Query index:
/** * Query index */public void query () {Indexreader Indexreader = null;try {indexreader = directoryreader.open (directory);//Through RE Ader can get the number of documents SYSTEM.OUT.PRINTLN ("Numdosc:" +indexreader.numdocs ()); System.out.println ("Maxdocs:" +indexreader.maxdoc ());} catch (IOException e) {e.printstacktrace ();} Finally{if (Indexreader!=null) try {indexreader.close ();} catch (IOException e) {e.printstacktrace ();}}}



JUnit Test Run:



Deletion of the index:

/** * Delete index */public void Deleteindex () {IndexWriter IndexWriter = null;try {indexwriter = new IndexWriter (directory, New in Dexwriterconfig (New StandardAnalyzer ()));//The parameter is an option, either a query or a term, Term is an exact lookup of the value indexwriter.deletedocuments (new term ("id", "1")); catch (Exception e) {e.printstacktrace ();} Finally{if (Indexwriter!=null) try {indexwriter.close ();} catch (IOException e) {e.printstacktrace ();}}}



JUnit Test:

When the Deleteindex method is executed, the query index is executed again





Lucene Learning Note II (array-based Lucene retrieval, index deletion)

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.