lucene第一步---4.Field.Store解析
lucene第一步---4.Field.Store解析
Store
COMPRESS:压缩保存。用于长文本或二进制数据
YES:保存
NO:不保存
具体理解当然是给出示例了:
package demo.first;import java.io.IOException;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.document.Field.Index;import org.apache.lucene.document.Field.Store;import org.apache.lucene.index.CorruptIndexException;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.queryParser.ParseException;import org.apache.lucene.queryParser.QueryParser;import org.apache.lucene.search.Hits;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.store.LockObtainFailedException;public class TestFieldStore {/** * 索引文件的存放位置 */String path = "D://workspace//fwk//lucenedemo//firstLuceneIndex";public void createLuceneIndex(){try {IndexWriter iw = new IndexWriter(path,new StandardAnalyzer(),true);Document doc = new Document();//Store.YES 保存 可以查询 可以打印内容Field storeYes = new Field("storeyes","storeyes",Store.YES,Index.TOKENIZED);//Store.NO 不保存 可以查询 不可打印内容 由于不保存内容所以节省空间Field storeNo = new Field("storeno","storeno",Store.NO,Index.TOKENIZED);//Store.COMPRESS 压缩保存 可以查询 可以打印内容 可以节省生成索引文件的空间Field storeCompress = new Field("storecompress","storecompress",Store.COMPRESS,Index.TOKENIZED);doc.add(storeYes);doc.add(storeNo);doc.add(storeCompress);iw.addDocument(doc);iw.optimize();iw.close();} catch (CorruptIndexException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (LockObtainFailedException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public void testSearch(){try {IndexSearcher iser = new IndexSearcher(path);/* * Store.YES 采用保存模式,可以查询到,并且可以打印出内容 */System.out.println("---storeYes");QueryParser queryParser1 = new QueryParser("storeyes",new StandardAnalyzer());Hits hits1 = iser.search(queryParser1.parse("storeyes"));for(int i = 0;i<hits1.length();i++){System.out.println("id :"+hits1.id(i));System.out.println("doc :"+hits1.doc(i));System.out.println("context :"+hits1.doc(i).get("storeyes"));System.out.println("score :"+hits1.score(i));}/* * Store.NO 采用不保存模式,可以查询到,但是不能打印出内容 */System.out.println("---storeNo");QueryParser queryParser2 = new QueryParser("storeno",new StandardAnalyzer());Hits hits2 = iser.search(queryParser2.parse("storeno"));for(int i = 0;i<hits2.length();i++){System.out.println("id :"+hits2.id(i));System.out.println("doc :"+hits2.doc(i));System.out.println("context :"+hits2.doc(i).get("storeno"));System.out.println("score :"+hits2.score(i));}/* * Store.COMPRESS 采用压缩保存模式,可以查询到,并且可以打印出内容 */System.out.println("---storeCompress");QueryParser queryParser3 = new QueryParser("storecompress",new StandardAnalyzer());Hits hits3 = iser.search(queryParser3.parse("storecompress"));for(int i = 0;i<hits3.length();i++){System.out.println("id :"+hits3.id(i));System.out.println("doc :"+hits3.doc(i));System.out.println("context :"+hits3.doc(i).get("storecompress"));System.out.println("score :"+hits3.score(i));}iser.close();} catch (CorruptIndexException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static void main(String[] args) {TestFieldStore tfs = new TestFieldStore();tfs.createLuceneIndex();tfs.testSearch();}}