Luncene学习 第一天 《入门程序》
整个luncene 流程
下面贴出代码
package com.zuoyan.lucene.demo; import java.io.File; import org.apache.commons.io.FileUtils;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.StoredField;
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;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer; /**
* Lucene 的第一个程序
*
* @author zuoyan
*
*/
public class LuceneDemo01 { /*
* 创建索引 1.首先创建IndexWriter对象 他有两个参数 1.Directory 2.IndexWriterConfig
*/
@Test
public void testCreateIndex() throws Exception {
String filePath = "G:\\temp\\index";
Directory directory = FSDirectory.open(new File(filePath));
Analyzer analyzer = new IKAnalyzer();
IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, analyzer);
IndexWriter indexWriter = new IndexWriter(directory, config);
// 创建field对象,将field添加到document对象中
File f = new File("G:\\temp\\files");
File[] listFiles = f.listFiles();
for (File file : listFiles) {
// 创建Document对象。
Document document = new Document();
// 文件名称
String fileName = file.getName();
Field fileNameField = new TextField("fileName", fileName, Store.YES);
// 文件大小
long fileSize = FileUtils.sizeOf(file);
Field fileSizeField = new LongField("fileSize", fileSize, Store.YES);
// 文件路径
String file_path = file.getPath();
Field filePathField = new StoredField("filePath", file_path);
// 文件内容
String file_content = FileUtils.readFileToString(file);
Field fileContentField = new TextField("fileContent", file_content, Store.NO); document.add(fileNameField);
document.add(fileSizeField);
document.add(filePathField);
document.add(fileContentField);
// 第四步:使用indexwriter对象将document对象写入索引库,此过程进行索引创建。并将索引和document对象写入索引库。
indexWriter.addDocument(document); } } }
创建出来的文件索引
原来的文件
Luncene学习 第一天 《入门程序》的更多相关文章
- mybais学习记录一——入门程序
一.传统连接数据库和执行sql的不足 1.数据库连接,使用时就创建,不使用立即释放,对数据库进行频繁连接开启和关闭,造成数据库资源浪费,影响 数据库性能. 设想:使用数据库连接池管理数据库连接. 2. ...
- Delphi第一个入门程序——鼠标点击计数 - imsoft.cnblogs
实现的效果如下: 制作要点: 添加一个按钮Button1和一个标签Label1,并双击按钮进入编程界面在var Form1: TForm1;下面一行加上 n:integer;//定义变量. 然后在 ...
- Python第一个入门程序
#!usr/bin/env python3 #在UNIX上,当某程序在控制台中被引用时,该文件的头两个字节先被读入.如果这两个字节是ASCII字符 #!, #shell就会认为该文件将要由解释器执行, ...
- Bootstrap3.0学习第一轮(入门)
详情请查看 http://aehyok.com/Blog/Detail/7.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:ht ...
- JAVAEE——Mybatis第一天:入门、jdbc存在的问题、架构介绍、入门程序、Dao的开发方法、接口的动态代理方式、SqlMapConfig.xml文件说明
1. 学习计划 第一天: 1.Mybatis的介绍 2.Mybatis的入门 a) 使用jdbc操作数据库存在的问题 b) Mybatis的架构 c) Mybatis的入门程序 3.Dao的开发方法 ...
- JAVAEE——SpringMVC第一天:介绍、入门程序、架构讲解、SpringMVC整合MyBatis、参数绑定、SpringMVC和Struts2的区别
1. 学习计划 第一天 1.SpringMVC介绍 2.入门程序 3.SpringMVC架构讲解 a) 框架结构 b) 组件说明 4.SpringMVC整合MyBatis 5.参数绑定 a) Sp ...
- C语言编程入门之--第一章初识程序
第一章 初识程序 导读:计算机程序无时不刻的影响着人类的生活,现代社会已经离不开程序,程序的作用如此巨大,那么程序到底是什么呢?本章主要讨论程序的概念,唤起读者对程序的兴趣,同时对C语言程序与其它语言 ...
- Elasticsearch7.X 入门学习第一课笔记----基本概念
原文:Elasticsearch7.X 入门学习第一课笔记----基本概念 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https: ...
- AndroidStudio学习笔记-第一个安卓程序
要带一个本科生做一部分跟安卓有点关系的项目,于是趁着机会学习一下编写安卓程序. 第一篇材料来自谷歌官方,传送门:https://developer.android.com/training/basic ...
随机推荐
- python之小数据池
代码块 Python 程序 是由代码块构造的.块是一个python程序的文本,它是作为一个执行单元的. 代码块:一个模块,一个函数,一个类,一个文件等都是一个代码块. 而作为交互方式输入的每个命令都是 ...
- Lonsdor K518ISE free update news on what makes and year can work
Lonsdor K518ISE engineers recently tested a number of cars and verified working great, below are tes ...
- java初学者必看的学习路线
不管在编程语言的排行榜中,还是在大多数企业应用的广泛程度来看,Java一直都是当之无愧的榜首.Java语言有着独特的魅力吸引着广大的年轻人去学习,每个人学习的方式方法不一样. 第一步:首先要做好学习前 ...
- ID3和C4.5分类决策树算法 - 数据挖掘算法(7)
(2017-05-18 银河统计) 决策树(Decision Tree)是在已知各种情况发生概率的基础上,通过构成决策树来判断其可行性的决策分析方法,是直观运用概率分析的一种图解法.由于这种决策分支画 ...
- @RefreshScope 的作用
让在application.properties里自定义的变量也能通过@Value 注解正常注入
- 模型(model-->orm)系统
一.ORM介绍 1)ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 简单的说,ORM是通过 ...
- Makefile shell subst $(1)
MAKE_3_80_realpath = $(shell $(top_srcdir)/scripts/realpath.sh '$(subst $(SQUOTE),\\$(SQUOTE),$(1 ...
- 使用准现网的数据,使用本地的样式脚本,本地调试准现网页面(PC适用)
原理: 本地逻辑,重新渲染 步骤: 1.安装插件:Tampermonkey 度盘:https://pan.baidu.com/s/1bpBVVT9 2.设置: 点击插件-->仪表盘 添加脚本 将 ...
- linux装sqlite3
下载sqlite3源码包 tar xvfz sqlite-src-3.3.5 cd sqlite-3.3.5 ./configure –no-tcl make python继续一次. apt inst ...
- Docker学习笔记之使用 Docker Hub 中的镜像
0x00 概述 自己编写 Dockerfile 能够很好的实现我们想要的程序运行环境,不过如果装有我们想要环境的镜像已经由热心的开发者构建好并共享在 Docker Hub 上,直接使用它们就会远比自己 ...