lucene3.0 学习总结

近日,一直在学习lucene。应用lucene3.0编写了一段简单的代码,实现了搜索引擎最基本的功能:索引和检索。但在进行检索的时候出现了问题:在把field加入到segment后(doc1.add(new Field("content","matter bird scan cancer scan matter",Store.YES,Index.NOT_ANALYZED));),明明有两个"scan",可是在检索的时候返回的hits只有一个,请问为什么?
哪位好心人能帮我解决一下,我是个初学者,望多指教,在此拜过。


package document;

import java.io.File;
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.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;

public class fileDoc {
private Directory index_dir = null;
private String index_path = "c:\\index";
private IndexWriter write = null;
private IndexSearcher indexSearch = null;
public fileDoc() throws CorruptIndexException, LockObtainFailedException, IOException{
File file = new File(index_path);
index_dir = SimpleFSDirectory.open(file);//一般用SimpleFSDirectory创建索引目录
write = new IndexWriter(index_dir,new StandardAnalyzer(Version.LUCENE_30),true,MaxFieldLength.LIMITED);
indexSearch = new IndexSearcher(index_dir);
}

@Test
public void createIndex() throws CorruptIndexException, IOException{
//往哪儿建立索引
Document doc1 = new Document();
doc1.add(new Field("content","matter bird scan cancer scan matter",Store.YES,Index.NOT_ANALYZED));
doc1.add(new Field("title","doca",Store.YES,Index.ANALYZED));

write.addDocument(doc1);

write.close();//只有调用了close方法后,索引器才会将存放于内存中的所有内容写入磁盘,并关闭输出流。否则索引目录下将只有segment文件

}
@Test
public void search() throws CorruptIndexException, IOException, ParseException{
//生成Query对象
Query query = new BooleanQuery();
String key1 = "scan";

//检索
QueryParser queryParser = new QueryParser(Version.LUCENE_30, "content", new StandardAnalyzer(Version.LUCENE_30));
query = queryParser.parse(key1);
TopScoreDocCollector collector = TopScoreDocCollector.create(100, true);//有序排列
indexSearch.search(query, collector);
System.out.println("查询到该关键词有:"+collector.getTotalHits()+"个");

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值