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 ...
随机推荐
- go语言 documentation
Documentation文档 The Go programming language is an open source project to make programmers more pro ...
- sql server中扩展存储过程
--列出服务器上安装的所有OLEDB提供的程序 execute master..xp_enum_oledb_providers --得到硬盘文件信息 --参数说明:目录名,目录深度,是否显示文件 (少 ...
- Kafka ACL使用实战(单机版)
一.简介 自0.9.0.0.版本引入Security之后,Kafka一直在完善security的功能.当前Kafka security主要包含3大功能:认证(authentication).信道加密( ...
- Ubuntu 搭建docker registry 私有仓库
一.为什么要搭建 docker 私有仓库 原因有几个: 项目需要,不希望将项目放到 docker hub 上. 环境需求,考虑网络.效率的问题,希望在私有服务器上建立自用的仓库,提高便利性和访问速度. ...
- 在oracle中varchar和varchar2有什么区别?
1.varchar2把所有字符都占两字节处理(一般情况下),varchar只对汉字和全角等字符占两字节,数字,英文字符等都是一个字节:2.VARCHAR2把空串等同于null处理,而varchar仍按 ...
- oracle centos 静默安装
http://blog.csdn.net/tongzidane/article/details/43852705 静默安装Oracle 11G过程中提示:Exception in thread &qu ...
- mongo 常用操作命令
表的帮助,格式:db.表名.help() db.test.find({id:10}) 返回test数据集ID=10的数据集 db.test.find({id:10}).count ...
- 微信接口问题(The underlying connection was closed: An unexpected error occurred on a send)
突然在调用微信接口是报:The underlying connection was closed: An unexpected error occurred on a send错误,跟踪了半天,是因为 ...
- LoadRunner常用知识点-----LoadRunner日志输出
在Windows环境下,日志文件output.txt保存在脚本目录中:在UNIX环境下,保存在标准输出中. [Vuser]——[Run Time Settings]——[General]——[Log] ...
- 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:fhcq-oa' did not find a matching property.
当你在使用Eclipse运行web项目时,你可能会看到控制台出现: 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Settin ...