Lucene教程(一) 创建索引
2016-08-04 16:59
295 查看
简述:
由于Lucene不同的版本差距较大,,此系列教程打算把3.5版本,4.5版本,5.0版本都给出个例子,方便大家学习,也方便自己复习。
注:由于Lucene5.0版本是基于JDK1.7开发的,所以想学习的同学请配置1.7及以上的版本。故测试Lucene 6.1.0也适用Lucene 5.0中的代码。Lucene 6.1.0最低要求也是JDK1.7.
创建索引可分为主要的几步,我自己试验过,不同的版本间会有些不同,但是跟着如下的几大步骤一步一步写,问题不会太大。
创建Directory
创建IndexWriter
创建Document对象
为Document添加Field
通过IndexWriter添加文档到索引中
下面为例子代码:
3.5版本:
3.5版本比较简单,只需要Lucene核心包lucene-core即可,
例子代码如下:
4.5版本:
4.5版本需要Lucene核心包lucene-core和分词包lucene-analyzers-common,从4.0版本之后分词包从核心包分离。
代码如下:
5.0版本:
5.0版本和4.5版本一样,例子代码如下:
测试代码:
由于测试代码都一样,仅贴出5.0版本的此时代码:
由于Lucene不同的版本差距较大,,此系列教程打算把3.5版本,4.5版本,5.0版本都给出个例子,方便大家学习,也方便自己复习。
注:由于Lucene5.0版本是基于JDK1.7开发的,所以想学习的同学请配置1.7及以上的版本。故测试Lucene 6.1.0也适用Lucene 5.0中的代码。Lucene 6.1.0最低要求也是JDK1.7.
创建索引可分为主要的几步,我自己试验过,不同的版本间会有些不同,但是跟着如下的几大步骤一步一步写,问题不会太大。
创建Directory
创建IndexWriter
创建Document对象
为Document添加Field
通过IndexWriter添加文档到索引中
下面为例子代码:
3.5版本:
3.5版本比较简单,只需要Lucene核心包lucene-core即可,
例子代码如下:
package com.darren.lucene35; import java.io.File; import java.io.FileReader; 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.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.queryParser.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.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; public class HelloLucene { /** * 建立索引 */ public void index() { IndexWriter indexWriter = null; try { // 1、创建Directory // 建立在内存中 // Directory directory = new RAMDirectory(); // 建立在硬盘式 Directory directory = FSDirectory.open(new File("F:/test/lucene/index")); // 2、创建IndexWriter // 使用默认的标准分词器 Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35); IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_35, analyzer); indexWriter = new IndexWriter(directory, indexWriterConfig); File dFile = new File("F:/test/lucene/document"); File[] files = dFile.listFiles(); for (File file : files) { // 3、创建Document对象 Document document = new Document(); // 4、为Document添加Field document.add(new Field("content", new FileReader(file))); document.add(new Field("filename", file.getName(), Field.Store.YES, Field.Index.NOT_ANALYZED)); document.add(new Field("filepath", file.getAbsolutePath(), Field.Store.YES, Field.Index.NOT_ANALYZED)); // 5、通过IndexWriter添加文档到索引中 indexWriter.addDocument(document); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (indexWriter != null) { indexWriter.close(); } } catch (Exception e1) { e1.printStackTrace(); } } } }
4.5版本:
4.5版本需要Lucene核心包lucene-core和分词包lucene-analyzers-common,从4.0版本之后分词包从核心包分离。
代码如下:
package com.darren.lucene45; import java.io.File; import java.io.FileReader; 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.TextField; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; public class HelloLucene { /** * 建立索引 */ public void index() { IndexWriter indexWriter = null; try { // 1、创建Directory Directory directory = FSDirectory.open(new File("F:/test/lucene/index")); // 2、创建IndexWriter /** * 注意StandardAnalyzer与3.5版本的不同: * * StandardAnalyzer不在lucene-core包中而在lucene-analyzers-common包中 从4.0版本以后分离 */ Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_45); IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_45, analyzer); indexWriter = new IndexWriter(directory, indexWriterConfig); File dFile = new File("F:/test/lucene/document"); File[] files = dFile.listFiles(); for (File file : files) { // 3、创建Document对象 Document document = new Document(); // 4、为Document添加Field /** * 注意Field与3.5版本的不同:两个参数的构造器已过时,使用如下构造器 */ // 第三个参数是FieldType 但是定义在TextField中作为静态变量,看API也不好知道怎么写 document.add(new Field("content", new FileReader(file), TextField.TYPE_NOT_STORED)); document.add(new Field("filename", file.getName(), TextField.TYPE_STORED)); document.add(new Field("filepath", file.getAbsolutePath(), TextField.TYPE_STORED)); // 5、通过IndexWriter添加文档到索引中 indexWriter.addDocument(document); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (indexWriter != null) { indexWriter.close(); } } catch (Exception e) { e.printStackTrace(); } } } }
5.0版本:
5.0版本和4.5版本一样,例子代码如下:
package com.darren.lucene50; import java.io.File; import java.io.FileReader; import java.nio.file.FileSystems; 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.TextField; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; public class HelloLucene { /** * 建立索引 */ public void index() { IndexWriter indexWriter = null; try { // 1、创建Directory /** * 注意open方法与3.5版本和4.5版本的不同: * * 这里不再接受一个File而是Path,使用的是JDK1.7的新特性,也就是说5.0版本是基于JDK1.7开发的 * * 如何获取Path,请参照 Java7新特性--Path http://blog.csdn.net/zpf336/article/details/45074445 * */ Directory directory = FSDirectory.open(FileSystems.getDefault().getPath("F:/test/lucene/index")); // 2、创建IndexWriter /** * 注意StandardAnalyzer与3.5版本的不同: * * StandardAnalyzer不在lucene-core包中而在lucene-analyzers-common包中 从4.0版本以后分离 * * 并且不需要提供版本号 */ Analyzer analyzer = new StandardAnalyzer(); /** * 注意IndexWriterConfig与3.5版本和4.5版本的不同: * * 不需要提供版本号 */ IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer); indexWriter = new IndexWriter(directory, indexWriterConfig); File dFile = new File("F:/test/lucene/document"); File[] files = dFile.listFiles(); for (File file : files) { // 3、创建Document对象 Document document = new Document(); // 4、为Document添加Field /** * 注意Field与3.5版本的不同:两个参数的构造器已过时,使用如下构造器 * * 但是和4.5版本是相同的 */ // 第三个参数是FieldType 但是定义在TextField中作为静态变量,看API也不好知道怎么写 document.add(new Field("content", new FileReader(file), TextField.TYPE_NOT_STORED)); document.add(new Field("filename", file.getName(), TextField.TYPE_STORED)); document.add(new Field("filepath", file.getAbsolutePath(), TextField.TYPE_STORED)); // 5、通过IndexWriter添加文档到索引中 indexWriter.addDocument(document); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (indexWriter != null) { indexWriter.close(); } } catch (Exception e) { e.printStackTrace(); } } } }
测试代码:
由于测试代码都一样,仅贴出5.0版本的此时代码:
package com.darren.lucene50; import org.junit.Test; public class HelloLuceneTest { @Test public void testIndex() { HelloLucene helloLucene = new HelloLucene(); helloLucene.index(); } }
相关文章推荐
- Lucene 4.7 教程起步--创建索引
- Lucene 6.2.1入门教程(一) 创建索引和基本搜索索引
- Lucene 4.7 教程起步--创建索引
- Lucene教程(一) 创建索引初步
- 利用Lucene.net搭建站内搜索(3)---创建索引
- Lucene.net搜索——创建索引
- Lucene源代码之创建索引小结
- 如何用Lucene为目录创建索引
- DotLucene搜索引擎Demo之:创建索引
- lucene创建索引高级特性和索引创建参数优化
- Lucene的入门例子 - 创建索引,利用索引查询
- 中文Access2000速成教程--1.7 创建索引
- Lucene.NET 系列之一-------------初识Lucene&&创建索引
- Lucene中创建索引的效率和删除索引的实现
- lucene索引管理(索引创建于删除)
- lucene-创建文档索引处理框架
- 新一篇: lucene学习----创建索引 |
- lucene学习----创建索引
- Lucene 源代码剖析-9 索引创建过程
- Lucene学习-创建索引(一)