package cn.tz.lucene;

import java.io.File;
import java.util.ArrayList;
import java.util.List; import org.apache.commons.io.FileUtils;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer; public class IndexManagerTest { @Test
public void testIndexCreate() throws Exception{
//采集文件系统中的文档数据到Lucene中
//创建文档列表
List<Document> docList=new ArrayList<Document>();
//指定文件目录
File dir=new File("C:\\Users\\admin\\searchsource");
//循环文件夹
for(File file:dir.listFiles()){
String fileName = file.getName();
String fileContent=FileUtils.readFileToString(file);
Long fileSize=FileUtils.sizeOf(file);
//创建文档对象
Document document=new Document();
TextField namefield=new TextField("fileName",fileName,Store.YES);
TextField contentField=new TextField("fileContent",fileContent,Store.YES);
LongField sizeField=new LongField("fileSize",fileSize,Store.YES);
//LongField
document.add(namefield);
document.add(contentField);
document.add(sizeField);
docList.add(document);
} //创建分词器Analyzer
// Analyzer analyzer=new StandardAnalyzer(); //采用第三方的中文分词器 IKAnalyzer
Analyzer analyzer=new IKAnalyzer();
//指定索引和文档的存储目录
FSDirectory desFile=FSDirectory.open(new File("d:\\lucene"));
//创建写对象的初始化对象
IndexWriterConfig config=new IndexWriterConfig(Version.LUCENE_4_10_3,analyzer);
//创建索引和文档的写对象
IndexWriter writer=new IndexWriter(desFile,config);
//将文档 加到索引和文档的写对象中
for(Document doc:docList){
writer.addDocument(doc);
}
//提交
writer.commit();
//关闭流
writer.close();
} @Test
public void testIndexDel() throws Exception{
Analyzer analyzer=new IKAnalyzer();
FSDirectory dir=FSDirectory.open(new File("d:\\lucene"));
IndexWriterConfig config=new IndexWriterConfig(Version.LUCENE_4_10_3, analyzer);
IndexWriter writer=new IndexWriter(dir, config); //删除所有索引
//writer.deleteAll(); //删除指定索引(根据域删除)
//Term("域名","搜索的关键字")
writer.deleteDocuments(new Term("fileName","apache"));
//提交
writer.commit();
//关闭
writer.close(); } /**
* 更新操作<br>:
* <li>按照Term进行指定域搜索关键字,如果查到记录就删除,然后将更新后的内容重新生成Document对象</li>
* <li>如果没有查到记录,则直接将更新后的内容添加一个Document对象</li>
*/
@Test
public void testIndexUpdate() throws Exception{
Analyzer analyzer=new IKAnalyzer();
//存储目录
FSDirectory dir=FSDirectory.open(new File("d:\\lucene"));
IndexWriterConfig config=new IndexWriterConfig(Version.LUCENE_4_10_3, analyzer);
IndexWriter writer=new IndexWriter(dir,config); //按照fileName域进行搜索关键字"web"
Term term=new Term("fileName","哦哦"); Document doc=new Document();
doc.add(new TextField("fileName","not exit",Store.YES));
doc.add(new LongField("fileSize",100L,Store.YES));
doc.add(new StringField("fileContent", "egfao容", Store.YES));
//更新
writer.updateDocument(term, doc);
//提交
writer.commit();
//关闭
writer.close();
}
}

  

Lucene——索引的创建、删除、修改的更多相关文章

  1. hive 学习系列二(数据库的创建删除修改) 拿走,不谢。

    database 相当于一个目录或者命名空间,用来更好地进行表的管理 在hdfs 的目录位置大致如下: [root@iZbp12vtv76y9q3d633bh6Z /]# hadoop fs -ls ...

  2. Lucene索引维护(添加、修改、删除)

    1. Field域属性分类 添加文档的时候,我们文档当中包含多个域,那么域的类型是我们自定义的,上个案例使用的TextField域,那么这个域他会自动分词,然后存储            我们要根据数 ...

  3. Oracle 唯一 索引 约束 创建 删除

    http://www.blogjava.net/lukangping/articles/340683.html/*给创建bitmap index分配的内存空间参数,以加速建索引*/ show para ...

  4. Mysql的基础使用之SQL原生语句的使用:表的 创建 删除 修改 (一)

    上一篇主要讲的是关于Mysql的分支MariaDB在Linux下的安装 顺利安装完成的小伙伴,就可以接着来试试SQL的魅力了 红色为命令 蓝色为自定义名 查看数据库 MariaDB [(none)]& ...

  5. [No000005]C#注册表操作,创建,删除,修改,判断节点是否存在

    //用.NET下托管语言C#操作注册表,主要内容包括:注册表项的创建,打开与删除.键值的创建(设置值.修改),读取和删除.判断注册表项是否存在.判断键值是否存在. //准备工作: //1:要操作注册表 ...

  6. sql创建删除修改表的基本操作

    1 建立表格 在建立好数据库以后,就可以根据储存资料的需求,使用SQL叙述建立所有需要的表格(table).建立表格的设定非常多,以建立"world.city"表格来说,它的叙述会 ...

  7. lucene索引的创建与搜索

    package com.cs.multi; import java.io.File;import java.io.IOException; import org.apache.lucene.analy ...

  8. c# 注册表操作,创建,删除,修改,判断节点是否存在

    用.NET下托管语言C#操作注册表,主要内容包括:注册表项的创建,打开与删除.键值的创建(设置值.修改),读取和 删除.判断注册表项是否存在.判断键值是否存在. 准备工作: 1:要操作注册表,我们必须 ...

  9. *lucene索引_的删除和更新

    [删除] [恢复删除] [强制删除] [优化和合并] [更新索引] 附: 代码: IndexUtil.java: package cn.hk.index; import java.io.File; i ...

随机推荐

  1. Fiddler是最强大最好用的Web调试工具

    Fiddler是最强大最好用的Web调试工具之一,它能记录所有客户端和服务器的http和https请求,允许你监视,设置断点,甚至修改输入输出数据. 使用Fiddler无论对开发还是测试来说,都有很大 ...

  2. 2013 NEERC

    2013 NEERC Problem A. ASCII Puzzle 题目描述:完成一个拼图. solution 暴搜,但好像挺难打的,但听说因为题目限制比较多,其实很多奇怪的情况都不存在. Prob ...

  3. set IDENTITY_INSERT on 和 off 的设置

    qlserver 批量插入记录时,对有标识列的字段要设置 set IDENTITY_INSERT 表名 on,然后再执行插入记录操作;插入完毕后恢复为 off 设置 格式:  set IDENTITY ...

  4. 20155309南皓芯2016-2017 2《Java程序设计》第一周学习总结

    关于java学习笔记的思考问题 第一章:JDK与JRE,JVM之间有没有必然的联系 第二章:可执行文件夹找到相关链接库 第三章:for与while循环的用法与比较,break与continue跳出的注 ...

  5. PHP性能调优,PHP慢日志---PHP脚本执行效率性能检测之WebGrind的使用

    如何一睹webgrind这个神奇的php性能检测工具神奇呢? 废话不多说首先webgrind这个性能检测是需要xdebug来配合,因为webgrind 进行性能检测分析就是通过xdebug生成的日志文 ...

  6. HTML标签列表总览

    超文本标记语言(简称:HTML)标记标签通常被称为HTML标签,HTML标签是HTML语言中最基本的单位,HTML标签是HTML(标准通用标记语言下的一个应用)最重要的组成部分.HTML标签的大小写无 ...

  7. django使用RestFramework的Token认证

    今天实现的想法有点不正规: Django Rest framework的框架的认证,API都运行良好. 现在是要自己写一个function来实现用户的功能. 而不是用Rest 框架里的APIVIEW这 ...

  8. scrapy中对于item的把控

    其实很简单,就是想要存储的位置发生改变.直接看例子,然后触类旁通. 以大众点评 评论的内容为例 ,位置:http://www.dianping.com/shop/77489519/review_mor ...

  9. [水煮 ASP.NET Web API2 方法论](1-4)从 MVC Controller 链接到 API Controller 以及反向链接

    问题 想创建一个从 ASP.NET MVC controller 到 ASP.NET Web API controller 的直接链接,或者反向链接. 解决方案 可以使用 System.Web.Htt ...

  10. MongoDB入门教程二[MongoDB Shell 简介与使用]

    MongoDB Shell 是MongoDB自带的JavaScript Shell,随MongoDB一同发布,它是MonoDB客户端工具,可以在Shell中使用命令与MongoDB实例交互,对数据库的 ...