lucene创建索引
1.导入jar包
2.创建实体Bean
package com.zhishang.lucene;/*** Created by Administrator on 2017/7/8.*/ public class HtmlBean {private String title;private String content;private String url;public void setTitle(String title) {this.title = title;}public void setContent(String content) {this.content = content;}public void setUrl(String url) {this.url = url;}public String getTitle() {return title;}public String getContent() {return content;}public String getUrl() {return url;} }
3.创建工具Bean
package com.zhishang.lucene;import net.htmlparser.jericho.Element; import net.htmlparser.jericho.HTMLElementName; import net.htmlparser.jericho.Source; import org.junit.Test;import java.io.File; import java.io.IOException;/*** Created by Administrator on 2017/7/8.*/ public class HtmlBeanUtil {public static HtmlBean parseHtml(File file){try {Source sc = new Source(file);Element element = sc.getFirstElement(HTMLElementName.TITLE);if (element == null || element.getTextExtractor() == null){return null;}HtmlBean htmlBean = new HtmlBean();htmlBean.setTitle(element.getTextExtractor().toString());htmlBean.setContent(sc.getTextExtractor().toString());htmlBean.setUrl(file.getAbsolutePath());return htmlBean;} catch (IOException e) {e.printStackTrace();}return null;} }
4.创建操作Bean
package com.zhishang.lucene;import org.apache.commons.io.FileUtils; import org.apache.commons.io.filefilter.TrueFileFilter; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.*; 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.store.RAMDirectory; import org.apache.lucene.util.Version; import org.junit.Test; import org.wltea.analyzer.lucene.IKAnalyzer;import java.io.File; import java.io.IOException; import java.util.Collection;/*** Created by Administrator on 2017/7/7.*/ public class CreateIndex {public static final String indexDir = "G:/index";public static final String dataDir = "G:/data";public void createIndex(){try {Directory dir = FSDirectory.open(new File(indexDir));//分词器Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_4_9);IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_9,analyzer);config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);IndexWriter writer = new IndexWriter(dir,config);File file = new File(dataDir);RAMDirectory ramdir = new RAMDirectory();Analyzer analyzer1 = new IKAnalyzer();IndexWriterConfig config1 = new IndexWriterConfig(Version.LUCENE_4_9,analyzer1);IndexWriter ramWriter = new IndexWriter(ramdir,config1);Collection<File> files = FileUtils.listFiles(file, TrueFileFilter.INSTANCE,TrueFileFilter.INSTANCE);int count = 0;for(File f:files){HtmlBean bean = HtmlBeanUtil.parseHtml(f);if(bean != null){Document document = new Document();document.add(new StringField("title",bean.getTitle(), Field.Store.YES));document.add(new TextField("content",bean.getContent(), Field.Store.YES));document.add(new StringField("url",bean.getUrl(), Field.Store.YES));ramWriter.addDocument(document);count++;if (count == 50){ramWriter.close();writer.addIndexes(ramdir);ramdir = new RAMDirectory();Analyzer analyzer2 = new IKAnalyzer();IndexWriterConfig config2 = new IndexWriterConfig(Version.LUCENE_4_9,analyzer2);ramWriter = new IndexWriter(ramdir,config2);count = 0;}}}writer.close();} catch (IOException e) {e.printStackTrace();}} }
5.创建测试Bean
package com.zhishang.lucene;import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; 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; import org.junit.Test;import java.io.File;/*** Created by Administrator on 2017/7/8.*/ public class LuceneBean {/*创建索引*/@Testpublic void createIndex(){File file = new File(CreateIndex.indexDir);if (file.exists()){file.delete();file.mkdirs();}CreateIndex createIndex = new CreateIndex();createIndex.createIndex();} }
6.查看生成的索引文件
转载于:https://blog.51cto.com/suyanzhu/1945466
lucene创建索引相关推荐
- 【示例】Lucene创建索引库编程步骤
[示例]Lucene创建索引库编程步骤
- lucene创建索引_Lucene概述第一部分:创建索引
lucene创建索引 介绍 我最近一直在与开源搜索引擎Lucene合作 . 我不是专家,但是由于我只是浏览了一些相当稀疏的文档并将应用程序从Lucene的很旧的版本迁移到了最新版本的2.4,所以我在总 ...
- lucene创建索引时出错:扑捉到FileNotFoundException
lucene创建索引文件出错,如下图: 解决方案: 解锁即可 代码如下: bool isUpdate = IndexReader.IndexExists(directory);if (isUpdate ...
- 搜索引擎学习(二)Lucene创建索引
PS:需要用到的jar包: 代码实现 1.工程结构 2.设置工程依赖的jar包 3.代码实现 /*** Lucene入门* 创建索引*/ public class CreateIndex {/*** ...
- Lucene创建索引入门案例
最近在学习lucene,参考网上的资料写了一个简单搜索demo: 项目jar包: //索引关键类 <pre name="code" class="java" ...
- lucene 创建索引慢的问题
网上随便一搜都能搜到很多关于lucene的教程,这里就不细展开了.简单说下过程: IndexWriterConfig indexWriterConfig = new IndexWriterConfig ...
- Lucene概述第一部分:创建索引
介绍 我最近一直在与开源搜索引擎Lucene合作 . 我不是专家,但是由于我只是浏览了一些相当稀疏的文档并将应用程序从Lucene的很旧的版本迁移到了最新版本的2.4,所以我在总体上很清楚. Luce ...
- Lucene构建索引的原理及源代码分析
文章目录 1. Lucene是什么 2. 全文检索是什么 3. 术语 4. 创建索引过程 4.1 Lucene创建索引示例代码 4.2 分词的过程 4.2.1 原理 4.2.2 源代码 4.3 建索引 ...
- lucene全文搜索之三:生成索引字段,创建索引文档(给索引字段加权)基于lucene5.5.3...
前言:上一章中我们已经实现了索引器的创建,但是我们没有索引文档,本章将会讲解如何生成字段.创建索引文档,给字段加权以及保存文档到索引器目录 luncene5.5.3集合jar包下载地址:http:// ...
最新文章
- LeetCode中等题之最简分数
- 小目标检测的增强算法
- 安全领域应该关注的网站
- “这块布的艺术成分都几高唧!”“有几高啊?”“三、四层楼那么高啦。”...
- C++设计模式--适配器模式
- VTK:Arbitrary3DCursor用法实战
- 一对多分页查询mysql编写_一对多分页的SQL到底应该怎么写?
- 使用适用于Java 2的AWS开发工具包的AWS DynamoDB版本字段
- 紫书搜索 习题7-4 UVA - 818 Cutting Chains 暴力+dfs判环+位运算
- Powershell tricks::Powershell Remoting
- 计算机WIN7系统网络访问权限设置,win7系统ipv6无网络访问权限如何解决?
- 数据分析【实践】——教育行业指标体系搭建和生命周期维护
- TypeError: conv2d() received an invalid combination of arguments
- Maven学习之路(五)maven的灵活构建--属性、profile和资源过滤
- 阿里巴巴图标库(iconfont)使用
- HDR电视显示技术概况及标准发展前景汇总
- ldd 执行结果:不是动态可执行文件
- 一些企业个人网银的U盾或是usbkey在web(IE)网页中无法使用的解决方法
- Linux高级应用(十)控制蜂鸣器的应用程序
- 03-Tensorboard的使用 (老衲又卷土重来了!!!)