lucene 中关于Store.YES 关于Store.NO的解释

        总算搞明白 lucene 中关于Store.YES和Store.NO的解释了。一直对Lucene Store.YES不太理解,网上多数的说法是存储字段,NO为不存储。这样的解释有点郁闷:字面意思一看就明白,但是不解。之前我的理解是:如果字段可以不存储,那要怎么搜索这个不存储的字段呢?

        原来Lucene就是这样,可以设置某些字段为不存储,但是可以用来检索。终于在一篇文章里看到这几句话,突然间就明白了。

//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

  1. storeCompress = new Field("storecompress","storecompress",Store.COMPRESS,Index.TOKENIZED); 

        至此,对于理解Store.YES,Store.NO 就是不存储就不能直接获取此字段的内容,存储了就可以。但是两者都可以用于检索。字段是否能被搜索,还与Index有关。

package luxun.lucene.base;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

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.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.FieldDoc;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.SortField.Type;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopFieldDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
public class SortFieldValueTest {
    
    @SuppressWarnings("deprecation")
    public  void buildIndex() throws CorruptIndexException, LockObtainFailedException, IOException {
        File indexDir = new File("/home/cristo/luxun_test/luxunlucenetest/04/index");
        // dataDir is the directory that hosts the text files that to be indexed
        Directory directory = new SimpleFSDirectory(indexDir);
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_47);
        IndexWriter writer = new IndexWriter(directory,     new IndexWriterConfig(Version.LUCENE_47, analyzer));
        Document doc = new Document();
        doc.add(new Field("id", "1", Store.YES, Index.NO));
        doc.add(new Field("text", "lucene", Store.NO, Index.ANALYZED));
        doc.add(new Field("time", "2010", Store.NO, Index.NOT_ANALYZED_NO_NORMS));
        doc.add(new Field("tide", "149", Store.NO, Index.NOT_ANALYZED_NO_NORMS));
        writer.addDocument(doc);
        doc = new Document();
        doc.add(new Field("id", "3", Store.YES, Index.NO));
        doc.add(new Field("text", "lucene", Store.NO, Index.ANALYZED));
        doc.add(new Field("time", "2011", Store.NO, Index.NOT_ANALYZED_NO_NORMS));
        doc.add(new Field("tide", "14", Store.NO, Index.NOT_ANALYZED_NO_NORMS));
        writer.addDocument(doc);
        doc = new Document();
        doc.add(new Field("id", "2", Store.YES, Index.NO));
        doc.add(new Field("text", "lucene", Store.NO, Index.ANALYZED));
        doc.add(new Field("time", "2001", Store.NO, Index.NOT_ANALYZED_NO_NORMS));
        doc.add(new Field("tide", "13", Store.NO, Index.NOT_ANALYZED_NO_NORMS));
        writer.addDocument(doc);
        doc = new Document();
        doc.add(new Field("id", "5", Store.YES, Index.NO));
        doc.add(new Field("text", "lucene", Store.NO, Index.ANALYZED));
        doc.add(new Field("time", "2001", Store.NO, Index.NOT_ANALYZED_NO_NORMS));
        doc.add(new Field("tide", "19", Store.NO, Index.NOT_ANALYZED_NO_NORMS));
        writer.addDocument(doc);
        doc = new Document();
        doc.add(new Field("id", "9", Store.YES, Index.NO));
        doc.add(new Field("text", "lucene", Store.NO, Index.ANALYZED));
        doc.add(new Field("time", "2171", Store.NO, Index.NOT_ANALYZED_NO_NORMS));
        doc.add(new Field("tide", "19", Store.NO, Index.NOT_ANALYZED_NO_NORMS));
        writer.addDocument(doc);
        writer.commit();
        writer.close();
    }
    public  void searchWithOneSortField() throws CorruptIndexException, IOException {//可以通过对time属性进行索引,排序,获取对应的id
        File indexDir = new File("/home/cristo/luxun_test/luxunlucenetest/04/index");
        @SuppressWarnings("deprecation")
        IndexSearcher searcher = new IndexSearcher(IndexReader.open(FSDirectory.open( indexDir)));
        TermQuery termQuery = new TermQuery(new Term("time", "2001"));
        TopFieldDocs topFieldDocs = searcher.search(termQuery, null, 10, new Sort(new SortField("time",Type.STRING, true)));
        ScoreDoc[] sorDocs = topFieldDocs.scoreDocs;
        for (ScoreDoc doc : sorDocs) {
        //    FieldDoc fieldDoc = (FieldDoc) doc;
            System.out.println(searcher.doc(doc.doc).get("id"));
        }
    }
   
    public static void main(String[] args) throws CorruptIndexException, IOException {
        SortFieldValueTest SortFieldValueTest1=new SortFieldValueTest();
        SortFieldValueTest1.buildIndex();
        SortFieldValueTest1.searchWithOneSortField();

    }
}

 

转载于:https://my.oschina.net/u/3209682/blog/820698

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值