Lucene学习笔记(1)-索引创建和简单的查询

本文是基于6.2.1版本,本人也是刚刚接触有什么不对的地方还请大家多多指导。

1、创建索引

static Directory directory;
static {
try {
// 保存在内存中
// directory = new RAMDirectory();
// 保存在硬盘上
directory = FSDirectory.open(Paths.get("e:/lucene/index"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private String[] ids = { "1", "2", "3", "4" };
private String[] titles = { "hello world", "this is a notice", "i like you", "i like football" };
private String[] contents = {
"Apache LuceneTM is a high-performance, full-featured text search engine library written entirely in Java. It is a technology suitable for nearly any application that requires full-text search, especially cross-platform.",
"Available as Open Source software under the Apache License which lets you use Lucene in both commercial and Open Source programs",
"The Apache Software Foundation provides support for the Apache community of open-source software projects. ",
"Software Foundation provides" };
private int[] attachs = { 1, 2, 3, 4 };


/**
* 创建索引

* @throws Exception
*/
public void index() throws Exception {


IndexWriterConfig indexWriterConfig = new IndexWriterConfig(new StandardAnalyzer());
IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
indexWriter.deleteAll();
Document doc;
for (int i = 0; i < attachs.length; i++) {
doc = new Document();
doc.add(new StringField("id", ids[i], Store.YES));
doc.add(new TextField("title", titles[i], Store.YES));
doc.add(new TextField("content", contents[i], Store.NO));
// 创建整数索引
IntPoint intPoint = new IntPoint("attach", attachs[i]);
doc.add(intPoint);
// 存储整数索引的值
StoredField storedField = new StoredField("attachs", attachs[i]);
doc.add(storedField);
indexWriter.addDocument(doc);
}
indexWriter.close();


}

2、检索数据

/**
* 查询索引

* @throws Exception
*/
public void reader() throws Exception {
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
// 字符串查询(谨记字符串查询时字母都要小写)
// Query query = new TermQuery(new Term("title", "like"));


// 表达式匹配
// PhraseQuery.Builder builder = new Builder();
// builder.add(new Term("content", "apache"));
// builder.add(new Term("content", "software"));
// PhraseQuery query = builder.build();


// 范围查询(true:包含,false:不包含)
// TermRangeQuery query = new TermRangeQuery("title", new
// BytesRef("her"), new BytesRef("is"), true, false);
// 整数精确查询
// Query query = IntPoint.newExactQuery("attach", 3);
// 整数范围查询(包含2和4)
Query query = IntPoint.newRangeQuery("attach", 2, 4);
TopDocs topDocs = indexSearcher.search(query, 10);
ScoreDoc[] docs = topDocs.scoreDocs;
for (ScoreDoc scoreDoc : docs) {
Document document = indexSearcher.doc(scoreDoc.doc);
System.out.println("id:" + document.get("id"));
System.out.println("title:" + document.get("title"));
System.out.println("content:" + document.get("content"));
System.out.println("attach:" + document.get("attachs"));
System.out.println("------------------------------");
}
}

public static void main(String[] args) throws Exception {
IndexDemo demo = new IndexDemo();
demo.index();
demo.reader();
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值