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 ...
随机推荐
- 【转】基于Selenium的web自动化框架(python)
1 什么是selenium Selenium 是一个基于浏览器的自动化工具,它提供了一种跨平台.跨浏览器的端到端的web自动化解决方案.Selenium主要包括三部分:Selenium IDE.Sel ...
- .net 缓存
缓存有很多实现方法,所有这些可以被分为两类,基于内存的缓存和基于磁盘的缓存: 1. 内存驻留缓存——包含在内存中临时存储数据的所有实现方法,通常在以下情况下使用: a) 应用程序频繁使用 ...
- First Wainberg-2018-Deep learning in biomedicine Experience
ppt+paper 链接:https://pan.baidu.com/s/14toqjcSJti5ZXT3ff4rwIA 提取码:xgkt
- py3中reduce
前因 因为 Guido 先生讨厌 reduce.(Because Guido hates it. ) 详细过程请翻阅原文:http://www.artima.com/forums/flat.jsp?f ...
- DOM jquery
DOM 文档对象模型(Document Object Model)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的内容和呈现方式.我们最为关心的是,DOM ...
- 问题 1084: 用筛法求之N内的素数。
#include <iostream> #include <cstdio> #include <cstring> #include <string> # ...
- JDK源码之Lock接口
public interface Lock { //阻塞的获取锁,如果获取到锁,从该方法返回 void lock(); //可中断的获取锁,该方法会响应中断,在锁的获取中可以中断当前线程 void l ...
- Spring Boot(十八):使用Spring Boot集成FastDFS
Spring Boot(十八):使用Spring Boot集成FastDFS 环境:Spring Boot最新版本1.5.9.jdk使用1.8.tomcat8.0 功能:使用Spring Boot将文 ...
- dubbo rpc调用抛出的Exception处理
关于dubbo的Exception堆栈被吃处理,网上已经有比较多的解决方法,在我们的应用场景中,不希望RPC调用对方抛出业务exception,而是通过Resp中的errorCode,errorMsg ...
- [ERROR] InnoDB: Trying to access page number 7 in space 957, space name XXX which is outside the tablespace bounds
早上,测试说演示环境mysql老实断开重连,一update就挂,经查日志,有如下异常: 2017-04-05T23:13:01.729250+08:00 17065 [ERROR] InnoDB: T ...