lucene-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 block
            e.printStackTrace();
        } catch (LockObtainFailedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.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 block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        TestFieldStore tfs = new TestFieldStore();
        tfs.createLuceneIndex();
        tfs.testSearch();
    }
}

 

由此可以看出Field.Store的设置与否与是否可以搜索到无关。 
这里整理一下 

Field.Store 
     :YES 可以搜索,保存原值 
      :NO  可以搜索,不保存原值 
     :COMPRESS 可以搜索,压缩保存原值 

 


这里需要注意的是在实际使用中,并不建议使用COMPRESS,存在压缩和解压过程,效率低下,对于大文本尽量使用NO 
还有一点就是是否可被搜索与Store无关,只与Index有关。 
这里使用的是lucene 2.3.2 

 

转载于:https://www.cnblogs.com/hwaggLee/p/5228971.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值