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 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
本系列教程如果没有明确指出都是沿用此版本
分享到:
相关推荐
赠送jar包:lucene-spatial3d-7.3.1.jar; 赠送原API文档:lucene-spatial3d-7.3.1-javadoc.jar; 赠送源代码:lucene-spatial3d-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-spatial3d-7.3.1.pom; 包含...
赠送jar包:lucene-analyzers-smartcn-7.7.0.jar; 赠送原API文档:lucene-analyzers-smartcn-7.7.0-javadoc.jar; 赠送源代码:lucene-analyzers-smartcn-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-...
赠送jar包:lucene-spatial3d-6.6.0.jar; 赠送原API文档:lucene-spatial3d-6.6.0-javadoc.jar; 赠送源代码:lucene-spatial3d-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-spatial3d-6.6.0.pom; 包含...
赠送jar包:lucene-backward-codecs-7.2.1.jar; 赠送原API文档:lucene-backward-codecs-7.2.1-javadoc.jar; 赠送源代码:lucene-backward-codecs-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...
赠送jar包:lucene-spatial3d-7.7.0.jar; 赠送原API文档:lucene-spatial3d-7.7.0-javadoc.jar; 赠送源代码:lucene-spatial3d-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-spatial3d-7.7.0.pom; 包含...
赠送jar包:lucene-spatial3d-7.2.1.jar; 赠送原API文档:lucene-spatial3d-7.2.1-javadoc.jar; 赠送源代码:lucene-spatial3d-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-spatial3d-7.2.1.pom; 包含...
赠送jar包:lucene-backward-codecs-7.3.1.jar; 赠送原API文档:lucene-backward-codecs-7.3.1-javadoc.jar; 赠送源代码:lucene-backward-codecs-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...
赠送jar包:lucene-analyzers-smartcn-7.7.0.jar; 赠送原API文档:lucene-analyzers-smartcn-7.7.0-javadoc.jar; 赠送源代码:lucene-analyzers-smartcn-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-...
赠送jar包:lucene-spatial-extras-7.3.1.jar; 赠送原API文档:lucene-spatial-extras-7.3.1-javadoc.jar; 赠送源代码:lucene-spatial-extras-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras...
赠送jar包:lucene-spatial-extras-7.2.1.jar; 赠送原API文档:lucene-spatial-extras-7.2.1-javadoc.jar; 赠送源代码:lucene-spatial-extras-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras...
赠送jar包:lucene-spatial-extras-6.6.0.jar; 赠送原API文档:lucene-spatial-extras-6.6.0-javadoc.jar; 赠送源代码:lucene-spatial-extras-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras...
赠送jar包:lucene-backward-codecs-6.6.0.jar; 赠送原API文档:lucene-backward-codecs-6.6.0-javadoc.jar; 赠送源代码:lucene-backward-codecs-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...
赠送jar包:lucene-core-6.6.0.jar; 赠送原API文档:lucene-core-6.6.0-javadoc.jar; 赠送源代码:lucene-core-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-core-6.6.0.pom; 包含翻译后的API文档:lucene...
赠送jar包:lucene-spatial3d-6.6.0.jar; 赠送原API文档:lucene-spatial3d-6.6.0-javadoc.jar; 赠送源代码:lucene-spatial3d-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-spatial3d-6.6.0.pom; 包含...
赠送jar包:lucene-spatial3d-7.2.1.jar; 赠送原API文档:lucene-spatial3d-7.2.1-javadoc.jar; 赠送源代码:lucene-spatial3d-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-spatial3d-7.2.1.pom; 包含...
赠送jar包:lucene-analyzers-common-6.6.0.jar; 赠送原API文档:lucene-analyzers-common-6.6.0-javadoc.jar; 赠送源代码:lucene-analyzers-common-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-...
包括了xpdf-3.02pl4-win32.zip和xpdf-chinese-simplified.tar.gz用于Lucene对PDF的中文查询
赠送jar包:lucene-spatial3d-7.7.0.jar; 赠送原API文档:lucene-spatial3d-7.7.0-javadoc.jar; 赠送源代码:lucene-spatial3d-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-spatial3d-7.7.0.pom; 包含...
赠送jar包:lucene-spatial3d-7.3.1.jar; 赠送原API文档:lucene-spatial3d-7.3.1-javadoc.jar; 赠送源代码:lucene-spatial3d-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-spatial3d-7.3.1.pom; 包含...
赠送jar包:lucene-spatial-6.6.0.jar; 赠送原API文档:lucene-spatial-6.6.0-javadoc.jar; 赠送源代码:lucene-spatial-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-6.6.0.pom; 包含翻译后的API...