您的位置:首页 > 数据库 > Oracle

oracle学习笔记-1

2013-11-10 20:25 323 查看
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
本系列教程如果没有明确指出都是沿用此版本
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: