创建索引之代码开发

摘要:
使用indexwriter对象创建索引。创建一个java项目并导入jar包。1) 指定存储索引库的目录对象。2) 指定用于分析文档内容的解析器。创建Document对象,创建字段对象,并将字段添加到Document对象。FirstLucene.java:1packagecom.hk。lucene;23 importstaticorg.junit.Assert.*;4导入java.io。文件5 importorg.apache.mons.io。文件应用程序;6进口葡萄酒、葡萄酒、葡萄酒分析。分析仪;7进口葡萄酒、葡萄酒、葡萄酒分析。TokenStream;8进口。CJK分析仪;9importorg.apach.lucene.analysis.cn.smart。SmartChineseAnalyzer;10importorg.apach.lucene.analysis标准。标准分析仪;11进口产品名称。CharTermAttribute;12进口。偏移属性;13 importorg.apach.lucene.document。文件14importorg.apache.lucene.document。领域15 importorg.apach.lucene.document.Field。百货商店16 importorg.apach.lucene.document。LongField公司;17 importorg.apach.lucene.document。StoredField;18 importorg.apach.lucene.document。文本字段;19进口机构名称。DirectoryReader;20进口有机玻璃。索引阅读器;21进口机构名称。IndexWriter;22importorg.apach.lucene.index。IndexWriterConfig;23进口机构名称。学期24 importorg.apach.lucene.search。索引搜索器;25 importorg.apach.lucene.search。查询26 importorg.apach.lucene.search。ScoreDoc;27 importorg.apach.lucene.search。术语查询;28 importorg.apach.lucene.search。TopDocs;29importorg.apach.lucene.store。目录30进口水果。FSDirectory;31进口水果。RAM目录;32importorg.apach.lucene.util。版本33importorg.junit。测验34导入器.分析器.透明。IKAnalyzer;3536publicclassFirstLucene{3738//创建索引39@Test40publicvoidtestIndex()throwsException{41//步骤1:创建一个java项目并导入jar包。43Directorydirectory=FSDirectory.open;44//Directory=newRAMDirectory();//将索引保存到内存45//Analyzeranalyzer=newStandardAnalyzer();//Official recommendation 46Analyzeranalysizer=newIKAnalyzer() ;// 官方推荐47IndexWriterConfig=newIndexWriter配置;48IndexWriterindexWriter=新IndexWriter;49//1)指定存储索引库的目录对象50//2)指定分析文档内容的解析器。51//步骤3:创建字段对象并将字段添加到文档对象。52Filef=newFile;53文件[]列表文件=f。listFiles();54用于{55//步骤3:创建文档对象。

【创建索引库】

使用indexwriter对象创建索引。

【实现步骤】

(1)创建一个java工程,并导入jar包。

(2)创建一个indexwriter对象。

        1)指定索引库的存放位置Directory对象。

        2)指定一个分析器,对文档内容进行分析。

(3)创建Document对象

(4)创建filed对象,将field添加到Document对象中。

(5)使用indexwriter对象将Document对象写入到索引库,此过程进行索引创建,并将索引和Document对象写入索引库。

(6)关闭IndexWriter对象。

FirstLucene.java:

  1 package com.hk.lucene;
  2 
  3 import static org.junit.Assert.*;
  4 import java.io.File;
  5 import org.apache.commons.io.FileUtils;
  6 import org.apache.lucene.analysis.Analyzer;
  7 import org.apache.lucene.analysis.TokenStream;
  8 import org.apache.lucene.analysis.cjk.CJKAnalyzer;
  9 import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
 10 import org.apache.lucene.analysis.standard.StandardAnalyzer;
 11 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
 12 import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
 13 import org.apache.lucene.document.Document;
 14 import org.apache.lucene.document.Field;
 15 import org.apache.lucene.document.Field.Store;
 16 import org.apache.lucene.document.LongField;
 17 import org.apache.lucene.document.StoredField;
 18 import org.apache.lucene.document.TextField;
 19 import org.apache.lucene.index.DirectoryReader;
 20 import org.apache.lucene.index.IndexReader;
 21 import org.apache.lucene.index.IndexWriter;
 22 import org.apache.lucene.index.IndexWriterConfig;
 23 import org.apache.lucene.index.Term;
 24 import org.apache.lucene.search.IndexSearcher;
 25 import org.apache.lucene.search.Query;
 26 import org.apache.lucene.search.ScoreDoc;
 27 import org.apache.lucene.search.TermQuery;
 28 import org.apache.lucene.search.TopDocs;
 29 import org.apache.lucene.store.Directory;
 30 import org.apache.lucene.store.FSDirectory;
 31 import org.apache.lucene.store.RAMDirectory;
 32 import org.apache.lucene.util.Version;
 33 import org.junit.Test;
 34 import org.wltea.analyzer.lucene.IKAnalyzer;
 35 
 36 public class FirstLucene {
 37 
 38     // 创建索引
 39     @Test
 40     public void testIndex() throws Exception {
 41         // 第一步:创建一个java工程,并导入jar包。
 42         // 第二步:创建一个indexwriter对象。
 43         Directory directory = FSDirectory.open(new File("D:\temp\index"));
 44         // Directory directory = new RAMDirectory();//保存索引到内存中 (内存索引库)
 45         //Analyzer analyzer = new StandardAnalyzer();// 官方推荐
 46         Analyzer analyzer = new IKAnalyzer();// 官方推荐
 47         IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, analyzer);
 48         IndexWriter indexWriter = new IndexWriter(directory, config);
 49         // 1)指定索引库的存放位置Directory对象
 50         // 2)指定一个分析器,对文档内容进行分析。
 51         // 第三步:创建field对象,将field添加到document对象中。
 52         File f = new File("D:\Lucene&solr\searchsource");
 53         File[] listFiles = f.listFiles();
 54         for (File file : listFiles) {
 55             // 第三步:创建document对象。
 56             Document document = new Document();
 57             // 文件名称
 58             String file_name = file.getName();
 59             Field fileNameField = new TextField("fileName", file_name, Store.YES);
 60             // 文件大小
 61             long file_size = FileUtils.sizeOf(file);
 62             Field fileSizeField = new LongField("fileSize", file_size, Store.YES);
 63             // 文件路径
 64             String file_path = file.getPath();
 65             Field filePathField = new StoredField("filePath", file_path);
 66             // 文件内容
 67             String file_content = FileUtils.readFileToString(file);
 68             Field fileContentField = new TextField("fileContent", file_content, Store.NO);
 69 
 70             document.add(fileNameField);
 71             document.add(fileSizeField);
 72             document.add(filePathField);
 73             document.add(fileContentField);
 74             // 第四步:使用indexwriter对象将document对象写入索引库,此过程进行索引创建。并将索引和document对象写入索引库。
 75             indexWriter.addDocument(document);
 76 
 77         }
 78         // 第五步:关闭IndexWriter对象。
 79         indexWriter.close();
 80     }

运行结果:

在D: empindex下:

创建索引之代码开发第1张

免责声明:文章转载自《创建索引之代码开发》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇在PyQt中构建 Python 菜单栏、菜单和工具栏Node中的cookie的使用下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

JBOSS目录结构详细说明

一、下载与安装。 如何下载以及安装配置,请参考:Windows下JBOSS安装配置图文教程 二、现在主要了解一下JBOSS目录结构。 1. 主目录: E:jboosjboss-6.1.0.Final bin 开始和停止JBoss的地方。 其中有两个主要的批处理文件:run.bat和shutdown.bat。要启动JBoss只要执行run.bat文件即可;...

给MongoDB添加索引

用过数据库的都知道,数据库索引与书籍的索引类似,都是用来帮助快速查找的。   MongoDB的索引跟关系型数据库的索引几乎一致。       1. 索引的创建   mongodb采用ensureIndex来创建索引,如:   db.user.ensureIndex({"name":1})   表示在user集合的name键创建一个索引,这里的1表示索引创建...

Oracle中的Truncate和Delete语句

Oracle中的Truncate和Delete语句   首先讲一下,truncate命令:   语法:TRUNCATE  TABLE  table; 表格里的数据被清空,存储空间被释放。 运行后会自动提交,包括之前其它未提交的会话,因而一旦清空无法回退。 只有表格的创建者或者其他拥有删除任意表格权限的用户(如DBA)才能清空表格。 TRUNCATE  T...

Lucene架构

      先整体上看一下Lucene的架构设计图(见下图),先看上层应用,首先是信息采集的过程,文件系统、数据库、万维网以及手工输入的文件都可以作为信息采集的对象,也是要搜索的文档的来源,采集万维网上的信息一般使用网络爬虫。完成信息采集之后到Lucene层面有两大任务:索引文档和搜索文档,索引文档的过程完成由原始文档到倒排索引的构建过程,搜索文档用以处理...

手动更新nexus的索引

安装nexus 1.下载 源码包nexus-2.13.0-01-bundle.tar.gz 下载地址http://www.sonatype.org/nexus/ 2.解压源码包 3.启动并访问 ./nexus start 启动 restart重启 stop 关闭 ip:端口/nexus访问 端口可在config的nexus.properties中修改 默认...

Shapefile 文件扩展名

Shapefile 是一种用于存储地理要素的几何位置和属性信息的非拓扑简单格式。shapefile 是可以在 ArcGIS 中使用和编辑的其中一种空间数据格式。 shapefile 格式在应存储在同一项目工作空间且使用特定文件扩展名的三个或更多文件中定义地理引用要素的几何和属性。这些文件是: .shp - 用于存储要素几何的主文件;必需文件。 .shx...