本文是基于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();
}