Lucene增删改查
IndexManager.java
package com.witwicky.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.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.LongField;
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.queryparser.classic.QueryParser;
import org.apache.lucene.search.Query;
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 Administrator
*
*/
public class IndexManager { /**
* 创建索引
*
* @throws Exception
*/
@Test
public void IndexCreate() throws Exception { String dataPath = "D:\\Lucene\\searchsource";
String indexPath = "D:\\Lucene\\dic"; // 文档集合
List<Document> listDocument = new ArrayList<>(); // 读取文件,创建Document列表
File file = new File(dataPath);
for (File f : file.listFiles()) {
String fileName = f.getName();
String fileContent = FileUtils.readFileToString(f);
long fileSize = FileUtils.sizeOf(f); // 创建文档
Document doc = new Document();
doc.add(new TextField("fileName", fileName, Store.YES));
doc.add(new TextField("fileContent", fileContent, Store.YES));
doc.add(new LongField("fileSize", fileSize, Store.YES)); // 放入文档列表
listDocument.add(doc);
} // 创建分词器
// Analyzer analyzer = new StandardAnalyzer();
Analyzer analyzer = new IKAnalyzer(); // 创建写入目录
Directory directory = FSDirectory.open(new File(indexPath));
// 创建写入索引对象的配置对象
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_4_10_3, analyzer);
// 创建写索引对象
IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig); // 将文档添加入写入索引对象的流中
for (Document document : listDocument) {
indexWriter.addDocument(document);
} // 提交
indexWriter.commit(); // 关闭流
indexWriter.close();
} /**
* 删除索引
*/
@Test
public void delIndex() throws Exception {
Analyzer analyzer = new IKAnalyzer();
// Analyzer analyzer = new StandardAnalyzer(); Directory directory = FSDirectory.open(new File("D:\\Lucene\\dic"));
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_4_10_3, analyzer);
IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig); // 删除所有
indexWriter.deleteAll(); // 根据域删除
// 1.
// QueryParser queryParser = new QueryParser("fileName", analyzer);
// Query query = queryParser.parse("fileName:apache");
// indexWriter.deleteDocuments(term); // 2.
// Term term = new Term("fileName", "java");
// indexWriter.deleteDocuments(term); indexWriter.commit();
indexWriter.close();
} /**
* 更新索引
*
* @throws Exception
*/
@Test
public void updateIndex() throws Exception {
Analyzer analyzer = new IKAnalyzer();
// Analyzer analyzer = new StandardAnalyzer(); Directory directory = FSDirectory.open(new File("D:\\Lucene\\dic"));
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_4_10_3, analyzer);
IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig); Term term = new Term("fileName", "adsf");
Document doc = new Document();
doc.add(new TextField("fileName", "adsf", Store.YES));
doc.add(new TextField("fileContent", "vvvvvvvvvvvvvvv", Store.YES));
doc.add(new LongField("fileSize", 200L, Store.YES)); indexWriter.updateDocument(term, doc); indexWriter.commit();
indexWriter.close();
}
}
IndexSearch.java
package com.witwicky.lucene; import java.io.File; import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer; public class IndexSearch { /**
* 索引搜索
*/
@Test
public void indexSearch() throws Exception {
String indexPath = "D:\\Lucene\\dic"; // 分词器
// Analyzer analyzer = new StandardAnalyzer();
Analyzer analyzer = new IKAnalyzer(); // 查询解析对象
QueryParser queryParser = new QueryParser("fileName", analyzer);
Query query = queryParser.parse("fileName:apache"); Directory directory = FSDirectory.open(new File(indexPath));
IndexReader indexReader = DirectoryReader.open(directory); IndexSearcher indexSearcher = new IndexSearcher(indexReader); TopDocs topDocs = indexSearcher.search(query, 10); System.out.println("总记录数:" + topDocs.totalHits); ScoreDoc[] scoreDocs = topDocs.scoreDocs; for (ScoreDoc scoreDoc : scoreDocs) {
Document doc = indexSearcher.doc(scoreDoc.doc); System.out.println("名称:" + doc.get("fileName"));
// System.out.println("内容:" + doc.get("fileContent"));
System.out.println("大小:" + doc.get("fileSize"));
} indexReader.close();
}
}
Lucene增删改查的更多相关文章
- 【ES】ElasticSearch初体验之使用Java进行最基本的增删改查~
好久没写博文了, 最近项目中使用到了ElaticSearch相关的一些内容, 刚好自己也来做个总结. 现在自己也只能算得上入门, 总结下自己在工作中使用Java操作ES的一些小经验吧. 本文总共分为三 ...
- Elasticsearch之文档的增删改查以及ik分词器
文档的增删改查 增加文档 使用elasticsearch-head查看 修改文档 使用elasticsearch-head查看 删除文档 使用elasticsearch-head查看 查看文档的三种方 ...
- Es图形化软件使用之ElasticSearch-head、Kibana,Elasticsearch之-倒排索引操作、映射管理、文档增删改查
今日内容概要 ElasticSearch之-ElasticSearch-head ElasticSearch之-安装Kibana Elasticsearch之-倒排索引 Elasticsearch之- ...
- Dapper逆天入门~强类型,动态类型,多映射,多返回值,增删改查+存储过程+事物案例演示
Dapper的牛逼就不扯蛋了,答应群友做个入门Demo的,现有园友需要,那么公开分享一下: 完整Demo:http://pan.baidu.com/s/1i3TcEzj 注 意 事 项:http:// ...
- ASP.NET从零开始学习EF的增删改查
ASP.NET从零开始学习EF的增删改查 最近辞职了,但是离真正的离职还有一段时间,趁着这段空档期,总想着写些东西,想来想去,也不是很明确到底想写个啥,但是闲着也是够 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(9)-MVC与EasyUI结合增删改查
系列目录 文章于2016-12-17日重写 在第八讲中,我们已经做到了怎么样分页.这一讲主要讲增删改查.第六讲的代码已经给出,里面包含了增删改,大家可以下载下来看下. 这讲主要是,制作漂亮的工具栏,虽 ...
- 通过Java代码实现对数据库的数据进行操作:增删改查
在写代码之前,依然是引用mysql数据库的jar包文件:右键项目-构建路径-设置构建路径-库-添加外部JAR 在数据库中我们已经建立好一个表xs :分别有xuehao xingming xue ...
- Hibernate全套增删改查+分页
1.创建一个web工程 2.导入jar包 3.创建Student表 4.创建实体类 package com.entity; public class Student { private Integer ...
- 使用 Json.Net 对Json文本进行 增删改查
JSON 已经成为当前主流交互格式, 如何在C#中使用 Json.Net 对Json文本进行 增删改查呢?见如下代码 #region Create (从零创建) public static strin ...
随机推荐
- 小程序踩过的一个小坑---解析二维码decodeURIComponent() url解码
因为我们需要用户扫码进入小程序,每一个货柜都有一个对应的二维码,当然每个二维码里的信息也不一样.用户扫码进入小程序之后,二维码的信息会以参数q带进去,而我们只能在onLoad事件中拿到这个参数, 但是 ...
- 给Elasticsearch 5.2.2 设置用户权限 how to setting security for elasticsearch on windows
1. download the plugin of elasticsearch: 下载 readonlyrest-1.14.0_es5.2.2.zip 2. install readonlyrest ...
- 案例:用Redis来存储关注关系
Redis提供了丰富的数据类型,比起关系型数据库或者简单的Key-Value存储(比如Memcached)来,Redis的数据模型与实际应用的数据模型更相近.比如下面说到的好友关系的存储,原作者使用了 ...
- Shell脚本大量示例
Shell基础之控制流结构 一.控制结构 几乎所有的脚本里都有某种流控制结构,很少有例外.流控制是什么?假定有一个脚本,包含下列几个命令: #!/bin/sh # make a directory ...
- 【线程】linux之thread错误解决方案
1.错误现象: undefined reference to 'pthread_create' undefined reference to 'pthread_join' 2.问题原因: pt ...
- How to disable SSL certificate checking with Spring RestTemplate?(使用resttemplate访问https时禁用证书检查)
How to disable SSL certificate checking with Spring RestTemplate?(使用resttemplate访问https时禁用证书检查) **** ...
- error LNK2005: _DllMain@12 已经在 MSVCRTD.lib(dllmain.obj) 中定义
备注:我上次遇到这个问题是Win32 DLL项目中无意中include了afxwin.h,这个是MFC的头文件,把这个include删掉就解决了 ================ 转自:http:// ...
- Zabbix监控JVM内存
上篇最后提到了jstat,jstat可以查看统计JVM内存信息,那么结合Zabbix,就可以监控多实例的JVM内存了. 1.下面两个脚本部署在被监控主机: vm.py 用于JVM实例PID查找,ps命 ...
- Mysql 多表查询详解
Reference: https://blog.csdn.net/jintao_ma/article/details/51260458 一.前言 二.示例 三.注意事项 一.前言 上篇讲到Mysql ...
- 将目录下面所有的 .cs 文件合并到一个 code.cs 文件中,写著作权复制代码时的必备良药
将目录下面所有的 .cs 文件合并到一个 code.cs 文件中,写著作权复制代码时的必备良药 @echo off echo 将该目录下所有.cs文件的内容合并到一个 code.cs 文件中! pau ...