前提
lucene有7个包需要导入:analysis,document,index,queryParser,search,store,util
solr和lucene对比:
首先Solr是基于Lucene做的,Lucene是一套信息检索工具包,但并不包含搜索引擎系统,它包含了索引结构、读写索引工具、相关性工具、排序等功能,因此在使用Lucene时你仍需要关注搜索引擎系统,例如数据获取、解析、分词等方面的东西。
而Solr的目标是打造一款企业级的搜索引擎系统,因此它更接近于我们认识到的搜索引擎系统,它是一个搜索引擎服务,通过各种API可以让你的应用使用搜索服务,而不需要将搜索逻辑耦合在应用中。而且Solr可以根据配置文件定义数据解析的方式,更像是一个搜索框架,它也支持主从、热换库等操作。还添加了飘红、facet等搜索引擎常见功能的支持。
因而,Lucene使用上更加灵活,但是你需要自己处理搜素引擎系统架构,以及其他附加附加功能的实现。而Solr帮你做了更多,但是是一个处于高层的框架,Lucene很多新特性不能及时向上透传,所以有时候可能发现需要一个功能,Lucene是支持的,但是Solr上已经看不到相关接口。
Lucene更像是一个SDK。 有完整的API族以及对应的实现。你可以利用这些在自己的应用里实现高级查询(基于倒排索引技术的),Lucene对单机或者桌面应用很实用很方便。
但是Lucene,需要开发者自己维护索引文件,在多机环境中备份同步索引文件很是麻烦。于是,就有了Solr。
packagecom.lucene;
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileReader;
import java.io.IOException;
importjava.io.InputStreamReader;
importjava.util.ArrayList;
importjava.util.Date;
importjava.util.List;
importorg.apache.lucene.analysis.Analyzer;
importorg.apache.lucene.analysis.standard.StandardAnalyzer;
importorg.apache.lucene.document.Document;
importorg.apache.lucene.document.Field;
importorg.apache.lucene.document.Field.Index;
importorg.apache.lucene.document.Field.Store;
importorg.apache.lucene.index.IndexReader;
importorg.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
importorg.apache.lucene.queryParser.QueryParser;
importorg.apache.lucene.search.IndexSearcher;
importorg.apache.lucene.search.Query;
importorg.apache.lucene.search.TopDocs;
importorg.apache.lucene.store.Directory;
importorg.apache.lucene.store.SimpleFSDirectory;
importorg.apache.lucene.util.Version;
public class SearchFile {
public void creatIndex(){
File fileDir = new File("D://s");
File indexDir = new File("D://index");
Version matchVersion = Version.LUCENE_36;
//创建需要的分类器
Analyzer luceneAnalyzer = new StandardAnalyzer(matchVersion);
//创建版本为36的IndexWriterConfig
IndexWriterConfig iwconfig = new IndexWriterConfig(matchVersion,luceneAnalyzer);
IndexWriter indexWriter = null;
try{
//创建目录
Directory direct = new SimpleFSDirectory(indexDir);
//创建索引文件
indexWriter = new IndexWriter(direct, iwconfig);
File[] textFiles = fileDir.listFiles();
//开始的测试时间
long startTime = new Date().getTime();
for (int i = 0; i < textFiles.length; i++) {
BufferedReader bufferread = null;
StringBuffer sbuffer = null;
FileReader fileread = null;
if(textFiles[i].isFile() &&textFiles[i].getName().endsWith(".txt")){
System.out.println("----------------File"+textFiles[i].getCanonicalPath()+"正在被索引!------------");
try{
fileread = new FileReader(textFiles[i]);
bufferread = new BufferedReader(fileread);
sbuffer = new StringBuffer();
String str = "";
while((str=bufferread.readLine()) != null){
sbuffer.append(str);
}
System.out.println(sbuffer.toString());
}catch (IOException e){
fileread.close();
bufferread.close();
}
Document doc = new Document();
doc.add(new Field("filename", textFiles[i].getPath(), Store.YES, Index.ANALYZED));
doc.add(new Field("body", sbuffer.toString(), Store.YES, Index.ANALYZED));
indexWriter.addDocument(doc);
}
}
//结束时间
long endTime = new Date().getTime();
System.out.println("-------------添加索引需要的时间"+(endTime-startTime)+"毫秒---------------");
}catch (Exception e){
e.printStackTrace();
}finally{
if(indexWriter != null){
try {
indexWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public void searchIndex() throws Exception{
String parstr = "";
List<SearchBean> list = new ArrayList<SearchBean>();
while(true){
System.out.println("请输入你要查找的内容:“Enter”,结束请不输入");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
parstr = br.readLine();
if(parstr.equals("")){
System.out.println("退出系统!!");
break;
}
System.out.println("--------您输入了:"+parstr+"--------");
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
QueryParser parser = new QueryParser(Version.LUCENE_36, "body", analyzer);
Directory direct = new SimpleFSDirectory(new File("D://index"));
IndexReader indexreader = IndexReader.open(direct);
IndexSearcher searcher = new IndexSearcher(indexreader);
Query query = parser.parse(parstr);
TopDocs top = searcher.search(query, 1000);
int num = top.totalHits;
int k = 0;
System.out.println("-----------------一共有【"+num+"】条记录---------------");
for (int i = 0; i < num; i++) {
SearchBean bean = new SearchBean();
k = top.scoreDocs[i].doc;
Document doc =indexreader.document(k);
bean.setBody(doc.get("body"));
bean.setPath(doc.get("filename"));
System.out.println("-----------------文件路径:"+doc.get("filename")+" "+"文件内容:"+doc.get("body")+"------------------------");
list.add(bean);
}
}
}
public static void main(String[] args) {
/*new SearchFile().creatIndex();*/
try {
new SearchFile().searchIndex();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//boost默认值为1.0,值越大搜索越靠前
//tagField.setBoost(4.0f);
//titleField.setBoost(2.0f);
lucene-analyzers-3.6.2.jar
lucene-core-3.6.2.jar
lucene-smartcn-3.6.2.jar
lucene要求我们首先创建一个索引然后根据索引来搜索全文,在这里我们首先要creatIndex(),然后再执行searchindex()