Lucene——索引的创建、删除、修改
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——索引的创建、删除、修改的更多相关文章
- hive 学习系列二(数据库的创建删除修改) 拿走,不谢。
database 相当于一个目录或者命名空间,用来更好地进行表的管理 在hdfs 的目录位置大致如下: [root@iZbp12vtv76y9q3d633bh6Z /]# hadoop fs -ls ...
- Lucene索引维护(添加、修改、删除)
1. Field域属性分类 添加文档的时候,我们文档当中包含多个域,那么域的类型是我们自定义的,上个案例使用的TextField域,那么这个域他会自动分词,然后存储 我们要根据数 ...
- Oracle 唯一 索引 约束 创建 删除
http://www.blogjava.net/lukangping/articles/340683.html/*给创建bitmap index分配的内存空间参数,以加速建索引*/ show para ...
- Mysql的基础使用之SQL原生语句的使用:表的 创建 删除 修改 (一)
上一篇主要讲的是关于Mysql的分支MariaDB在Linux下的安装 顺利安装完成的小伙伴,就可以接着来试试SQL的魅力了 红色为命令 蓝色为自定义名 查看数据库 MariaDB [(none)]& ...
- [No000005]C#注册表操作,创建,删除,修改,判断节点是否存在
//用.NET下托管语言C#操作注册表,主要内容包括:注册表项的创建,打开与删除.键值的创建(设置值.修改),读取和删除.判断注册表项是否存在.判断键值是否存在. //准备工作: //1:要操作注册表 ...
- sql创建删除修改表的基本操作
1 建立表格 在建立好数据库以后,就可以根据储存资料的需求,使用SQL叙述建立所有需要的表格(table).建立表格的设定非常多,以建立"world.city"表格来说,它的叙述会 ...
- lucene索引的创建与搜索
package com.cs.multi; import java.io.File;import java.io.IOException; import org.apache.lucene.analy ...
- c# 注册表操作,创建,删除,修改,判断节点是否存在
用.NET下托管语言C#操作注册表,主要内容包括:注册表项的创建,打开与删除.键值的创建(设置值.修改),读取和 删除.判断注册表项是否存在.判断键值是否存在. 准备工作: 1:要操作注册表,我们必须 ...
- *lucene索引_的删除和更新
[删除] [恢复删除] [强制删除] [优化和合并] [更新索引] 附: 代码: IndexUtil.java: package cn.hk.index; import java.io.File; i ...
随机推荐
- ios 个人开发者账户 给其他团队用坑爹的教程
最新版本的 ios 支持 3个开发者证书 和 3个发布者证书 ,如果是多余3台电脑设备要真机调试,就比较麻烦 (手机支持100个设备) 解决方案就是: 在别人的电脑上要成功安装,须具备两个文件: ...
- 15 Defer, Panic, and Recover
Defer, Panic, and Recover 4 August 2010 Go has the usual mechanisms for control flow: if, for, switc ...
- SpringCloud Config Bus webhook 只能刷新config server 不能刷新config client
在 https://github.com/spring-cloud/spring-cloud-bus/issues/124 中有提到 版本 SpringCloud:Greenwich.RC1 原因 由 ...
- Hive(三)Hive元数据信息对应MySQL数据库表
概述 Hive 的元数据信息通常存储在关系型数据库中,常用MySQL数据库作为元数据库管理.上一篇hive的安装也是将元数据信息存放在MySQL数据库中. Hive的元数据信息在MySQL数据中有57 ...
- ASP.NET WebAPI 04 Model绑定
在前面的几篇文章中我们都是采用在URI中元数据类型进行传参,实际上ASP.NET Web API也提供了对URI进行复杂参数的绑定方式--Model绑定.这里的Model可以简单的理解为目标Ancti ...
- Vue学习笔记进阶篇——Render函数
基础 Vue 推荐在绝大多数情况下使用 template 来创建你的 HTML.然而在一些场景中,你真的需要 JavaScript 的完全编程的能力,这就是 render 函数,它比 template ...
- 《Android源码设计模式》--享元模式
No1: 享元模式是对象池的一种实现.享元模式用来尽可能减少内存使用量,它适合用于可能存在大量重复对象的场景,来缓存可共享的对象,达到对象共享.避免创建过多对象的效果,这样一来就可以提升性能.避免内存 ...
- 【记录】HTTP协议状态码含义
状态码200-299之间的状态码表示成功300-399之间的代码表示资源已经被移走400-499之间的代码表示客户端的请求出错500-599之间的代码表示服务器出错了
- [python 源码]整数对象的创建和维护
刚开始学python时候,发现一个很迷惑的现象,一直到看了源码后才知道了: >>> a=6 >>> b=6 >>> a is b True 想用同 ...
- [leetcode tree]101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...