⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 luceneindeximpl.java

📁 一个实用的CMS管理
💻 JAVA
字号:
package com.suncms.service.lucene;

import java.io.StringReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleFragmenter;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import com.suncms.domain.Article;
import com.suncms.domain.SearchResult;
import com.suncms.exception.SuncmsException;
import com.suncms.persistence.iface.ArticleDao;
import com.suncms.persistence.sqlmapdao.ArticleSqlMapDao;
import com.suncms.service.ibatis.ArticleService;

public class LuceneIndexImpl implements LuceneIndex{

	private ArticleDao articleDao; 
	
	
	public ArticleDao getArticleDao() {
		return articleDao;
	}

	public void setArticleDao(ArticleDao articleDao) {
		this.articleDao = articleDao;
	}

	public  void makeIndex(Document doc, String indexDir) throws SuncmsException {
		try{
		List<Document> aList = new ArrayList<Document>();
		aList.add(doc);
		makeIndex(aList, indexDir);
		}catch(Exception e){
			throw new SuncmsException("error in LuceneIndexImpl.addNewArticleClass()");
		}
	}

	public  void makeIndex(List<Document> docs, String indexDir) throws SuncmsException {
		if (null == docs) {
			return;
		}
		boolean indexExist = indexExist(indexDir);
		IndexWriter writer = null;
		try {
			StandardAnalyzer analyzer = new StandardAnalyzer();
			if (indexExist) {
				writer = new IndexWriter(indexDir, analyzer, false);
			} else {
				writer = new IndexWriter(indexDir, analyzer, true);
			}
			for (int i = 0; i < docs.size(); i++) {
				Document doc = (Document) docs.get(i);
				if (null != doc) {
					writer.addDocument(doc);
				}
			}
			writer.optimize();
		}catch(Exception e){
			throw new SuncmsException("error in LuceneIndexImpl.addNewArticleClass()");
		} finally {
			try {
				if (null != writer) {
					writer.close();
				}
			}catch(Exception e){
				throw new SuncmsException("error in LuceneIndexImpl.addNewArticleClass()");
			}
		}
	}

	public  boolean indexExist(String indexDir) throws SuncmsException {
		try{
		return IndexReader.indexExists(indexDir);
		}catch(Exception e){
			throw new SuncmsException("error in LuceneIndexImpl.addNewArticleClass()");
		}
	}

	public  Document makeNewsSearchDocument(Article article) throws SuncmsException {
		try{
		Document doc = new Document();
		doc.add(new Field("nid", article.getRow_id(), Field.Store.YES,
				Field.Index.UN_TOKENIZED));
		doc.add(new Field("title", article.getArticle_title(), Field.Store.YES,
				Field.Index.TOKENIZED));
		// String content = parseHtmlContent(article.getArticle_content());
		doc.add(new Field("content", article.getArticle_content(),
				Field.Store.NO, Field.Index.TOKENIZED));
		doc.add(new Field("file", article.getArticle_file(), Field.Store.YES,
				Field.Index.UN_TOKENIZED));
		doc.add(new Field("addtime", new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(article.getModify_time()),
				Field.Store.YES, Field.Index.UN_TOKENIZED));
		
		return doc;
		}catch(Exception e){
			throw new SuncmsException("error in LuceneIndexImpl.addNewArticleClass()");
		}
	}

	public  void makeNewsInfoIndex(Article article, String indexDir) throws SuncmsException {
		if (null == article) {
			return;
		}
		try{
		makeIndex(makeNewsSearchDocument(article), indexDir);
		}catch(Exception e){
			throw new SuncmsException("error in LuceneIndexImpl.addNewArticleClass()");
		}
	}

	public  void deleteIndex(Term aTerm, String indexDir) throws SuncmsException {
		try{
		List<Term> aList = new ArrayList<Term>();
		aList.add(aTerm);
		deleteIndex(aList, indexDir);
		}catch(Exception e){
			throw new SuncmsException("error in LuceneIndexImpl.addNewArticleClass()");
		}
	}

	public  void deleteIndex(List<Term> terms, String indexDir) throws SuncmsException {
		if (null == terms) {
			return;
		}

		if (!indexExist(indexDir)) {
			return;
		}
		IndexReader reader = null;
		try {
			reader = IndexReader.open(indexDir);
			for (int i = 0; i < terms.size(); i++) {
				Term aTerm = (Term) terms.get(i);
				if (null != aTerm) {
					reader.deleteDocuments(aTerm);
				}
			}
		}catch(Exception e){
			throw new SuncmsException("error in LuceneIndexImpl.addNewArticleClass()");
		}finally {
			try {
				if (null != reader) {
					reader.close();
				}
			}catch(Exception e){
				throw new SuncmsException("error in LuceneIndexImpl.addNewArticleClass()");
			}
		}
	}

	public  void deleteNewsInfoIndex(String nid, String indexDir) throws SuncmsException {
		try{
		Term aTerm = new Term("nid", nid);
		deleteIndex(aTerm, indexDir);
		}catch(Exception e){
			throw new SuncmsException("error in LuceneIndexImpl.addNewArticleClass()");
		}
	}

	public  void updateNewsInfoIndex(Article article, String indexDir) throws SuncmsException {
		if (null == article) {
			return;
		}
		try{
		deleteNewsInfoIndex(article.getRow_id(), indexDir);
		makeNewsInfoIndex(article, indexDir);
		}catch(Exception e){
			throw new SuncmsException("error in LuceneIndexImpl.addNewArticleClass()");
		}
	}

	public  void makeAllNewsInfoIndex(List<Article> newsList,
			String indexDir) throws SuncmsException {
		try{
		List<Term> terms = new ArrayList<Term>();
		List<Document> docs = new ArrayList<Document>();
		for (int i = 0; i < newsList.size(); i++) {
			Article aitem = (Article) newsList.get(i);
			if (null != aitem) {
				terms.add(new Term("nid", aitem.getRow_id()));
				docs.add(makeNewsSearchDocument(aitem));
			}
		}
		deleteIndex(terms, indexDir);
		makeIndex(docs, indexDir);
		}catch(Exception e){
			throw new SuncmsException("error in LuceneIndexImpl.addNewArticleClass()");
		}
	}

	public  List<SearchResult> search(String searchText, String indexDir) throws SuncmsException {
		// List<Document> docs = new ArrayList<Document>();
		
		List<SearchResult> sr = new ArrayList<SearchResult>();
		if (!indexExist(indexDir)) {
			return sr;
		}
		Searcher searcher = null;
		try {
			StandardAnalyzer analyzer = new StandardAnalyzer();
			QueryParser parser = new QueryParser("title", analyzer);
			QueryParser parser1 = new QueryParser("content", analyzer);
			// 处理检索条件
			Query titleQuery = parser.parse(searchText);
			Query contextQuery = parser1.parse(searchText);
			BooleanQuery query = new BooleanQuery();
			query.add(titleQuery, null);
			query.add(contextQuery, null);
			// 分页检索
			searcher = new IndexSearcher(indexDir);
			Hits hits = searcher.search(query);
			// Scorer scorer = new QueryScorer(query);

			for (int i = 1; i <= hits.length(); i++) {
				Document docTemp = hits.doc(i - 1);
				SearchResult sritem = new SearchResult();
				sritem.setArt_title(docTemp.get("title"));
				sritem.setArt_file(docTemp.get("file"));
				sritem.setArt_time(docTemp.get("addtime"));

				String row_id = docTemp.get("nid");
				//ArticleSqlMapDao asd = new ArticleSqlMapDao();
				Article art = articleDao.getArtById(row_id);
				sritem.setArt_class_name(art.getArtclass_name());
				String value= getTxtWithoutHTMLElement(art.getArticle_content());
				// 对要高亮显示的字段格式化,这里只是加红色显示和加粗
				SimpleHTMLFormatter sHtmlF = new SimpleHTMLFormatter(
						"<b><font color='red'>", "</font></b>");
				Highlighter highlighter = new Highlighter(sHtmlF,
						new QueryScorer(query));
				highlighter.setTextFragmenter(new SimpleFragmenter(100));

				if (value != null) {
					TokenStream tokenStream = analyzer.tokenStream("content",
							new StringReader(value));
					String str = highlighter
							.getBestFragment(tokenStream, value);
					sritem.setArt_content(str);
					
				}else{
					sritem.setArt_content("");
				}
				sr.add(sritem);
			}
		}catch(Exception e){
			throw new SuncmsException("error in LuceneIndexImpl.addNewArticleClass()");
		}finally {
			try {
				if (null != searcher) {
					searcher.close();
				}
			}catch(Exception e){
				throw new SuncmsException("error in LuceneIndexImpl.addNewArticleClass()");
			}
		}
		return sr;
	}

	 public String getTxtWithoutHTMLElement (String element) throws SuncmsException
	    {
	        
	        if(null==element||"".equals(element.trim()))
	        {
	            return element;
	        }
try{
	        Pattern pattern=Pattern.compile("<[^<|^>]*>");
	        Matcher matcher=pattern.matcher(element);
	        StringBuffer txt=new StringBuffer();
	        while(matcher.find())
	        {
	            String group=matcher.group();
	            if(group.matches("<[\\s]*>"))
	            {
	                matcher.appendReplacement(txt,group);    
	            }
	            else
	            {
	                matcher.appendReplacement(txt,"");
	            }
	        }
	        matcher.appendTail(txt);
	       
	        return txt.toString();
}catch(Exception e){
	throw new SuncmsException("error in LuceneIndexImpl.addNewArticleClass()");
}
	    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -