lucene全文检索小例子

引入jar包::


lucene-analyzers-3.6.1.jar


lucene-core-3.6.1.jar


lucene-highlighter-3.6.1.jar (高亮效果)






首先,将需要检索的数据用内定的方式创建索引,(这里创建索引保存在硬盘)


1、新建一个config.properties


#路径
indexPath=C:/lucene-doc


2、建一个Configuration类:


import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;


public class Configuration {

//采用单例模式
private static final Configuration configuration = new Configuration();


private Configuration(){}

public synchronized static Configuration getInstance(){
return configuration;
}

public String read(String properties,String key){
//读取配置文件
InputStream in = this.getClass().getClassLoader().getResourceAsStream(properties);
Properties p = new Properties();
try {
p.load(in);
} catch (IOException e) {
e.printStackTrace();
}

//取得配置文件中的值
return p.getProperty(key);
}
}


3、创建索引:


/**
* 创建索引
*/
public String createSearch() throws Exception {
System.out.println("开始创建索引。。。");
long stime = new Date().getTime();
String indexPath = Configuration.getInstance().read("config.properties", "indexPath");
Directory dir = FSDirectory.open(new File(indexPath));
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_36, analyzer);
iwc.setOpenMode(OpenMode.CREATE); //即创建新索引文件OpenMode.CREATE_OR_APPEND表示创建或追加到已有索引文件
IndexWriter writer = new IndexWriter(dir, iwc);

//需要建立 索引的数据
List users = userService.getAll(); //从数据库获取数据

for(User u : users){
Document doc = new Document();
// int 要转换 String
doc.add(new Field("id",String.valueOf(u.getId()), Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("username",u.getUsername(), Field.Store.YES, Field.Index.ANALYZED));


writer.addDocument(doc);
}
writer.close();
long endTime = new Date().getTime();
System.out.println("这花费了 " + (endTime - stime)+ "毫秒来把数据增加到索引"+indexPath+"里面去!");
return SUCCESS;
}


结果如下:


开始创建索引。。。
这花费了 1285毫秒来把数据增加到索引C:/lucene-doc里面去!


4、根据关键字,检索:


/**
* 从Lucene索引库中——搜索
*/
public String searchKeyword() throws Exception {
System.out.println("开始检索。。。");
long startTime = new Date().getTime();
String indexPath = Configuration.getInstance().read("config.properties", "indexPath");
users = new ArrayList();

IndexReader reader = IndexReader.open(FSDirectory.open(new File(indexPath)));
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
String field = "username"; //设置一个默认的field,因为在用户没有指定field时,lucene会在默认的field中检索
QueryParser parser = new QueryParser(Version.LUCENE_36, field, analyzer);
Query query = parser.parse(keyword); //搜索关键词
searcher.search(query, null, 100);
TopDocs results = searcher.search(query, 10); //只取排名前10的搜索结果
ScoreDoc[] hits = results.scoreDocs;
Document doc = null;
for(ScoreDoc scorceDoc : hits){
doc = searcher.doc(scorceDoc.doc);
User u = new User();
u.setId(Integer.parseInt(doc.get("id")));
//u.setUsername(doc.get("username")); //不高亮
u.setUsername(this.getHighLight(doc, analyzer, query, "username")); //使用高亮
users.add(u);
}
searcher.close();
reader.close();
long endTime = new Date().getTime();
System.out.println("检索花费了 " + (endTime - startTime)+ "毫秒来把数据从"+indexPath+"里面检索出来!");
for(User u : users){
System.out.println("以下是检索结果:"+u.getUsername()+"---"+u.getId());


}
return SUCCESS;
}


5、高亮方法


public String getHighLight(Document doc,Analyzer analyzer,Query query,String field)throws Exception{
//设置高亮显示格式
SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("", "");
/* 语法高亮显示设置 */
Highlighter highlighter = new Highlighter(simpleHTMLFormatter,new QueryScorer(query));
highlighter.setTextFragmenter(new SimpleFragmenter(100));
// 取 field 字段值,准备进行高亮
String fieldValue = https://www.it610.com/article/doc.get(field);
TokenStream tokenStream = analyzer.tokenStream(field,new StringReader(fieldValue));
//转成高亮的值
String highLightFieldValue = https://www.it610.com/article/highlighter.getBestFragment(tokenStream, fieldValue);
if(highLightFieldValue =https://www.it610.com/article/= null)
highLightFieldValue = https://www.it610.com/article/fieldValue;
return highLightFieldValue;
}


PS:还有几个全局变量:


private String keyword;
private List users;


以上代码是在ssh框架中实现的,代码不好打包上传。

    推荐阅读