package com.java.lucene;
import java.io.StringReader;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.List;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
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.index.Term;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
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.Fragmenter;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.apache.lucene.search.highlight.SimpleSpanFragmenter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import com.java.entity.Blog;
import com.java.util.StringUtil;
public class IndexBuilder {
private Directory dir=null;
/**
* 获取IndexWrite
* @return
* @throws Exception
*/
public IndexWriter getIndexWriter()throws Exception{
dir=FSDirectory.open(Paths.get("C://lucene"));
SmartChineseAnalyzer analyzer=new SmartChineseAnalyzer();
IndexWriterConfig config=new IndexWriterConfig(analyzer);
IndexWriter writer=new IndexWriter(dir, config);
return writer;
}
/**
* 给blog添加索引
* @param blog
* @throws Exception
*/
public void addIndexWriter(Blog blog)throws Exception{
IndexWriter writer=getIndexWriter();
Document document=new Document();
document.add(new StringField("id",String.valueOf(blog.getId()),Field.Store.YES));
document.add(new TextField("content",blog.getContentNoTag(),Field.Store.YES));
document.add(new TextField("title",blog.getTitle(),Field.Store.YES));
writer.addDocument(document);
writer.close();
}
/**
* 更新博客索引
* @param blog
* @throws Exception
*/
public void updateIndexWriter(Blog blog)throws Exception{
IndexWriter writer=getIndexWriter();
Document document=new Document();
document.add(new StringField("id",String.valueOf(blog.getId()),Field.Store.YES));
document.add(new TextField("content",blog.getContentNoTag(),Field.Store.YES));
writer.updateDocument(new Term("id",String.valueOf(blog.getId())), document);
writer.close();
}
/**
* 删除指定博客索引
* @param blogId
* @throws Exception
*/
public void deleteIndexWriter(String blogId)throws Exception{
IndexWriter writer=getIndexWriter();
writer.deleteDocuments(new Term("id", blogId));
writer.forceMergeDeletes();
writer.close();
}
/**
* 查询博客
* @return
* @throws Exception
*/
public List<Blog> searchIndexWriter(String q)throws Exception{
dir=FSDirectory.open(Paths.get("C://lucene"));
IndexReader reader = DirectoryReader.open(dir);
IndexSearcher is=new IndexSearcher(reader);
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
SmartChineseAnalyzer analyzer=new SmartChineseAnalyzer();
QueryParser parser=new QueryParser("title",analyzer);
Query query=parser.parse(q);
QueryParser parser2=new QueryParser("content",analyzer);
Query query2=parser2.parse(q);
booleanQuery.add(query,BooleanClause.Occur.SHOULD);
booleanQuery.add(query2,BooleanClause.Occur.SHOULD);
TopDocs hits=is.search(booleanQuery.build(), 100);
QueryScorer scorer=new QueryScorer(query);
Fragmenter fragmenter = new SimpleSpanFragmenter(scorer);
SimpleHTMLFormatter simpleHTMLFormatter=new SimpleHTMLFormatter("<b><font color='red'>","</font></b>");
Highlighter highlighter=new Highlighter(simpleHTMLFormatter, scorer);
highlighter.setTextFragmenter(fragmenter);
List<Blog> blogList=new LinkedList<Blog>();
for(ScoreDoc scoreDoc:hits.scoreDocs){
Document doc=is.doc(scoreDoc.doc);
Blog blog=new Blog();
blog.setId(Integer.parseInt(doc.get(("id"))));
String title=doc.get("title");
String content=doc.get("content");
if(title!=null){
TokenStream tokenStream = analyzer.tokenStream("title", new StringReader(title));
String hTitle=highlighter.getBestFragment(tokenStream, title);
if(StringUtil.isEmpty(hTitle)){
blog.setTitle(title);
}else{
blog.setTitle(hTitle);
}
}
if(content!=null){
TokenStream tokenStream = analyzer.tokenStream("content", new StringReader(content));
String hContent=highlighter.getBestFragment(tokenStream, content);
if(StringUtil.isEmpty(hContent)){
if(content.length()<=200){
blog.setContent(content);
}else{
blog.setContent(content.substring(0, 200));
}
}else{
blog.setContent(hContent);
}
}
blogList.add(blog);
}
return blogList;
}
}
lucene学习
最新推荐文章于 2021-01-17 21:57:17 发布