The usage of fsdirectory and ramdirectory in Lucene

Source: Internet
Author: User

Package com.ljq.one;

Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.InputStreamReader;

Import Org.apache.lucene.analysis.Analyzer;
Import Org.apache.lucene.analysis.standard.StandardAnalyzer;
Import org.apache.lucene.document.Document;
Import Org.apache.lucene.document.Field;
Import Org.apache.lucene.document.NumberTools;
Import Org.apache.lucene.document.Field.Index;
Import Org.apache.lucene.document.Field.Store;
Import Org.apache.lucene.index.IndexWriter;
Import org.apache.lucene.index.IndexWriter.MaxFieldLength;
Import Org.apache.lucene.queryParser.MultiFieldQueryParser;
Import Org.apache.lucene.queryParser.QueryParser;
Import Org.apache.lucene.search.Filter;
Import Org.apache.lucene.search.IndexSearcher;
Import Org.apache.lucene.search.Query;
Import Org.apache.lucene.search.ScoreDoc;
Import Org.apache.lucene.search.TopDocs;
Import org.apache.lucene.store.Directory;
Import org.apache.lucene.store.FSDirectory;
Import org.apache.lucene.store.RAMDirectory;
Import org.junit.Test;

public class Directorytest {
Data source Path
String Dspath = "E:/workspace/mylucene/lucenes/indexwriter adddocument ' a javadoc. txt";
The location where the index file is stored, that is, the index library
String Indexpath = "E:/workspace/mylucene/luceneindex";
Word breaker
Analyzer Analyzer = new StandardAnalyzer ();

/**
* Create an index, throw an exception, because the index library is not saved
*
* IndexWriter is used to manipulate (increase, delete, change) the index library
*/
@Test
public void CreateIndex () throws Exception {
Directory dir=fsdirectory.getdirectory (Indexpath);
Memory storage: The advantages of fast, the shortcomings of the program exit data is gone, so remember when the program quit Saving Index library, has been fsdirectory combined use
Because this is only temporarily stored in memory, the program exits without index library saving, so the program will error when searching
Directory dir=new ramdirectory ();
File File = new file (Dspath);
Document stores an organized data source that can be indexed and searched only if it is converted to a Document object
Document doc = new document ();
File name
Doc.add (New Field ("name", File.getname (), Store.yes, index.analyzed));
Retrieved content
Doc.add (New Field ("Content", readfilecontent (file), Store.yes, index.analyzed));
File size
Doc.add (New Field ("Size", numbertools.longtostring (File.length ()),
Store.yes, index.not_analyzed));
The location of the retrieved file
Doc.add (New Field ("Path", File.getabsolutepath (), Store.yes, index.not_analyzed));

Build an index
The first way
IndexWriter indexwriter = new IndexWriter (Indexpath, Analyzer, maxfieldlength.limited);
The second way
IndexWriter indexwriter = new IndexWriter (dir, analyzer, maxfieldlength.limited);
Indexwriter.adddocument (DOC);
Indexwriter.close ();
}

/**
* Index Creation (recommended)
*
* IndexWriter is used to manipulate (increase, delete, change) the index library
*/
@Test
public void CreateIndex2 () throws Exception {
Directory Fsdir = fsdirectory.getdirectory (Indexpath);
1. Read at startup
Directory Ramdir = new Ramdirectory (fsdir);

Operation Ramdir when running a program
IndexWriter ramindexwriter = new IndexWriter (Ramdir, Analyzer, maxfieldlength.limited);

Data source
File File = new file (Dspath);
Add Document
Document doc = new document ();
File name
Doc.add (New Field ("name", File.getname (), Store.yes, index.analyzed));
Retrieved content
Doc.add (New Field ("Content", readfilecontent (file), Store.yes, index.analyzed));
File size
Doc.add (New Field ("Size", numbertools.longtostring (File.length ()), Store.yes, index.not_analyzed));
The location of the retrieved file
Doc.add (New Field ("Path", File.getabsolutepath (), Store.yes, index.not_analyzed));
Ramindexwriter.adddocument (DOC);
Ramindexwriter.close ();

2. Save at exit
IndexWriter fsindexwriter = new IndexWriter (Fsdir, Analyzer, True, maxfieldlength.limited);
Fsindexwriter.addindexesnooptimize (New Directory[]{ramdir});

Optimize operations
Fsindexwriter.commit ();
Fsindexwriter.optimize ();

Fsindexwriter.close ();
}

/**
* Optimized operation
*
* @throws Exception
*/
@Test
public void CreateIndex3 () throws exception{
Directory Fsdir = fsdirectory.getdirectory (Indexpath);
IndexWriter fsindexwriter = new IndexWriter (Fsdir, Analyzer, maxfieldlength.limited);

Fsindexwriter.optimize ();
Fsindexwriter.close ();
}

/**
* Search
*
* Indexsearcher is used to query in the index library
*/
@Test
public void Search () throws Exception {
Request Field
String queryString = "Document";
String queryString = "Adddocument";

1. Parse the text you want to search into Query
string[] fields = {"Name", "content"};
Queryparser queryparser = new Multifieldqueryparser (fields, analyzer);
Query query = queryparser.parse (queryString);

2, query, find from index Library
Indexsearcher indexsearcher = new Indexsearcher (Indexpath);
Filter filter = NULL;
Topdocs Topdocs = indexsearcher.search (query, filter, 10000);
System.out.println ("A total of" "+ Topdocs.totalhits +" "Bar matching results");

3, printing results
for (Scoredoc ScoreDoc:topDocs.scoreDocs) {
Document Internal number
int index = Scoredoc.doc;
Remove the corresponding document according to the number
Document doc = Indexsearcher.doc (index);
System.out.println ("------------------------------");
System.out.println ("name =" + doc.get ("name"));
System.out.println ("content =" + doc.get ("content"));
SYSTEM.OUT.PRINTLN ("size =" + Numbertools.stringtolong (doc.get ("size"));
SYSTEM.OUT.PRINTLN ("path =" + doc.get ("path"));
}
}

/**
* Read File contents
*/
public static String readfilecontent (file file) {
try {
BufferedReader reader = new BufferedReader (new InputStreamReader (new FileInputStream (file));
StringBuffer content = new StringBuffer ();
for (String line = null; (line = Reader.readline ()) = null;) {
Content.append (line) append ("\ n");
}
Reader.close ();
return content.tostring ();
} catch (Exception e) {
throw new RuntimeException (e);
}
}

}

The usage of fsdirectory and ramdirectory in Lucene

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.