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创建索引相关推荐

  1. 【示例】Lucene创建索引库编程步骤

    [示例]Lucene创建索引库编程步骤

  2. lucene创建索引_Lucene概述第一部分:创建索引

    lucene创建索引 介绍 我最近一直在与开源搜索引擎Lucene合作 . 我不是专家,但是由于我只是浏览了一些相当稀疏的文档并将应用程序从Lucene的很旧的版本迁移到了最新版本的2.4,所以我在总 ...

  3. lucene创建索引时出错:扑捉到FileNotFoundException

    lucene创建索引文件出错,如下图: 解决方案: 解锁即可 代码如下: bool isUpdate = IndexReader.IndexExists(directory);if (isUpdate ...

  4. 搜索引擎学习(二)Lucene创建索引

    PS:需要用到的jar包: 代码实现 1.工程结构 2.设置工程依赖的jar包 3.代码实现 /*** Lucene入门* 创建索引*/ public class CreateIndex {/*** ...

  5. Lucene创建索引入门案例

    最近在学习lucene,参考网上的资料写了一个简单搜索demo: 项目jar包: //索引关键类 <pre name="code" class="java" ...

  6. lucene 创建索引慢的问题

    网上随便一搜都能搜到很多关于lucene的教程,这里就不细展开了.简单说下过程: IndexWriterConfig indexWriterConfig = new IndexWriterConfig ...

  7. Lucene概述第一部分:创建索引

    介绍 我最近一直在与开源搜索引擎Lucene合作 . 我不是专家,但是由于我只是浏览了一些相当稀疏的文档并将应用程序从Lucene的很旧的版本迁移到了最新版本的2.4,所以我在总体上很清楚. Luce ...

  8. Lucene构建索引的原理及源代码分析

    文章目录 1. Lucene是什么 2. 全文检索是什么 3. 术语 4. 创建索引过程 4.1 Lucene创建索引示例代码 4.2 分词的过程 4.2.1 原理 4.2.2 源代码 4.3 建索引 ...

  9. lucene全文搜索之三:生成索引字段,创建索引文档(给索引字段加权)基于lucene5.5.3...

    前言:上一章中我们已经实现了索引器的创建,但是我们没有索引文档,本章将会讲解如何生成字段.创建索引文档,给字段加权以及保存文档到索引器目录 luncene5.5.3集合jar包下载地址:http:// ...

最新文章

  1. LeetCode中等题之最简分数
  2. 小目标检测的增强算法
  3. 安全领域应该关注的网站
  4. “这块布的艺术成分都几高唧!”“有几高啊?”“三、四层楼那么高啦。”...
  5. C++设计模式--适配器模式
  6. VTK:Arbitrary3DCursor用法实战
  7. 一对多分页查询mysql编写_一对多分页的SQL到底应该怎么写?
  8. 使用适用于Java 2的AWS开发工具包的AWS DynamoDB版本字段
  9. 紫书搜索 习题7-4 UVA - 818 Cutting Chains 暴力+dfs判环+位运算
  10. Powershell tricks::Powershell Remoting
  11. 计算机WIN7系统网络访问权限设置,win7系统ipv6无网络访问权限如何解决?
  12. 数据分析【实践】——教育行业指标体系搭建和生命周期维护
  13. TypeError: conv2d() received an invalid combination of arguments
  14. Maven学习之路(五)maven的灵活构建--属性、profile和资源过滤
  15. 阿里巴巴图标库(iconfont)使用
  16. HDR电视显示技术概况及标准发展前景汇总
  17. ldd 执行结果:不是动态可执行文件
  18. 一些企业个人网银的U盾或是usbkey在web(IE)网页中无法使用的解决方法
  19. Linux高级应用(十)控制蜂鸣器的应用程序
  20. 03-Tensorboard的使用 (老衲又卷土重来了!!!)

热门文章

  1. lisp函数大全 微盘_LISP函数(分类)大全
  2. python ctypes 回调函数_Python ctypes中具有自定义类型的回调
  3. vim之格式化代码功能——gg=G
  4. AutoML Challenge 历史回顾
  5. 麒麟操作系统配置网络_讲解银河麒麟桌面操作系统
  6. 论文,质量管理+进度管理(主质量)
  7. 软件管理定律系列之布鲁克斯定律
  8. 提高mysql千万级大数据SQL查询优化30条经验
  9. Winform中实现颜色拾取器获取RGB与16进制颜色程序与源码分享
  10. Winform中实现文件另存为后并打开文件