lucene实例(lucene demo)

public class CreateIndex {//建立索引文件
public static void main(String[] args) throws Exception {

File indexDir = new File("c:\\index"); //索引所在路径
Analyzer luceneAnalyzer = new StandardAnalyzer();
IndexWriter indexWriter = new IndexWriter(indexDir, luceneAnalyzer, true);
long startTime = new Date().getTime();
CreateIndexDAO creareindex=new CreateIndexDAO();
List indexlist=new ArrayList();//数据源
indexlist=creareindex.selectData();//
for (int i = 0; i < indexlist.size(); i++) {
Data data = (Data) indexlist.get(i);
Document doc = new Document();
doc.add(new Field("uid", data.getUid(),
Field.Store.YES, Field.Index.NO));
doc.add(new Field("keyword",data.getKeyword(),
Field.Store.YES,Field.Index.TOKENIZED,Field.TermVector.WITH_POSITIONS_OFFSETS));
doc.add(new Field("txtinfo", data.getTextinfo(),
Field.Store.YES, Field.Index.UN_TOKENIZED));
indexWriter.addDocument(doc);
}
indexWriter.optimize();
indexWriter.close();

long endTime = new Date().getTime(); {
System.out.println("用时"
+ (endTime - startTime)
+ "秒"
);
}
}
//查询方法
public class CreateSearch {
public static void main(String[] args) throws IOException, ParseException {
Hits hits = null;
String queryString = "十";
Query query = null;
IndexSearcher searcher = new IndexSearcher("c:\\index");
Analyzer analyzer = new StandardAnalyzer();
try {
QueryParser qp = new QueryParser("keyword", analyzer);
query = qp.parse(queryString);

} catch (ParseException e) {
}
if (searcher != null) {
hits = searcher.search(query);
for (int i = 0; i < hits.length(); i++) {
Document doc = hits.doc(i);
String uid = doc.get("uid");
String keyword = doc.get("keyword");
System.out.println("记录:" + keyword + "对应id"+keyword);
}
System.out.println("共查出记录" + hits.length() + "条");

}
}

}
[color=red]有不明白的地方欢迎联系我,共同探讨[/color]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值