lucene 索引

//索引UCD操作

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;
import org.wltea.analyzer.lucene.IKQueryParser;

public class UCDIndex {

private Directory dir;
private IndexWriter writer;

public UCDIndex() {
}

public UCDIndex(String indexDir) throws IOException {
this.dir = FSDirectory.open(new java.io.File(indexDir));
Analyzer analyzer = new IKAnalyzer();
this.writer = new IndexWriter(dir, new IndexWriterConfig(
Version.LUCENE_32, analyzer));
}

public boolean removeDocument(String docKey) throws IOException{

try{
Query query = IKQueryParser.parse("key", docKey);
writer.deleteDocuments(query);
}catch(IOException e){
return false;
}finally{
try {
if(dir != null && IndexWriter.isLocked(dir)){
IndexWriter.unlock(dir);
}
} catch (IOException e) {}
}

return true;
}

public boolean updateDocument(Index index) throws IOException{

removeDocument(index.getKey());

return addDocument(index);
}

public boolean addDocument(Index index){

Document doc = new Document();
doc.add(new Field("key", index.getKey(), Field.Store.YES,
Field.Index.ANALYZED));

doc.add(new Field("title", index.getTitle(), Field.Store.YES,
Field.Index.ANALYZED));

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
doc.add(new Field("date", sdf.format(new Date()), Field.Store.YES,
Field.Index.ANALYZED));

doc.add(new Field("content", index.getContent(), Field.Store.YES,
Field.Index.ANALYZED));

doc.add(new Field("contentType", index.getContentType(), Field.Store.YES,
Field.Index.ANALYZED));

try{
writer.addDocument(doc);

return true;

}catch(Exception e){
return false;
}finally{
try {
if(dir != null && IndexWriter.isLocked(dir)){
IndexWriter.unlock(dir);
}
} catch (IOException e) {}
}
}

public boolean addDocument(List<Index> indexList){

int count = 0;
if(indexList != null && !indexList.isEmpty()){
for(Index index:indexList){
if(addDocument(index)){
count++;
}else{
break;
}
}
return count == indexList.size();
}

return false;
}

public void close() {
if(writer != null){
try {
writer.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public void commit() {
if(writer != null){
try {
writer.commit();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

//索引查询
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.document.Document;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.wltea.analyzer.lucene.IKQueryParser;
import org.wltea.analyzer.lucene.IKSimilarity;

public class QueryIndex {

private IndexSearcher searcher;

public QueryIndex() {
}

public QueryIndex(String indexDir) throws IOException {
searcher = new IndexSearcher(FSDirectory.open(new java.io.File(indexDir)));
searcher.setSimilarity(new IKSimilarity());
}

public List<Index> search(String keyword) throws IOException {

List<Index> docList = new ArrayList<Index>();

try{
Query query = IKQueryParser.parseMultiField(new String[] {"title","contents"}, keyword);

TopDocs hits = searcher.search(query, 50);

for (int i = 0; i < hits.scoreDocs.length; i++) {
Document hitDoc = searcher.doc(hits.scoreDocs[i].doc);
Index doc = new Index();
String content = hitDoc.get("contents");
if (content != null) {
if (content.length() > 200) {
content = content.substring(0, 200) + "...";
}
content = content.replaceAll(keyword, "<font color=\"red\">"
+ keyword + "</font>");
doc.setContent(content);
}
if (hitDoc.get("title") != null) {
doc.setTitle(hitDoc.get("title").replaceAll(keyword,
"<font color=\"red\">" + keyword + "</font>"));
}
doc.setDate(hitDoc.get("date"));
docList.add(doc);
}
}catch(IOException e){
searcher.close();
}

return docList;
}
}

public class Index {

private String key;
private String contentType;
private String title;
private String content;
private String date;

public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值