java搜索引擎

//在C盘下创建一个s的文件夹,然后在s文件夹中新建三个文件:1.txt,3.txt,2.txt,,1.txt中输入"想 飞的我",2.txt中输入"想飞",3.txt中输入"我".接下来我们来看下面的测试:

package test;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Date;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.LockObtainFailedException;
//需要的apache下的lucene包
/**
* 创建索引文件
*/
public class CreateIndex {
public static boolean createDocumentIndex()
{
boolean bool = false;
try{
//需要索引的目录
File dirpath = new File("c:\\s");
//将索引文件存放在index目录下
File indexpath = new File("c:\\index");
//创建分词器
Analyzer analyzer = new StandardAnalyzer();
IndexWriter index = new IndexWriter(indexpath,analyzer,true);

File[] txtfiles = dirpath.listFiles();

long starttime = new Date().getTime();

for(int i=0;i<txtfiles.length;i++)
{
if(txtfiles[i].isFile())
{
System.out.println("文件"+txtfiles[i].getCanonicalPath()+"正在索引中...");
Reader read = new FileReader(txtfiles[i]);
Document doc = new Document();
doc.add(new Field("content",read));
doc.add(new Field("path",txtfiles[i].getAbsolutePath(),Field.Store.YES,Field.Index.NO));
index.addDocument(doc);
}
}
index.optimize();
index.close();
long endTime = new Date().getTime();
System.out
.println("这花费了"
+ (endTime - starttime)
+ " 毫秒来把文档增加到索引里面去!"
+ dirpath.getPath());
bool=true;
}
catch(Exception e)
{
bool = false;
}
return bool;
}
/**
* 测试
*/
public static void main(String[] a)
{
CreateIndex.createDocumentIndex();
}
}

测试结果:

文件C:\s\1.txt正在索引中...
文件C:\s\2.txt正在索引中...
文件C:\s\3.txt正在索引中...
这花费了141 毫秒来把文档增加到索引里面去!c:\index


package test;

import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
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.search.Query;
/**
* 从索引文件查询内容
* @author Jan
*
*/
public class QueryString
{
public static void main(String[] args) throws IOException, ParseException {
Hits hits = null;
String queryString = "我";
Query query = null;
IndexSearcher searcher = new IndexSearcher("c:\\index");

Analyzer analyzer = new StandardAnalyzer();
try {
QueryParser qp = new QueryParser("content", analyzer);
query = qp.parse(queryString);
} catch (ParseException e) {
}
if (searcher != null) {
hits = searcher.search(query);
if (hits.length() > 0) {
System.out.println("找到:" + hits.length() + " 个结果!");
}
}
}
}


测试结果:

找到:2 个结果!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值