花了整整一天時(shí)間,將Lucene5中有關(guān)索引的常見(jiàn)操作進(jìn)行了簡(jiǎn)單封裝,廢話不多說(shuō),上代碼:
- package com.yida.framework.lucene5.util;
-
- import java.io.IOException;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.locks.Lock;
- import java.util.concurrent.locks.ReentrantLock;
-
- 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.search.IndexSearcher;
- import org.apache.lucene.store.Directory;
- import org.apache.lucene.store.LockObtainFailedException;
- /**
- * Lucene索引讀寫(xiě)器/查詢器單例獲取工具類
- * @author Lanxiaowei
- *
- */
- public class LuceneManager {
- private volatile static LuceneManager singleton;
-
- private volatile static IndexWriter writer;
-
- private volatile static IndexReader reader;
-
- private volatile static IndexSearcher searcher;
-
- private final Lock writerLock = new ReentrantLock();
-
- //private final Lock readerLock = new ReentrantLock();
-
- //private final Lock searcherLock = new ReentrantLock();
-
- private LuceneManager() {}
-
- public static LuceneManager getInstance() {
- if (null == singleton) {
- synchronized (LuceneManager.class) {
- if (null == singleton) {
- singleton = new LuceneManager();
- }
- }
- }
- return singleton;
- }
-
- /**
- * 獲取IndexWriter單例對(duì)象
- * @param dir
- * @param config
- * @return
- */
- public IndexWriter getIndexWriter(Directory dir, IndexWriterConfig config) {
- if(null == dir) {
- throw new IllegalArgumentException("Directory can not be null.");
- }
- if(null == config) {
- throw new IllegalArgumentException("IndexWriterConfig can not be null.");
- }
- try {
- writerLock.lock();
- if(null == writer){
- //如果索引目錄被鎖,則直接拋異常
- if(IndexWriter.isLocked(dir)) {
- throw new LockObtainFailedException("Directory of index had been locked.");
- }
- writer = new IndexWriter(dir, config);
- }
- } catch (LockObtainFailedException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- writerLock.unlock();
- }
- return writer;
- }
-
- /**
- * 獲取IndexReader對(duì)象
- * @param dir
- * @param enableNRTReader 是否開(kāi)啟NRTReader
- * @return
- */
- public IndexReader getIndexReader(Directory dir,boolean enableNRTReader) {
- if(null == dir) {
- throw new IllegalArgumentException("Directory can not be null.");
- }
- try {
- if(null == reader){
- reader = DirectoryReader.open(dir);
- } else {
- if(enableNRTReader && reader instanceof DirectoryReader) {
- //開(kāi)啟近實(shí)時(shí)Reader,能立即看到動(dòng)態(tài)添加/刪除的索引變化
- reader = DirectoryReader.openIfChanged((DirectoryReader)reader);
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return reader;
- }
-
- /**
- * 獲取IndexReader對(duì)象(默認(rèn)不啟用NETReader)
- * @param dir
- * @return
- */
- public IndexReader getIndexReader(Directory dir) {
- return getIndexReader(dir, false);
- }
-
- /**
- * 獲取IndexSearcher對(duì)象
- * @param reader IndexReader對(duì)象實(shí)例
- * @param executor 如果你需要開(kāi)啟多線程查詢,請(qǐng)?zhí)峁〦xecutorService對(duì)象參數(shù)
- * @return
- */
- public IndexSearcher getIndexSearcher(IndexReader reader,ExecutorService executor) {
- if(null == reader) {
- throw new IllegalArgumentException("The indexReader can not be null.");
- }
- if(null == searcher){
- searcher = new IndexSearcher(reader);
- }
- return searcher;
- }
-
- /**
- * 獲取IndexSearcher對(duì)象(不支持多線程查詢)
- * @param reader IndexReader對(duì)象實(shí)例
- * @return
- */
- public IndexSearcher getIndexSearcher(IndexReader reader) {
- return getIndexSearcher(reader, null);
- }
- }
- package com.yida.framework.lucene5.util;
-
- import java.io.IOException;
- import java.nio.file.Paths;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- import java.util.Set;
- import java.util.concurrent.ExecutorService;
-
- import org.ansj.lucene5.AnsjAnalyzer;
- import org.apache.lucene.analysis.Analyzer;
- import org.apache.lucene.document.Document;
- import org.apache.lucene.document.Field;
- import org.apache.lucene.document.TextField;
- import org.apache.lucene.index.IndexReader;
- import org.apache.lucene.index.IndexWriter;
- import org.apache.lucene.index.IndexWriterConfig;
- import org.apache.lucene.index.IndexableField;
- import org.apache.lucene.index.Term;
- 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.search.highlight.Formatter;
- import org.apache.lucene.search.highlight.Fragmenter;
- import org.apache.lucene.search.highlight.Highlighter;
- import org.apache.lucene.search.highlight.InvalidTokenOffsetsException;
- 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.store.Directory;
- import org.apache.lucene.store.FSDirectory;
-
- /**
- * Lucene工具類(基于Lucene5.0封裝)
- * @author Lanxiaowei
- *
- */
- public class LuceneUtils {
- private static final LuceneManager luceneManager = LuceneManager.getInstance();
- private static Analyzer analyzer = new AnsjAnalyzer();
-
- /**
- * 打開(kāi)索引目錄
- *
- * @param luceneDir
- * @return
- * @throws IOException
- */
- public static FSDirectory openFSDirectory(String luceneDir) {
- FSDirectory directory = null;
- try {
- directory = FSDirectory.open(Paths.get(luceneDir));
- /**
- * 注意:isLocked方法內(nèi)部會(huì)試圖去獲取Lock,如果獲取到Lock,會(huì)關(guān)閉它,否則return false表示索引目錄沒(méi)有被鎖,
- * 這也就是為什么unlock方法被從IndexWriter類中移除的原因
- */
- IndexWriter.isLocked(directory);
- } catch (IOException e) {
- e.printStackTrace();
- }
- return directory;
- }
-
- /**
- * 關(guān)閉索引目錄并銷毀
- * @param directory
- * @throws IOException
- */
- public static void closeDirectory(Directory directory) throws IOException {
- if (null != directory) {
- directory.close();
- directory = null;
- }
- }
-
- /**
- * 獲取IndexWriter
- * @param dir
- * @param config
- * @return
- */
- public static IndexWriter getIndexWrtier(Directory dir, IndexWriterConfig config) {
- return luceneManager.getIndexWriter(dir, config);
- }
-
- /**
- * 獲取IndexWriter
- * @param dir
- * @param config
- * @return
- */
- public static IndexWriter getIndexWrtier(String directoryPath, IndexWriterConfig config) {
- FSDirectory directory = openFSDirectory(directoryPath);
- return luceneManager.getIndexWriter(directory, config);
- }
-
- /**
- * 獲取IndexReader
- * @param dir
- * @param enableNRTReader 是否開(kāi)啟NRTReader
- * @return
- */
- public static IndexReader getIndexReader(Directory dir,boolean enableNRTReader) {
- return luceneManager.getIndexReader(dir, enableNRTReader);
- }
-
- /**
- * 獲取IndexReader(默認(rèn)不啟用NRTReader)
- * @param dir
- * @return
- */
- public static IndexReader getIndexReader(Directory dir) {
- return luceneManager.getIndexReader(dir);
- }
-
- /**
- * 獲取IndexSearcher
- * @param reader IndexReader對(duì)象
- * @param executor 如果你需要開(kāi)啟多線程查詢,請(qǐng)?zhí)峁〦xecutorService對(duì)象參數(shù)
- * @return
- */
- public static IndexSearcher getIndexSearcher(IndexReader reader,ExecutorService executor) {
- return luceneManager.getIndexSearcher(reader, executor);
- }
-
- /**
- * 獲取IndexSearcher(不支持多線程查詢)
- * @param reader IndexReader對(duì)象
- * @return
- */
- public static IndexSearcher getIndexSearcher(IndexReader reader) {
- return luceneManager.getIndexSearcher(reader);
- }
-
- /**
- * 創(chuàng)建QueryParser對(duì)象
- * @param field
- * @param analyzer
- * @return
- */
- public static QueryParser createQueryParser(String field, Analyzer analyzer) {
- return new QueryParser(field, analyzer);
- }
-
- /**
- * 關(guān)閉IndexReader
- * @param reader
- */
- public static void closeIndexReader(IndexReader reader) {
- if (null != reader) {
- try {
- reader.close();
- reader = null;
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- /**
- * 關(guān)閉IndexWriter
- * @param writer
- */
- public static void closeIndexWriter(IndexWriter writer) {
- if(null != writer) {
- try {
- writer.close();
- writer = null;
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- /**
- * 關(guān)閉IndexReader和IndexWriter
- * @param reader
- * @param writer
- */
- public static void closeAll(IndexReader reader, IndexWriter writer) {
- closeIndexReader(reader);
- closeIndexWriter(writer);
- }
-
- /**
- * 刪除索引[注意:請(qǐng)自己關(guān)閉IndexWriter對(duì)象]
- * @param writer
- * @param field
- * @param value
- */
- public static void deleteIndex(IndexWriter writer, String field, String value) {
- try {
- writer.deleteDocuments(new Term[] {new Term(field,value)});
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- /**
- * 刪除索引[注意:請(qǐng)自己關(guān)閉IndexWriter對(duì)象]
- * @param writer
- * @param query
- */
- public static void deleteIndex(IndexWriter writer, Query query) {
- try {
- writer.deleteDocuments(query);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- /**
- * 批量刪除索引[注意:請(qǐng)自己關(guān)閉IndexWriter對(duì)象]
- * @param writer
- * @param terms
- */
- public static void deleteIndexs(IndexWriter writer,Term[] terms) {
- try {
- writer.deleteDocuments(terms);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- /**
- * 批量刪除索引[注意:請(qǐng)自己關(guān)閉IndexWriter對(duì)象]
- * @param writer
- * @param querys
- */
- public static void deleteIndexs(IndexWriter writer,Query[] querys) {
- try {
- writer.deleteDocuments(querys);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- /**
- * 刪除所有索引文檔
- * @param writer
- */
- public static void deleteAllIndex(IndexWriter writer) {
- try {
- writer.deleteAll();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- /**
- * 更新索引文檔
- * @param writer
- * @param term
- * @param document
- */
- public static void updateIndex(IndexWriter writer,Term term,Document document) {
- try {
- writer.updateDocument(term, document);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- /**
- * 更新索引文檔
- * @param writer
- * @param term
- * @param document
- */
- public static void updateIndex(IndexWriter writer,String field,String value,Document document) {
- updateIndex(writer, new Term(field, value), document);
- }
-
- /**
- * 添加索引文檔
- * @param writer
- * @param doc
- */
- public static void addIndex(IndexWriter writer, Document document) {
- updateIndex(writer, null, document);
- }
-
- /**
- * 索引文檔查詢
- * @param searcher
- * @param query
- * @return
- */
- public static List<Document> query(IndexSearcher searcher,Query query) {
- TopDocs topDocs = null;
- try {
- topDocs = searcher.search(query, Integer.MAX_VALUE);
- } catch (IOException e) {
- e.printStackTrace();
- }
- ScoreDoc[] scores = topDocs.scoreDocs;
- int length = scores.length;
- if (length <= 0) {
- return Collections.emptyList();
- }
- List<Document> docList = new ArrayList<Document>();
- try {
- for (int i = 0; i < length; i++) {
- Document doc = searcher.doc(scores[i].doc);
- docList.add(doc);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return docList;
- }
-
- /**
- * 返回索引文檔的總數(shù)[注意:請(qǐng)自己手動(dòng)關(guān)閉IndexReader]
- * @param reader
- * @return
- */
- public static int getIndexTotalCount(IndexReader reader) {
- return reader.numDocs();
- }
-
- /**
- * 返回索引文檔中最大文檔ID[注意:請(qǐng)自己手動(dòng)關(guān)閉IndexReader]
- * @param reader
- * @return
- */
- public static int getMaxDocId(IndexReader reader) {
- return reader.maxDoc();
- }
-
- /**
- * 返回已經(jīng)刪除尚未提交的文檔總數(shù)[注意:請(qǐng)自己手動(dòng)關(guān)閉IndexReader]
- * @param reader
- * @return
- */
- public static int getDeletedDocNum(IndexReader reader) {
- return getMaxDocId(reader) - getIndexTotalCount(reader);
- }
-
- /**
- * 根據(jù)docId查詢索引文檔
- * @param reader IndexReader對(duì)象
- * @param docID documentId
- * @param fieldsToLoad 需要返回的field
- * @return
- */
- public static Document findDocumentByDocId(IndexReader reader,int docID, Set<String> fieldsToLoad) {
- try {
- return reader.document(docID, fieldsToLoad);
- } catch (IOException e) {
- return null;
- }
- }
-
- /**
- * 根據(jù)docId查詢索引文檔
- * @param reader IndexReader對(duì)象
- * @param docID documentId
- * @return
- */
- public static Document findDocumentByDocId(IndexReader reader,int docID) {
- return findDocumentByDocId(reader, docID, null);
- }
-
- /**
- * @Title: createHighlighter
- * @Description: 創(chuàng)建高亮器
- * @param query 索引查詢對(duì)象
- * @param prefix 高亮前綴字符串
- * @param stuffix 高亮后綴字符串
- * @param fragmenterLength 摘要最大長(zhǎng)度
- * @return
- */
- public static Highlighter createHighlighter(Query query, String prefix, String stuffix, int fragmenterLength) {
- Formatter formatter = new SimpleHTMLFormatter((prefix == null || prefix.trim().length() == 0) ?
- "<font color=\"red\">" : prefix, (stuffix == null || stuffix.trim().length() == 0)?"</font>" : stuffix);
- Scorer fragmentScorer = new QueryScorer(query);
- Highlighter highlighter = new Highlighter(formatter, fragmentScorer);
- Fragmenter fragmenter = new SimpleFragmenter(fragmenterLength <= 0 ? 50 : fragmenterLength);
- highlighter.setTextFragmenter(fragmenter);
- return highlighter;
- }
-
- /**
- * @Title: highlight
- * @Description: 生成高亮文本
- * @param document 索引文檔對(duì)象
- * @param highlighter 高亮器
- * @param analyzer 索引分詞器
- * @param field 高亮字段
- * @return
- * @throws IOException
- * @throws InvalidTokenOffsetsException
- */
- public static String highlight(Document document,Highlighter highlighter,Analyzer analyzer,String field) throws IOException {
- List<IndexableField> list = document.getFields();
- for (IndexableField fieldable : list) {
- String fieldValue = fieldable.stringValue();
- if(fieldable.name().equals(field)) {
- try {
- fieldValue = highlighter.getBestFragment(analyzer, field, fieldValue);
- } catch (InvalidTokenOffsetsException e) {
- fieldValue = fieldable.stringValue();
- }
- return (fieldValue == null || fieldValue.trim().length() == 0)? fieldable.stringValue() : fieldValue;
- }
- }
- return null;
- }
-
- /**
- * @Title: searchTotalRecord
- * @Description: 獲取符合條件的總記錄數(shù)
- * @param query
- * @return
- * @throws IOException
- */
- public static int searchTotalRecord(IndexSearcher search,Query query) {
- ScoreDoc[] docs = null;
- try {
- TopDocs topDocs = search.search(query, Integer.MAX_VALUE);
- if(topDocs == null || topDocs.scoreDocs == null || topDocs.scoreDocs.length == 0) {
- return 0;
- }
- docs = topDocs.scoreDocs;
- } catch (IOException e) {
- e.printStackTrace();
- }
- return docs.length;
- }
-
- /**
- * @Title: pageQuery
- * @Description: Lucene分頁(yè)查詢
- * @param searcher
- * @param query
- * @param page
- * @throws IOException
- */
- public static void pageQuery(IndexSearcher searcher,Directory directory,Query query,Page<Document> page) {
- int totalRecord = searchTotalRecord(searcher,query);
- //設(shè)置總記錄數(shù)
- page.setTotalRecord(totalRecord);
- TopDocs topDocs = null;
- try {
- topDocs = searcher.searchAfter(page.getAfterDoc(),query, page.getPageSize());
- } catch (IOException e) {
- e.printStackTrace();
- }
- List<Document> docList = new ArrayList<Document>();
- ScoreDoc[] docs = topDocs.scoreDocs;
- int index = 0;
- for (ScoreDoc scoreDoc : docs) {
- int docID = scoreDoc.doc;
- Document document = null;
- try {
- document = searcher.doc(docID);
- } catch (IOException e) {
- e.printStackTrace();
- }
- if(index == docs.length - 1) {
- page.setAfterDoc(scoreDoc);
- page.setAfterDocId(docID);
- }
- docList.add(document);
- index++;
- }
- page.setItems(docList);
- closeIndexReader(searcher.getIndexReader());
- }
-
- /**
- * @Title: pageQuery
- * @Description: 分頁(yè)查詢[如果設(shè)置了高亮,則會(huì)更新索引文檔]
- * @param searcher
- * @param directory
- * @param query
- * @param page
- * @param highlighterParam
- * @param writerConfig
- * @throws IOException
- */
- public static void pageQuery(IndexSearcher searcher,Directory directory,Query query,Page<Document> page,HighlighterParam highlighterParam,IndexWriterConfig writerConfig) throws IOException {
- IndexWriter writer = null;
- //若未設(shè)置高亮
- if(null == highlighterParam || !highlighterParam.isHighlight()) {
- pageQuery(searcher,directory,query, page);
- } else {
- int totalRecord = searchTotalRecord(searcher,query);
- System.out.println("totalRecord:" + totalRecord);
- //設(shè)置總記錄數(shù)
- page.setTotalRecord(totalRecord);
- TopDocs topDocs = searcher.searchAfter(page.getAfterDoc(),query, page.getPageSize());
- List<Document> docList = new ArrayList<Document>();
- ScoreDoc[] docs = topDocs.scoreDocs;
- int index = 0;
- writer = getIndexWrtier(directory, writerConfig);
- for (ScoreDoc scoreDoc : docs) {
- int docID = scoreDoc.doc;
- Document document = searcher.doc(docID);
- String content = document.get(highlighterParam.getFieldName());
- if(null != content && content.trim().length() > 0) {
- //創(chuàng)建高亮器
- Highlighter highlighter = LuceneUtils.createHighlighter(query,
- highlighterParam.getPrefix(), highlighterParam.getStuffix(),
- highlighterParam.getFragmenterLength());
- String text = highlight(document, highlighter, analyzer, highlighterParam.getFieldName());
- //若高亮后跟原始文本不相同,表示高亮成功
- if(!text.equals(content)) {
- Document tempdocument = new Document();
- List<IndexableField> indexableFieldList = document.getFields();
- if(null != indexableFieldList && indexableFieldList.size() > 0) {
- for(IndexableField field : indexableFieldList) {
- if(field.name().equals(highlighterParam.getFieldName())) {
- tempdocument.add(new TextField(field.name(), text, Field.Store.YES));
- } else {
- tempdocument.add(field);
- }
- }
- }
- updateIndex(writer, new Term(highlighterParam.getFieldName(),content), tempdocument);
- document = tempdocument;
- }
- }
- if(index == docs.length - 1) {
- page.setAfterDoc(scoreDoc);
- page.setAfterDocId(docID);
- }
- docList.add(document);
- index++;
- }
- page.setItems(docList);
- }
- closeIndexReader(searcher.getIndexReader());
- closeIndexWriter(writer);
- }
- }
- package com.yida.framework.lucene5.util;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.List;
- import org.apache.lucene.document.Document;
- import org.apache.lucene.search.ScoreDoc;
- public class Page<T> {
- /**當(dāng)前第幾頁(yè)(從1開(kāi)始計(jì)算)*/
- private int currentPage;
- /**每頁(yè)顯示幾條*/
- private int pageSize;
- /**總記錄數(shù)*/
- private int totalRecord;
- /**總頁(yè)數(shù)*/
- private int totalPage;
- /**分頁(yè)數(shù)據(jù)集合[用泛型T來(lái)限定集合元素類型]*/
- private Collection<T> items;
- /**當(dāng)前顯示起始索引(從零開(kāi)始計(jì)算)*/
- private int startIndex;
- /**當(dāng)前顯示結(jié)束索引(從零開(kāi)始計(jì)算)*/
- private int endIndex;
- /**一組最多顯示幾個(gè)頁(yè)碼[比如Google一組最多顯示10個(gè)頁(yè)碼]*/
- private int groupSize;
- /**左邊偏移量*/
- private int leftOffset = 5;
- /**右邊偏移量*/
- private int rightOffset = 4;
- /**當(dāng)前頁(yè)碼范圍*/
- private String[] pageRange;
- /**分頁(yè)數(shù)據(jù)*/
- private List<Document> docList;
- /**上一頁(yè)最后一個(gè)ScoreDoc對(duì)象*/
- private ScoreDoc afterDoc;
- /**上一頁(yè)最后一個(gè)ScoreDoc對(duì)象的Document對(duì)象ID*/
- private int afterDocId;
- public void setRangeIndex() {
- int groupSize = getGroupSize();
- int totalPage = getTotalPage();
- if(totalPage < 2) {
- startIndex = 0;
- endIndex = totalPage - startIndex;
- } else {
- int currentPage = getCurrentPage();
- if(groupSize >= totalPage) {
- startIndex = 0;
- endIndex = totalPage - startIndex - 1;
- } else {
- int leftOffset = getLeftOffset();
- int middleOffset = getMiddleOffset();
- if(-1 == middleOffset) {
- startIndex = 0;
- endIndex = groupSize - 1;
- } else if(currentPage <= leftOffset) {
- startIndex = 0;
- endIndex = groupSize - 1;
- } else {
- startIndex = currentPage - leftOffset - 1;
- if(currentPage + rightOffset > totalPage) {
- endIndex = totalPage - 1;
- } else {
- endIndex = currentPage + rightOffset - 1;
- }
- }
- }
- }
- }
- public int getCurrentPage() {
- if(currentPage <= 0) {
- currentPage = 1;
- } else {
- int totalPage = getTotalPage();
- if(totalPage > 0 && currentPage > getTotalPage()) {
- currentPage = totalPage;
- }
- }
- return currentPage;
- }
- public void setCurrentPage(int currentPage) {
- this.currentPage = currentPage;
- }
- public int getPageSize() {
- if(pageSize <= 0) {
- pageSize = 10;
- }
- return pageSize;
- }
- public void setPageSize(int pageSize) {
- this.pageSize = pageSize;
- }
- public int getTotalRecord() {
- return totalRecord;
- }
- public void setTotalRecord(int totalRecord) {
- this.totalRecord = totalRecord;
- }
- public int getTotalPage() {
- int totalRecord = getTotalRecord();
- if(totalRecord == 0) {
- totalPage = 0;
- } else {
- int pageSize = getPageSize();
- totalPage = totalRecord % pageSize == 0 ? totalRecord / pageSize : (totalRecord / pageSize) + 1;
- }
- return totalPage;
- }
- public void setTotalPage(int totalPage) {
- this.totalPage = totalPage;
- }
- public int getStartIndex() {
- return startIndex;
- }
- public void setStartIndex(int startIndex) {
- this.startIndex = startIndex;
- }
- public int getEndIndex() {
- return endIndex;
- }
- public void setEndIndex(int endIndex) {
- this.endIndex = endIndex;
- }
- public int getGroupSize() {
- if(groupSize <= 0) {
- groupSize = 10;
- }
- return groupSize;
- }
- public void setGroupSize(int groupSize) {
- this.groupSize = groupSize;
- }
- public int getLeftOffset() {
- leftOffset = getGroupSize() / 2;
- return leftOffset;
- }
- public void setLeftOffset(int leftOffset) {
- this.leftOffset = leftOffset;
- }
- public int getRightOffset() {
- int groupSize = getGroupSize();
- if(groupSize % 2 == 0) {
- rightOffset = (groupSize / 2) - 1;
- } else {
- rightOffset = groupSize / 2;
- }
- return rightOffset;
- }
- public void setRightOffset(int rightOffset) {
- this.rightOffset = rightOffset;
- }
- /**中心位置索引[從1開(kāi)始計(jì)算]*/
- public int getMiddleOffset() {
- int groupSize = getGroupSize();
- int totalPage = getTotalPage();
- if(groupSize >= totalPage) {
- return -1;
- }
- return getLeftOffset() + 1;
- }
- public String[] getPageRange() {
- setRangeIndex();
- int size = endIndex - startIndex + 1;
- if(size <= 0) {
- return new String[0];
- }
- if(totalPage == 1) {
- return new String[] {"1"};
- }
- pageRange = new String[size];
- for(int i=0; i < size; i++) {
- pageRange[i] = (startIndex + i + 1) + "";
- }
- return pageRange;
- }
- public void setPageRange(String[] pageRange) {
- this.pageRange = pageRange;
- }
- public Collection<T> getItems() {
- return items;
- }
- public void setItems(Collection<T> items) {
- this.items = items;
- }
- public List<Document> getDocList() {
- return docList;
- }
- public void setDocList(List<Document> docList) {
- this.docList = docList;
- }
- public ScoreDoc getAfterDoc() {
- setAfterDocId(afterDocId);
- return afterDoc;
- }
- public void setAfterDoc(ScoreDoc afterDoc) {
- this.afterDoc = afterDoc;
- }
- public int getAfterDocId() {
- return afterDocId;
- }
- public void setAfterDocId(int afterDocId) {
- this.afterDocId = afterDocId;
- if(null == afterDoc) {
- this.afterDoc = new ScoreDoc(afterDocId, 1.0f);
- }
- }
- public Page() {}
- public Page(int currentPage, int pageSize) {
- this.currentPage = currentPage;
- this.pageSize = pageSize;
- }
- public Page(int currentPage, int pageSize, Collection<T> items) {
- this.currentPage = currentPage;
- this.pageSize = pageSize;
- this.items = items;
- }
- public Page(int currentPage, int pageSize, Collection<T> items, int groupSize) {
- this.currentPage = currentPage;
- this.pageSize = pageSize;
- this.items = items;
- this.groupSize = groupSize;
- }
- public Page(int currentPage, int pageSize, int groupSize, int afterDocId) {
- this.currentPage = currentPage;
- this.pageSize = pageSize;
- this.groupSize = groupSize;
- this.afterDocId = afterDocId;
- }
- public static void main(String[] args) {
- Collection<Integer> items = new ArrayList<Integer>();
- int totalRecord = 201;
- for(int i=0; i < totalRecord; i++) {
- items.add(new Integer(i));
- }
- Page<Integer> page = new Page<Integer>(1,10,items,10);
- page.setTotalRecord(totalRecord);
- int totalPage = page.getTotalPage();
- for(int i=0; i < totalPage; i++) {
- page.setCurrentPage(i+1);
- String[] pageRange = page.getPageRange();
- System.out.println("當(dāng)前第" + page.currentPage + "頁(yè)");
- for(int j=0; j < pageRange.length; j++) {
- System.out.print(pageRange[j] + " ");
- }
- System.out.println("\n");
- }
- }
- }
- package com.yida.framework.lucene5.util;
- /**
- * @ClassName: HighlighterParam
- * @Description: 高亮器參數(shù)對(duì)象
- * @author Lanxiaowei
- * @date 2014-3-30 下午12:22:08
- */
- public class HighlighterParam {
- /**是否需要設(shè)置高亮*/
- private boolean highlight;
- /**需要設(shè)置高亮的屬性名*/
- private String fieldName;
- /**高亮前綴*/
- private String prefix;
- /**高亮后綴*/
- private String stuffix;
- /**顯示摘要最大長(zhǎng)度*/
- private int fragmenterLength;
- public boolean isHighlight() {
- return highlight;
- }
- public void setHighlight(boolean highlight) {
- this.highlight = highlight;
- }
- public String getFieldName() {
- return fieldName;
- }
- public void setFieldName(String fieldName) {
- this.fieldName = fieldName;
- }
- public String getPrefix() {
- return prefix;
- }
- public void setPrefix(String prefix) {
- this.prefix = prefix;
- }
- public String getStuffix() {
- return stuffix;
- }
- public void setStuffix(String stuffix) {
- this.stuffix = stuffix;
- }
- public int getFragmenterLength() {
- return fragmenterLength;
- }
- public void setFragmenterLength(int fragmenterLength) {
- this.fragmenterLength = fragmenterLength;
- }
- public HighlighterParam(boolean highlight, String fieldName, String prefix, String stuffix, int fragmenterLength) {
- this.highlight = highlight;
- this.fieldName = fieldName;
- this.prefix = prefix;
- this.stuffix = stuffix;
- this.fragmenterLength = fragmenterLength;
- }
-
- public HighlighterParam(boolean highlight, String fieldName, int fragmenterLength) {
- this.highlight = highlight;
- this.fieldName = fieldName;
- this.fragmenterLength = fragmenterLength;
- }
-
- public HighlighterParam(boolean highlight, String fieldName, String prefix, String stuffix) {
- this.highlight = highlight;
- this.fieldName = fieldName;
- this.prefix = prefix;
- this.stuffix = stuffix;
- }
- public HighlighterParam() {
- }
- }
工具類對(duì)IndexWriter,IndexReader,IndexSearcher,Analyzer,QueryParser等Lucene這些常用操作對(duì)象的獲取進(jìn)行了封裝,其中IndexWriter采用了單例模式,確保始終只有一個(gè)對(duì)象實(shí)例,因?yàn)長(zhǎng)ucene限制了索引寫(xiě)操作是阻塞的,即同一時(shí)刻只能有一個(gè)IndexWriter在執(zhí)行寫(xiě)操作,直到indexWriter釋放lock,而索引讀的操作是可以并發(fā)進(jìn)行的。
|