我的云之旅–Lucene内容存储进入Hadoop(136)

首先了解一下Lucene的使用:


package com.rx;索引的建立:

import java.io.File;

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.IndexWriter;

import org.apache.lucene.index.IndexWriterConfig;

import org.apache.lucene.store.Directory;

import org.apache.lucene.store.SimpleFSDirectory;

import org.apache.lucene.util.Version;

public class C {

public static void main(String[] args) throws IOException {

IndexWriterConfig i = new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35));

Directory d = new SimpleFSDirectory(new File("E:/index"));

IndexWriter writer = new IndexWriter(d, i);

Document doc = new Document();

doc.add(new Field("title", "lucene introduction", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));

doc.add(new Field("time", "60", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));

writer.addDocument(doc);

writer.commit();

writer.close();

}

}

 
索引的查询:
package com.rx;
 
import java.io.File;
import java.io.IOException;
 
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
 
public class R {
public static void main(String[] args) throws IOException {
Directory d = new SimpleFSDirectory(new File("E:/index"));
IndexReader reader = IndexReader.open(d);
IndexSearcher searcher = new IndexSearcher(reader);
Query query = new TermQuery(new Term("title", "lucene"));
TopDocs hits = searcher.search(query, 10);
System.out.println(hits.totalHits);
for (ScoreDoc scoreDoc : hits.scoreDocs) {
System.out.println(scoreDoc.doc);
Document doc = searcher.doc(scoreDoc.doc);
System.out.println("title /t " + doc.get("title"));
System.out.println(doc.get("time"));
}
searcher.close();
}
}
 
lucene的查看工具:
java -jar lukeall-3.5.0.jar 运行即可。
 
上面的写入运行2次后查询结果:
2
0
title /t lucene introduction
60
1
title /t lucene introduction
60
 
目前发现有人使用修改的lucene的代码和solr以及Hbase提供的分布式搜索,可以支持三千五百万的日搜索服务。

转载于:https://www.cnblogs.com/hehehaha/archive/2012/04/12/6332469.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值