总算搞明白 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/

lucene 中关于Store.YES 关于Store.NO的解释的更多相关文章

  1. SQL Server中TempDB管理(version store的逻辑结构)

    原文:SQL Server中TempDB管理(version store的逻辑结构) 原文来自: http://blogs.msdn.com/b/sqlserverstorageengine/arch ...

  2. vue-learning:41 - Vuex - 第二篇:const store = new Vue.Store(option)中option选项、store实例对象的属性和方法

    vuex 第二篇:const store = new Vue.Store(option)中option选项.store实例对象的属性和方法 import Vuex from 'vuex' const ...

  3. 【Lucene3.6.2入门系列】第03节_简述Lucene中常见的搜索功能

    package com.jadyer.lucene; import java.io.File; import java.io.IOException; import java.text.SimpleD ...

  4. Lucene 中自定义排序的实现

    使用Lucene来搜索内容,搜索结果的显示顺序当然是比较重要的.Lucene中Build-in的几个排序定义在大多数情况下是不适合我们使用的.要适合自己的应用程序的场景,就只能自定义排序功能,本节我们 ...

  5. lucene中的IndexWriter.setMaxFieldLength()

    lucene中的IndexWriter.setMaxFieldLength() 老版本的Lucene中,IndexWriter的maxFieldLength是指一个索引中的最大的Field个数. 这个 ...

  6. 《Lucene in Action 第二版》第4章节 学习总结 -- Lucene中的分析

    通过第四章的学习,可以了解lucene的分析过程是怎样的,并且可以学会如何使用lucene内置分析器,以及自定义分析器.下面是具体总结 1. 分析(Analysis)是什么? 在lucene中,分析就 ...

  7. lucene中Field简析

    http://blog.csdn.net/zhaoxiao2008/article/details/14180019 先看一段lucene3代码 Document doc = new Document ...

  8. lucene中TOKENIZED,UN_TOKENIZED 解釋

    Field("content",curArt.getContent(),Field.Store.NO,Field.Index.TOKENIZED)); 這些地方與舊版本有很大的區別 ...

  9. Lucene中的 Query对象

    "Lucene中的 Query对象": 检 索前,需要对检索字符串进行分析,这是由queryparser来完成的.为了保证查询的正确性,最好用创建索引文件时同样的分析器. quer ...

随机推荐

  1. CreateProcess注意的几个地方

    1.CreateProcess失败,GetLastError返回998,应该是最后两个参数没有初始化导致的. 2.要使外部程序隐藏窗口运行,需要将STARTUPINFO的dwFlags指定为START ...

  2. 高精度运算专题-输出函数与字符串转数字函数(Output function and the string to number function)

    输出函数:这个函数别看它小,但浓缩的都是精华啊 作用:对于高精度的数组进行倒序输出 思路:首先从被传入的数组第一位开始,一直往前扫输出就可以了(i--) 注释:因为每个数组的第一位是用来存储这个数组的 ...

  3. JS跨域解决方式 window.name

    window.name 传输技术,原本是 Thomas Frank 用于解决 cookie 的一些劣势(每个域名 4 x 20 Kb 的限制.数据只能是字符串.设置和获取 cookie 语法的复杂等等 ...

  4. 12c 补丁架构 以及opatch 功能

    cd $ORACLE_HOME/ccr/bin ./emocmrsp oracle@qc550705:/oracle/app/oracle/product/12.1.0.2/db_1/ccr/bin& ...

  5. 1.0 Python 学习网站

    w3cschool : http://www.runoob.com/python/python-tutorial.html cnblog Python 从入门到精通:  http://www.cnbl ...

  6. JPA 系列教程18-自动把firstName+lastName合并为name字段

    需求 设计的国际化网站,页面需要输入firstName,lastName,后台数据库只需要存储name属性. 页面获取的firstName,lastName持久化到数据库name属性,规则按照,分隔保 ...

  7. The Accomodation of Students(判断二分图以及求二分图最大匹配)

    The Accomodation of Students Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &a ...

  8. mysql编码设置 [http://blog.knowsky.com/254652.htm]

    MYSQL 2009-09-11 15:37 阅读73 评论1 字号: 大大 中中 小小mysql> SHOW VARIABLES LIKE 'character_set_%';+------- ...

  9. Visual Studio中使用Git Flow

    在VS下使用 GitFlow管理项目开发 1.右键将你的解决方案添加到源代码管理,如果你的VS没有安装git,会提示安装,安装完成之后,在团队资源管理可以看到如下界面 (图一) 2.安装gitflow ...

  10. android 线程池的使用

    转自http://www.trinea.cn/android/java-android-thread-pool/ Java(Android)线程池 介绍new Thread的弊端及Java四种线程池的 ...