整个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学习 第一天 《入门程序》的更多相关文章

  1. mybais学习记录一——入门程序

    一.传统连接数据库和执行sql的不足 1.数据库连接,使用时就创建,不使用立即释放,对数据库进行频繁连接开启和关闭,造成数据库资源浪费,影响 数据库性能. 设想:使用数据库连接池管理数据库连接. 2. ...

  2. Delphi第一个入门程序——鼠标点击计数 - imsoft.cnblogs

    实现的效果如下: 制作要点: 添加一个按钮Button1和一个标签Label1,并双击按钮进入编程界面在var  Form1: TForm1;下面一行加上  n:integer;//定义变量. 然后在 ...

  3. Python第一个入门程序

    #!usr/bin/env python3 #在UNIX上,当某程序在控制台中被引用时,该文件的头两个字节先被读入.如果这两个字节是ASCII字符 #!, #shell就会认为该文件将要由解释器执行, ...

  4. Bootstrap3.0学习第一轮(入门)

    详情请查看 http://aehyok.com/Blog/Detail/7.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:ht ...

  5. JAVAEE——Mybatis第一天:入门、jdbc存在的问题、架构介绍、入门程序、Dao的开发方法、接口的动态代理方式、SqlMapConfig.xml文件说明

    1. 学习计划 第一天: 1.Mybatis的介绍 2.Mybatis的入门 a) 使用jdbc操作数据库存在的问题 b) Mybatis的架构 c) Mybatis的入门程序 3.Dao的开发方法 ...

  6. JAVAEE——SpringMVC第一天:介绍、入门程序、架构讲解、SpringMVC整合MyBatis、参数绑定、SpringMVC和Struts2的区别

    1. 学习计划   第一天 1.SpringMVC介绍 2.入门程序 3.SpringMVC架构讲解 a) 框架结构 b) 组件说明 4.SpringMVC整合MyBatis 5.参数绑定 a) Sp ...

  7. C语言编程入门之--第一章初识程序

    第一章 初识程序 导读:计算机程序无时不刻的影响着人类的生活,现代社会已经离不开程序,程序的作用如此巨大,那么程序到底是什么呢?本章主要讨论程序的概念,唤起读者对程序的兴趣,同时对C语言程序与其它语言 ...

  8. Elasticsearch7.X 入门学习第一课笔记----基本概念

    原文:Elasticsearch7.X 入门学习第一课笔记----基本概念 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https: ...

  9. AndroidStudio学习笔记-第一个安卓程序

    要带一个本科生做一部分跟安卓有点关系的项目,于是趁着机会学习一下编写安卓程序. 第一篇材料来自谷歌官方,传送门:https://developer.android.com/training/basic ...

随机推荐

  1. Java基础(basis)-----关键字this和super的作用

    1.关键字this 可以用来修饰属性.方法.构造器:this理解为当前对象或当前正在创建的对象 局部变量与成员变量同名,成员变量被屏蔽,用"this.成员变量"的方式访问成员变量 ...

  2. Shell 比较两个数的大小

    格式很重要多一个空格少一个空格都可能出错 li@ubuntu:~/test$ cat compare.sh #!/bin/bash read x read y if [ $x -lt $y ] the ...

  3. TestNG 搭建测试框架 自动化测试

    框架层级及基本组件:    参考:https://www.cnblogs.com/jier888/p/8998724.html Java作为开发语言 Maven管理项目及Jar包 Testng作为测试 ...

  4. vue打包报内存溢出

    vue-cli 构建的项目:package.json 文件里修改: "build": "node build/build.js" 修改为: "buil ...

  5. GoldenGate Logdump基本使用

    Logdump是GoldenGate复制软件中附带的一个工具软件,在OGG的目录下可以找到.这个工具主要用于分析OGG生成的队列文件,查找记录.统计队列文件中的数据等. 在OGG安装目录下执行logd ...

  6. react复习总结(1)--react组件开发基础

    这次是年后第一次发文章,也有很长一段时间没有写文章了.准备继续写.总结是必须的. 最近一直在业余时间学习和复习前端相关知识点,在一个公司呆久了,使用的技术不更新,未来真的没有什么前景,特别是我们这种以 ...

  7. 今日总结(linux和plsql)

    #case ...when语句(根据字段不同值显示不同结果) ##1)case ...when语句的使用方法一: 语法格式: case column_name when value1 then res ...

  8. java使用wait(),notify(),notifyAll()实现等待/通知机制

    public class WaitNotify { static boolean flag=true; static Object lock=new Object(); static class Wa ...

  9. JavaScript document open() 方法:打开一个新文档

    <html> <head> <script type="text/javascript"> function createNewDoc() { ...

  10. ora-904 rowid create materialized view

    create materialized view t_v asselect t1.*,1 as marker,rowid from t1 t1union allselect t2.*,2 as mar ...