Lucene 搜索试用

传说中强大的Lucene搜索,首先要创建索引:

import java.io.IOException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
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.Term;
import org.apache.lucene.store.LockObtainFailedException;

public class TextFileIndexer {
private IndexWriter indexWriter;

public TextFileIndexer(String directory) {
try {
indexWriter = new IndexWriter(directory, new StandardAnalyzer(),
true);
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) throws Exception {
TextFileIndexer indexer = new TextFileIndexer("/home/com/tmp/index");
indexer.create("/home/sina", "axu你发三段论法的撒肥撒旦", "了脑门大量的萨考虑我的");
indexer.create("/home/blog", "9726", "java");
// indexer.delete("/home/blog");
indexer.commit();
}

public void create(String path, String title, String content) {
Document document = new Document();
Field fieldPath = new Field("path", path, Field.Store.YES,
Field.Index.NO);
Field fieldTitle = new Field("title", title, Field.Store.YES,
Field.Index.TOKENIZED);
Field fieldContent = new Field("content", content, Field.Store.NO,
Field.Index.TOKENIZED);
document.add(fieldPath);
document.add(fieldTitle);
document.add(fieldContent);
try {
indexWriter.addDocument(document);
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

public void commit() {
try {
indexWriter.optimize();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

public void delete(String path) {
Term term = new Term("path", path);
try {
indexWriter.deleteDocuments(term);
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public void update(String path, String title, String content) {
this.delete(path);
this.create(path, title, content);
}
}


然后就可以进行搜索了,搜索创建好的索引:

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.LockObtainFailedException;

public class TextFileSearcher {
private IndexSearcher searcher;

public TextFileSearcher(String directory) {
try {
searcher = new IndexSearcher(directory);
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) throws Exception {
TextFileSearcher indexer = new TextFileSearcher("/home/com/tmp/index");
List<Document> list = indexer.search("9726");
for (Document document : list) {
System.out.println(document.get("path"));
}
}

public List<Document> search(String keyword) {
List<Document> resultset = new ArrayList<Document>();
Analyzer analyzer = new StandardAnalyzer();
try {
QueryParser parser = new QueryParser(null, analyzer);
Query query = parser.parse("title:" + keyword + " OR content:"
+ keyword);
Hits hits = searcher.search(query);
for (int i = 0; i < hits.length(); i++) {
resultset.add(hits.doc(i));
}
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return resultset;
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值