package cn.zhangao.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
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.search.highlight.Formatter;
import org.apache.lucene.search.highlight.Fragmenter;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.Scorer;
import org.apache.lucene.search.highlight.SimpleFragmenter;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.apache.lucene.util.Version;
import org.springframework.stereotype.Service;
import cn.zhangao.cfg.Configuration;
import cn.zhangao.domain.Post;
import cn.zhangao.domain.Topic;
import cn.zhangao.lucene.dao.LuceneDao;
import cn.zhangao.service.TopicService;
import cn.zhangao.utils.IndexUtils;
@Service
@SuppressWarnings("unchecked")
public class TopicServiceImpl extends BaseServiceImpl<Topic> implements TopicService {
@Resource
protected LuceneDao luceneDao;
@Override
public void save(Topic topic) {
topic.setPostTime(new Date());
topic.setLastUpdateTime(topic.getPostTime());
this.getSession().save(topic);
luceneDao.save(topic);
}
@Override
public List<Topic> getAll() {
return this.getSession().createQuery("FROM Topic t ORDER BY t.lastUpdateTime DESC")//
.list();
}
@Override
public List<Topic> searchTopic(String queryString) {
IndexSearcher indexSearcher = null;
try {
indexSearcher = new IndexSearcher(Configuration.directory);
String[] fields = { "title", "content" };
MultiFieldQueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_30, fields, Configuration.analyzer);
Query query = queryParser.parse(queryString);
TopDocs topDocs = indexSearcher.search(query, 100);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
//配置高亮器
Formatter formatter = new SimpleHTMLFormatter("<font color='red'>", "</font>");
Scorer scorer = new QueryScorer(query);
Highlighter highlighter = new Highlighter(formatter, scorer);
//设置摘要
Fragmenter fragmenter = new SimpleFragmenter(20);
highlighter.setTextFragmenter(fragmenter);
List<Topic> topicList = new ArrayList();
for (int i = 0; i < scoreDocs.length; i++) {
ScoreDoc scoreDoc = scoreDocs[i];
Document document = indexSearcher.doc(scoreDoc.doc);
String text = highlighter.getBestFragment(Configuration.analyzer, "title", document.get("title"));
if (text != null) {
document.getField("title").setValue(text);
}
text = highlighter.getBestFragment(Configuration.analyzer, "content", document.get("content"));
if (text != null) {
document.getField("content").setValue(text);
}
topicList.add(IndexUtils.parseTopic(document));
}
return topicList;
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
if (indexSearcher != null)
indexSearcher.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}