`
- 浏览:
258720 次
- 性别:
- 来自:
深圳
-
- Y_indexer.java建索引
-
packagecom.hapark.lucene;
-
importjava.io.BufferedReader;
-
importjava.io.File;
-
importjava.io.FileInputStream;
-
importjava.io.IOException;
-
importjava.io.InputStreamReader;
-
importjava.util.ArrayList;
-
importjava.util.Date;
-
importorg.apache.lucene.analysis.Analyzer;
-
importorg.apache.lucene.analysis.standard.StandardAnalyzer;
-
importorg.apache.lucene.document.Document;
-
importorg.apache.lucene.document.Field;
-
importorg.apache.lucene.index.IndexWriter;
-
publicclassY_indexer{
-
publicstaticvoidmain(String[]args)throwsException{
-
-
FilefileDir=newFile("d:\\y");
-
-
FileindexDir=newFile("d:\\index");
-
AnalyzerluceneAnalyzer=newStandardAnalyzer();
-
IndexWriterindexWriter=newIndexWriter(indexDir,luceneAnalyzer,
-
true);
-
ArrayListlist=newArrayList();
- getList(fileDir,list);
-
File[]textFiles=(File[])list.toArray(newFile[0]);
- System.out.println(textFiles.length);
-
longstartTime=newDate().getTime();
-
-
for(inti=0;i<textFiles.length;i++){
-
if(textFiles[i].isFile()
-
&&textFiles[i].getName().endsWith(".html")){
-
System.out.println("File"+textFiles[i].getCanonicalPath()
-
+"正在被索引....");
- Stringtemp=FileReaderAll(textFiles[i].getCanonicalPath(),
-
"GBK");
- System.out.println(temp);
-
Documentdocument=newDocument();
-
FieldFieldPath=newField("path",textFiles[i].getPath(),
- Field.Store.YES,Field.Index.NO);
-
FieldFieldBody=newField("body",temp,Field.Store.YES,
- Field.Index.TOKENIZED,
- Field.TermVector.WITH_POSITIONS_OFFSETS);
-
FieldFieldTitle=newField("title",temp,Field.Store.YES,
- Field.Index.TOKENIZED,
- Field.TermVector.WITH_POSITIONS_OFFSETS);
- document.add(FieldPath);
- document.add(FieldBody);
- document.add(FieldTitle);
- indexWriter.addDocument(document);
- }
- }
-
- indexWriter.optimize();
- indexWriter.close();
-
-
longendTime=newDate().getTime();
- System.out
-
.println("这花费了"
- +(endTime-startTime)
-
+"毫秒来把文档增加到索引里面去!"
- +fileDir.getPath());
- }
-
-
-
publicstaticvoidgetList(Filefile,ArrayListlist){
-
if(file.isDirectory()&&file.getName().indexOf(".")!=0){
- File[]file2=file.listFiles();
-
for(inti=0;i<file2.length;i++)
- getList(file2[i],list);
-
}else{
- list.add(file);
- }
- }
-
publicstaticStringFileReaderAll(StringFileName,Stringcharset)
-
throwsIOException{
-
BufferedReaderreader=newBufferedReader(newInputStreamReader(
-
newFileInputStream(FileName),charset));
-
Stringline=newString();
-
Stringtemp=newString();
-
while((line=reader.readLine())!=null){
- temp+=line;
- }
- reader.close();
-
returntemp;
- }
- }
- Y_searcherlucene搜索
-
packagecom.hapark.lucene;
-
importjava.io.IOException;
-
importjava.util.ArrayList;
-
importjava.util.Date;
-
importjava.util.List;
-
importorg.apache.lucene.analysis.Analyzer;
-
importorg.apache.lucene.analysis.standard.StandardAnalyzer;
-
importorg.apache.lucene.queryParser.ParseException;
-
importorg.apache.lucene.queryParser.QueryParser;
-
importorg.apache.lucene.search.Hits;
-
importorg.apache.lucene.search.IndexSearcher;
-
importorg.apache.lucene.search.Query;
-
publicclassY_searcher{
-
publicListsearch(){
-
ListsearchResult=newArrayList();
-
try{
-
Hitshits=null;
-
Stringkey="苏";
-
Queryquery=null;
-
IndexSearchersearcher=newIndexSearcher("d:\\yuyang");
-
Analyzeranalyzer=newStandardAnalyzer();
-
QueryParserqp=newQueryParser("title",analyzer);
- query=qp.parse(key);
-
if(searcher!=null){
-
Datestart=newDate();
-
hits=searcher.search(query);
-
if(hits.length()==0){
-
System.out.println("对不起。没你想要的结果!");
- }
-
else{
-
for(inti=0;i<hits.length();i++){
-
Dateend=newDate();
-
-
System.out.println("文件的路径:"+hits.doc(i).get("path"));
-
-
System.out.println(hits.doc(i).get("title"));
-
System.out.println("检索完成,用时"+(end.getTime()-start.getTime())+"毫秒");
- }
- }
- }
- }
-
catch(ParseExceptionex){
- }
-
catch(IOExceptione){
- }
-
returnsearchResult;
- }
-
publicstaticvoidmain(Stringargs[]){
-
Y_searchery_s=newY_searcher();
- y_s.search();
- }
- }
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
lucene 3.0 入门实例项目,迅速教会你怎么使用3.0API
lucene3.6入门实例教程 完整代码示例,lucene入门学习很好的资料
Lucene 简单入门程序实例
Lucene 创建索引查询的入门实例,可以直接导入工具
介绍Lucene的原理,处理流程,以及实例代码,全面分析介绍!
NULL 博文链接:https://cuics-100.iteye.com/blog/1057266
lucene实例是一个比较详细的例子,包括lucene的入门到高级实例,代码里有比较详细的实例,所有的实例都是通过junit来测试的。实例包括各种搜索:如通配符查询、模糊查询、查询结果的分页、中文分词器、自定义分词器...
NULL 博文链接:https://kylinsoong.iteye.com/blog/719415
包含最新lucene5.2.1的入门经典案例,以及程序中所需的lucene-core-5.2.1.jar、lucene-analyzers-common-5.2.1.jar等几个jar包。代码可运行。
Eclipse工程文件,全文检索 lucene-5.2.1 入门Eclipse工程实例,福利放送,与lucene3结果比对
这是一个朋友的学习成果,传到网上,大家分享,方便自己,方便别人. 作者:xiexie
Lucene3.0和Lucene2.0API有多处改动,该实例用Luence3.0实现
根据lucene demo 写的一个入门小例子,很小,希望对你有所帮助,不好别骂!
java工程的形式,lucene2.1.0的包也打在里面。 有注释的代码,向原有索引中追加内容,检索以查看。 适合初学者入门使用。
这是一份Lncene快速入门的文档,文档里包括入门的代码实例以及工作原理和查询设置等一些常用到的Lucene知识
Luene2.4版本在以前的基础上进行了不少的改动,性能上也提升了不少,这里边学习边写了一个简单的例子,给初学者行个方便!
一个关于lucene实例初学者入门的好东东
基于lucene5.3.1的项目实例 lucene创建索引,删除索引,更新索引,查找索引。