Lucene4.3入门
来源:互联网 发布:费翔 刘文正 知乎 编辑:程序博客网 时间:2024/05/22 12:36
java社区比较火的当然是Lucene,想写一个简单的小例子,在网上找了些资料,不过都不是4.3的,自己看了一下。
下载地址:http://lucene.apache.org/core/
constans.java 是常量类
LuceneIndex.java 建立索引类
LuceneSearch.java 搜索类
数据文件:
package com.xin;public class Constants { public final static String INDEX_FILE_PATH = "e:\\lucene\\test"; //索引的文件的存放路径 public final static String INDEX_STORE_PATH = "e:\\lucene\\index"; //索引的存放位置 }
package com.xin;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.InputStreamReader;import java.io.Reader;import java.util.Date;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.IndexWriter;import org.apache.lucene.index.IndexWriterConfig;import org.apache.lucene.index.IndexWriterConfig.OpenMode;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.util.Version;/** * @author chongxin * @since 2013/6/19 * @version Lucene 4.3.1 * */public class LuceneIndex {// 索引器private IndexWriter writer = null;public LuceneIndex() {try {//索引文件的保存位置Directory dir = FSDirectory.open(new File(Constants.INDEX_STORE_PATH));//分析器Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);//配置类IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_40,analyzer);iwc.setOpenMode(OpenMode.CREATE);//创建模式 OpenMode.CREATE_OR_APPEND 添加模式writer = new IndexWriter(dir, iwc);} catch (Exception e) {e.printStackTrace();}}// 将要建立索引的文件构造成一个Document对象,并添加一个域"content"private Document getDocument(File f) throws Exception {Document doc = new Document();FileInputStream is = new FileInputStream(f);Reader reader = new BufferedReader(new InputStreamReader(is));//字符串 StringField LongField TextFieldField pathField = new StringField("path", f.getAbsolutePath(),Field.Store.YES);Field contenField = new TextField("contents", reader);//添加字段doc.add(contenField);doc.add(pathField);return doc;}public void writeToIndex() throws Exception {File folder = new File(Constants.INDEX_FILE_PATH);if (folder.isDirectory()) {String[] files = folder.list();for (int i = 0; i < files.length; i++) {File file = new File(folder, files[i]);Document doc = getDocument(file);System.out.println("正在建立索引 : " + file + "");writer.addDocument(doc);}}}public void close() throws Exception {writer.close();}public static void main(String[] args) throws Exception {// 声明一个对象LuceneIndex indexer = new LuceneIndex();// 建立索引Date start = new Date();indexer.writeToIndex();Date end = new Date();System.out.println("建立索引用时" + (end.getTime() - start.getTime()) + "毫秒");indexer.close();}}
执行结果:
正在建立索引 : e:\lucene\test\a.txt正在建立索引 : e:\lucene\test\b.txt正在建立索引 : e:\lucene\test\c.txt正在建立索引 : e:\lucene\test\d.txt建立索引用时109毫秒
生成的索引文件:
查找:
package com.xin;import java.io.File;import java.util.Date;import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.index.DirectoryReader;import org.apache.lucene.index.IndexReader;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.search.TopDocs;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.util.Version;/** * @author chongxin * @since 2013/6/19 * @version Lucene 4.3.1 * */public class LuceneSearch {// 声明一个IndexSearcher对象private IndexSearcher searcher = null;// 声明一个Query对象private Query query = null;private String field = "contents";public LuceneSearch() {try {IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(Constants.INDEX_STORE_PATH)));searcher = new IndexSearcher(reader);} catch (Exception e) {e.printStackTrace();}} //返回查询结果 public final TopDocs search(String keyword) {System.out.println("正在检索关键字 : " + keyword);try {Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);QueryParser parser = new QueryParser(Version.LUCENE_40, field,analyzer);// 将关键字包装成Query对象query = parser.parse(keyword);Date start = new Date();TopDocs results = searcher.search(query, 5 * 2);Date end = new Date();System.out.println("检索完成,用时" + (end.getTime() - start.getTime())+ "毫秒");return results;} catch (Exception e) {e.printStackTrace();return null;}} //打印结果public void printResult(TopDocs results) {ScoreDoc[] h = results.scoreDocs;if (h.length == 0) {System.out.println("对不起,没有找到您要的结果。");} else {for (int i = 0; i < h.length; i++) {try {Document doc = searcher.doc(h[i].doc);System.out.print("这是第" + i + "个检索到的结果,文件名为:");System.out.println(doc.get("path"));} catch (Exception e) {e.printStackTrace();}}}System.out.println("--------------------------");}public static void main(String[] args) throws Exception {LuceneSearch test = new LuceneSearch();TopDocs h = null;h = test.search("中国");test.printResult(h);h = test.search("人民");test.printResult(h);h = test.search("共和国");test.printResult(h);}}
- Lucene4.3入门
- Lucene4.3入门
- Lucene4.3入门
- Lucene4入门
- lucene4.0入门实例
- lucene4.2.1 入门
- lucene4.4入门实践
- lucene4.0入门实例
- lucene4.10.2入门
- lucene4.10.3入门
- Lucene4.0入门实例
- lucene4.0入门实例
- lucene4.3 自定义排序
- lucene4
- Lucene4 入门(2)–Field类及辅助类说明
- Lucene4.3搜索引擎开发之路分享
- Lucene4.3开发之第一步小试牛刀
- lucene4.3注意事项以及相关代码
- 主机的资安防护措施
- C++ 二进制文件读取
- uboot环境变量与内核MTD分区关系
- HRBUST 1787 矩阵乘法
- Background Indexer Crash Recoveryjava.lang.StackOverflowError
- Lucene4.3入门
- hdu 3685 Rotational Painting
- HDU 2795 Billboard / 线段树单点更新
- 运输层TCP协议详细介绍
- Virtualbox虚拟Ubuntu共享文件夹设置
- 链栈的操作,附一个行编辑器程序。
- win7 右键无法新建txt
- Windows Socket 非阻塞模式开发
- Windows socket之Select模型开发