您的位置:首页 > 其它

一个简单的Lucene例子

2011-08-15 16:20 435 查看
/**

* 只是简单的一个关键词的搜索

* 应用范围狭窄 只能搜索txt文件

* 不支持中文

* 只能搜索独立的关键词 如关键词lucene

* 不能搜索出luceneinaction lucene.txt等连载一起的字符关键词

*/

package com.cn.ciecc.gfa;

import java.io.File;

import java.io.FileReader;

import java.io.IOException;

import java.util.Date;

import org.apache.lucene.analysis.standard.StandardAnalyzer;

import org.apache.lucene.document.DateTools;

import org.apache.lucene.document.Document;

import org.apache.lucene.document.Field;

import org.apache.lucene.index.IndexWriter;

import org.apache.lucene.store.Directory;

import org.apache.lucene.store.SimpleFSDirectory;

import org.apache.lucene.util.Version;

/**

* <p>Title:Indexer.java</p>

* <p>Description:创建索引</p>

* <p>2008 All Rights Reserved. 北京国富安电子商务安全认证有限公司 版权所有</p>

* <p>Company: GFA</p>

* @author 袁振朝

* @version 1.0

* @Date 2011-8-15 下午02:00:39

*/

public class Indexer {

public static void main(String[] args) throws IOException {

//保存索引文件的地方

String indexDir = "D:\\workspace\\LucenceTest\\src\\com\\cn\\ciecc\\dir\\indexDir";

//将要搜索txt文件的地方

String dateDir = "D:\\workspace\\LucenceTest\\src\\com\\cn\\ciecc\\dir\\dateDir";

IndexWriter indexWriter = null;

//创建Directory对象

Directory dir = new SimpleFSDirectory(new File(indexDir));

//创建IndexWriter对象 第一个参数是Directory 第二个是分词器 第三个表示是否创建

//如果是false表示在此基础上进行修改,第四个表示分词的最大值,比如说 new MaxFieldlength(2)

//就表示两个字一分 一般用IndexWriter.MaxFiledLength.LIMITED

indexWriter = new IndexWriter(dir,new StandardAnalyzer(Version.LUCENE_33),true,IndexWriter.MaxFieldLength.LIMITED);

File[] files = new File(dateDir).listFiles();

for (int i = 0; i < files.length; i++) {

Document doc = new Document();

//创建Field对象,并放入doc对象中

doc.add(new Field("contents", new FileReader(files[i])));

doc.add(new Field("filename", files[i].getName(),Field.Store.YES,Field.Index.ANALYZED_NO_NORMS));

doc.add(new Field("indexDate", DateTools.dateToString(new Date(), DateTools.Resolution.DAY),Field.Store.YES,Field.Index.ANALYZED_NO_NORMS));

System.out.println("filename:"+files[i].getName());

System.out.println("indexDate:"+DateTools.dateToString(new Date(), DateTools.Resolution.DAY));

//写入IndexWriter

indexWriter.addDocument(doc);

}

//查看IndexWriter里面有多少个索引

System.out.println("numDocs"+indexWriter.numDocs());

//indexWriter.commit(); //如果indexWriter不commit活着不close 会致使添加的文档不能够被打开的IndexReader或IndexSearcher看到 导致检索不出内容

indexWriter.close();

}

}

/**

* 只是简单的一个关键词的搜索

* 应用范围狭窄 只能搜索txt文件

* 不支持中文

* 只能搜索独立的关键词 如关键词lucene

* 不能搜索出luceneinaction lucene.txt等连载一起的字符关键词

*/

package com.cn.ciecc.gfa;

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.index.Term;

import org.apache.lucene.queryParser.ParseException;

import org.apache.lucene.queryParser.QueryParser;

import org.apache.lucene.search.IndexSearcher;

import org.apache.lucene.search.Query;

import org.apache.lucene.search.ScoreDoc;

import org.apache.lucene.search.TermQuery;

import org.apache.lucene.search.TopDocs;

import org.apache.lucene.store.Directory;

import org.apache.lucene.store.SimpleFSDirectory;

import org.apache.lucene.util.Version;

/**

* <p>Title:Seacher.java</p>

* <p>Description:搜索索引</p>

* <p>2008 All Rights Reserved. 北京国富安电子商务安全认证有限公司 版权所有</p>

* <p>Company: GFA</p>

* @author 袁振朝

* @version 1.0

* @Date 2011-8-15 下午02:23:51

*/

public class Seacher {

public static void main(String[] args) throws IOException,ParseException {

//保存索引文件的地方

String indexDir = "D:\\workspace\\LucenceTest\\src\\com\\cn\\ciecc\\dir\\indexDir";

Directory directory = new SimpleFSDirectory(new File(indexDir));

//创建IndexSeacher 对象,相比IndexWriter对象,这个参数就要提供一个索引的目录就行了

IndexSearcher indexSearcher = new IndexSearcher(directory);

//创建QueryPaerser对象 第一个参数表示Lucence的版本,第二个表示搜索field的字段,第三个表示搜索使用的分词器

QueryParser queryParser = new QueryParser(Version.LUCENE_33,"filename",new StandardAnalyzer(Version.LUCENE_33));

//生成Query对象

Query query = queryParser.parse("lucene");

/*//生成Query对象

Query query = new TermQuery(new Term("filename","lucene"));*/

//搜索结果 topDocs 里面有scoreDocs][] 里面保存着索引值

TopDocs hits = indexSearcher.search(query, 10);

//hits.totalHits表示一共搜索了多少

System.out.println("找到了"+hits.totalHits+"个");

//循环hits.scoreDocs数据,并使用indexSearch.doc方法把Document还原,再拿出对应的字段的值

for (int i = 0; i < hits.totalHits; i++) {

ScoreDoc sDoc = hits.scoreDocs[i];

Document doc = indexSearcher.doc(sDoc.doc);

System.out.println(doc.get("filename"));

}

indexSearcher.close();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: