欢迎转载http://www.cnblogs.com/shizhongtao/p/3440479.html
接着上一篇,这里继续搜索,对于搜索和创建一样,首先你要确定搜索位置,然后用规定的类来读取。还要注意一点,确定分词器,因为不同的分词器所创建的分词规则不同。上篇我使用的是默认的分词器,这里我也先不管分词器。为了方便阅读,代码就全部粘上。
package com.bing.test; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException; 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.Store;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version; /**
* @author bingyulei
*
*/
public class HelloLucene
{ Directory directory = null;
Document doc;
IndexWriter writer = null; /**
*
* @param indexWriterPath
* 索引创建路径
* @param filePath
* 读取文件路径
*/
public void createIndex(String indexWriterPath, String filePath)
{ // 创建indexwriter
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_45);// 设置标准分词器
// ,默认是一元分词
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_45,
analyzer);// 设置IndexWriterConfig try
{
// 创建directory
// directory=RAMDirectory();//创建在内存中
// 创建在硬盘上
directory = FSDirectory.open(new File(indexWriterPath));// 打开存放索引的路径
writer = new IndexWriter(directory, iwc); // 为document添加field
addFile(writer, filePath); System.out.println("添加成功");
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} } private void addFile(IndexWriter writer, String filePath)
{
File f = new File(filePath);
FieldType ft = new FieldType();
ft.setIndexed(true);// 索引
ft.setStored(true);// 存储,数据量比较大,一般都是不鼓励存储,放在索引文件中会把索引文件撑大
ft.setTokenized(true);
for (File file : f.listFiles())
{
try
{
// 创建Document对象
doc = new Document();
// doc.add(new Field("content", new FileReader(file), ft));
doc.add(new TextField("content", new FileReader(file)));
doc.add(new TextField("filename", file.getName(), Store.YES));
doc.add(new StringField("path", file.getPath(), Store.YES));
// 添加文档
writer.addDocument(doc);
writer.commit();// 提交数据
} catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} }
} /**
* 搜索
*
* @param path
* 搜索路径
* @param indexReaderPath
* 索引存放路径
*/
public void seacher(String indexReaderPath, String searthText)
{
IndexReader reader=null;
try
{
directory = FSDirectory.open(new File(indexReaderPath));
// 创建读取索引的reader
reader = DirectoryReader.open(directory);
// 根据reader创建search
IndexSearcher searcher = new IndexSearcher(reader);
// 创建查询,第二个参数表示查询的字段名,第三个是分词器
QueryParser parser = new QueryParser(Version.LUCENE_45, "content",
new StandardAnalyzer(Version.LUCENE_45));
// 搜索包含searthText的内容
Query query = parser.parse(searthText);
// 搜索返回10条记录
TopDocs tds = searcher.search(query, 10); //获取scoredoc对象组,
ScoreDoc[] sds=tds.scoreDocs;
for(ScoreDoc sd:sds){
//获取具体的doc
Document doc=searcher.doc(sd.doc);
System.out.println(doc.get("filename")+":"+doc.get("path"));
}
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}// 打开存放索引的路径
catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (reader!=null)
{
try
{
reader.close();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
说明,"D:\\lucene\\file"是我复制lucene官方文档上的两段话,不过当你创建完索引之后,然后再修改文件内容,新加的内容并不能搜索出来。这个应该很好理解。
然后进行测试:searchTest,就可以得到那个文本文件中有"Changing Similarity"这段字符
package com.bing.test; import org.junit.Test; public class HelloLuceneTest
{
@Test
public void writertest(){
HelloLucene test=new HelloLucene();
test.createIndex("D:\\lucene\\index","D:\\lucene\\file");
}
@Test
public void searchTest(){
HelloLucene test=new HelloLucene();
test.seacher("D:\\lucene\\index", "Changing Similarity");
}
}