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

总算搞明白 lucene 中关于Store.YES  关于Store.NO的解释了

 

一直对Lucene Store.YES不太理解,网上多数的说法是存储字段,NO为不存储。

这样的解释有点郁闷:字面意思一看就明白,但是不解。

之前我的理解是:如果字段可以不存储,那要怎么搜索这个不存储的字段呢?

原来Lucene就是这样,可以设置某些字段为不存储,但是可以用来检索。

终于在一篇文章里看到这几句话,突然间就明白了。

  1. //Store.YES 保存 可以查询 可以打印内容
  2. Field storeYes = new Field("storeyes","storeyes",Store.YES,Index.TOKENIZED);
  3. //Store.NO 不保存 可以查询 不可打印内容 由于不保存内容所以节省空间,但是这个索引是存在的,可以通过这个索引去检索
  4. Field storeNo = new Field("storeno","storeno",Store.NO,Index.TOKENIZED);
  5. //Store.COMPRESS 压缩保存 可以查询 可以打印内容 可以节省生成索引文件的空间,Field 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();

    }
}

 

 

参考http://blog.csdn.net/telnetor/article/details/6187378

http://www.codeweblog.com/%E6%80%BB%E7%AE%97%E6%89%BE%E5%88%B0lucene-%E5%85%B3%E4%BA%8Estore-yes%E7%9A%84%E8%A7%A3%E9%87%8A%E4%BA%86/

posted on 2015-12-03 17:17  1130136248  阅读(823)  评论(0编辑  收藏  举报

导航