Lucene入门
lucene 是一个全文检索引擎的架构,提供了一个简单却强大的应用程式接口,能够做全文索引和搜寻。
创建索引库
俩种储存方式:
第一种:
//将索引存储到内存
Directory ramDirectory = new RAMDirectory();
第二种:
//将索引存储到硬盘
Directory fileDirectory = FSDirectory.open(Paths.get("f:\\testindex"));
创建IndexWriter
//创建分词器(此处采用标注分词器)
Analyzer analyzer = new StandardAnalyzer();
//写入索引库
IndexWriterConfig config = new IndexWriterConfig(analyzer);
//操作内存IndexWriter
IndexWriter iwriter = new IndexWriter(ramDirectory, config);
//操作文件的IndexWriter
IndexWriter fileIndexWriter = new IndexWriter(fileDirectory, config);
创建Document
可以采用Object进行添加数据,不一定只能用数组;
String[] texts = new String[]{
"Mybatis分页插件 - 示例",
"Mybatis 贴吧问答 第一期",
"Mybatis 示例之 复杂(complex)属性(property)",
"Mybatis极其(最)简(好)单(用)的一个分页插件",
"Mybatis 的Log4j日志输出问题 - 以及有关日志的所有问题",
"Mybatis 示例之 foreach (下)",
"Mybatis 示例之 foreach (上)",
"Mybatis 示例之 SelectKey",
"Mybatis 示例之 Association (2)",
"Mybatis 示例之 Association",
"大家好我是来自中日友好医院神经内科的医生"
"我已经是工作四年主要擅长的领域在神经内科很高兴在工作室里为大家做一些疾病知识科普健康教育方面的事情"
"希望大家能够从中了解到相关的疾病知识提升自我的健康管理意识帮助大家能够很好地与法治管理相关的病"
};
for (String text : texts) {
Document doc = new Document();TextField.TYPE_STORED));
doc.add(new TextField("title", text, Field.Store.YES));
doc.add(new StringField("isbn", ""+String.valueOf(Math.random()), Field.Store.YES));
iwriter.addDocument(doc);
}
读取索引并查询
DirectoryReader reader = DirectoryReader.open(fileDirectory);
IndexSearcher isearcher = new IndexSearcher(reader);
//解析一个简单的查询
QueryParser parser = new QueryParser("title", analyzer);
Query query = parser.parse("内科医生 ");
ScoreDoc[] hits = isearcher.search(query, 1000).scoreDocs;
//迭代输出结果
for (int i = 0; i < hits.length; i++) {
Document hitDoc = isearcher.doc(hits[i].doc);
System.out.println(hitDoc.get("title"));
System.out.println(hitDoc.get("isbn"));
}
完整示例
内存索引库优点:读取速度快
文件索引库:长时间存储
下面就是结合的示例
import java.io.IOException;
import java.nio.file.Paths;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;
public class IndexProcessor {
public static void main(String[] args) throws IOException,ParseException {
Analyzer analyzer = new StandardAnalyzer();
//将索引存储到内存中
Directory ramDirectory = new RAMDirectory();
//如下想把索引存储到硬盘上,使用下面的代码代替
Directory fileDirectory = FSDirectory.open(Paths.get("f:\\testindex"));
//写入索引库
IndexWriterConfig config = new IndexWriterConfig(analyzer);
//操作内存IndexWriter
IndexWriter iwriter = new IndexWriter(ramDirectory, config);
//操作文件的IndexWriter
IndexWriter fileIndexWriter = new IndexWriter(fileDirectory, config);
String[] texts = new String[]{
"Mybatis分页插件 - 示例",
"Mybatis 贴吧问答 第一期",
"Mybatis 示例之 复杂(complex)属性(property)",
"Mybatis极其(最)简(好)单(用)的一个分页插件",
"Mybatis 的Log4j日志输出问题 - 以及有关日志的所有问题",
"Mybatis 示例之 foreach (下)",
"Mybatis 示例之 foreach (上)",
"Mybatis 示例之 SelectKey",
"Mybatis 示例之 Association (2)",
"Mybatis 示例之 Association",
"大家好我是来自中日友好医院神经内科的医生",
"我已经是工作四年主要擅长的领域在神经内科很高兴在工作室里为大家做一些疾病知识科普健康教育方面的事情",
"希望大家能够从中了解到相关的疾病知识提升自我的健康管理意识帮助大家能够很好地与法治管理相关的病"
};
for (String text : texts) {
Document doc = new Document();
// doc.add(new Field("fieldname", text, TextField.TYPE_STORED));
doc.add(new TextField("title", text, Field.Store.YES));
doc.add(new StringField("isbn", ""+String.valueOf(Math.random()), Field.Store.YES));
iwriter.addDocument(doc);
}
iwriter.close();
//读取索引并查询
DirectoryReader reader = DirectoryReader.open(fileDirectory);
IndexSearcher isearcher = new IndexSearcher(reader);
//解析一个简单的查询
QueryParser parser = new QueryParser("title", analyzer);
Query query = parser.parse("内科医生 ");
ScoreDoc[] hits = isearcher.search(query, 1000).scoreDocs;
//迭代输出结果
for (int i = 0; i < hits.length; i++) {
Document hitDoc = isearcher.doc(hits[i].doc);
System.out.println(hitDoc.get("title"));
System.out.println(hitDoc.get("isbn"));
}
reader.close();
ramDirectory.close();
}
}
还没有评论,来说两句吧...