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 ...
随机推荐
- vue+elementui 新增和编辑如何实现共用一个弹框
//html代码: //按钮 <el-button type="primary" size="medium" @click="addEquipm ...
- http请求与传参
这并不算是文章,暂时只做粗略地记录,以免忘记,因此会显得杂乱无章,随便抓了几个包和对postman截图,日后有空再完善 1.get方式 只有一种方式,那就是在url后面跟参数 2.post方式 1)表 ...
- C# TimeSpan获取 年月
public static string GetYearMonthDayString(this DateTime expires) { try { var now = DateTime.Now; Ti ...
- 打造 Laravel 优美架构 谈可维护性与弹性设计
转载:https://juejin.im/post/5be4475c518825170559c044
- CVE-2012-1876Microsoft Internet Explorer Col元素远程代码执行漏洞分析
Microsoft Internet Explorer是微软Windows操作系统中默认捆绑的WEB浏览器. Microsoft Internet Explorer 6至9版本中存在漏 ...
- MIT6.006Lec02:DocumentDistance
MIT6.006是算法导论,Lec02讲的是Document Distance(文档距离),比如比较两个文档相似度或者搜索引擎中都会用到. 计算步骤为: 1.将每个文档分离为单词 2.统计词频 3.计 ...
- html meta标签使用总结(转)
之前学习前端中,对meta标签的了解仅仅只是这一句. <meta charset="UTF-8"> 但是打开任意的网站,其head标签内都有一列的meta标签.比如我博 ...
- Java学习(异常类)
一.什么是异常: 异常就是在运行时产生的问题.通常用Exception描述. 在java中,把异常封装成了一个类,当出现问题时,就会创建异常类对象并抛出异常相关的信息(如详细信息,名称以及异常所处的位 ...
- OpenCV持久化(一)
在OpenCV中,采用FileStorage类进行数据持久化,可以采用XML或YAML格式存储数据. 将数据写入XML或YAML文件,可采用以下步骤: 1.创建FileStorage对象.可以调用构造 ...
- CF455D. Serega and Fun
D. Serega and Fun time limit per test 4 seconds memory limit per test 256 megabytes input standard i ...