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增删改查的更多相关文章

  1. 【ES】ElasticSearch初体验之使用Java进行最基本的增删改查~

    好久没写博文了, 最近项目中使用到了ElaticSearch相关的一些内容, 刚好自己也来做个总结. 现在自己也只能算得上入门, 总结下自己在工作中使用Java操作ES的一些小经验吧. 本文总共分为三 ...

  2. Elasticsearch之文档的增删改查以及ik分词器

    文档的增删改查 增加文档 使用elasticsearch-head查看 修改文档 使用elasticsearch-head查看 删除文档 使用elasticsearch-head查看 查看文档的三种方 ...

  3. Es图形化软件使用之ElasticSearch-head、Kibana,Elasticsearch之-倒排索引操作、映射管理、文档增删改查

    今日内容概要 ElasticSearch之-ElasticSearch-head ElasticSearch之-安装Kibana Elasticsearch之-倒排索引 Elasticsearch之- ...

  4. Dapper逆天入门~强类型,动态类型,多映射,多返回值,增删改查+存储过程+事物案例演示

    Dapper的牛逼就不扯蛋了,答应群友做个入门Demo的,现有园友需要,那么公开分享一下: 完整Demo:http://pan.baidu.com/s/1i3TcEzj 注 意 事 项:http:// ...

  5. ASP.NET从零开始学习EF的增删改查

           ASP.NET从零开始学习EF的增删改查           最近辞职了,但是离真正的离职还有一段时间,趁着这段空档期,总想着写些东西,想来想去,也不是很明确到底想写个啥,但是闲着也是够 ...

  6. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(9)-MVC与EasyUI结合增删改查

    系列目录 文章于2016-12-17日重写 在第八讲中,我们已经做到了怎么样分页.这一讲主要讲增删改查.第六讲的代码已经给出,里面包含了增删改,大家可以下载下来看下. 这讲主要是,制作漂亮的工具栏,虽 ...

  7. 通过Java代码实现对数据库的数据进行操作:增删改查

    在写代码之前,依然是引用mysql数据库的jar包文件:右键项目-构建路径-设置构建路径-库-添加外部JAR 在数据库中我们已经建立好一个表xs :分别有xuehao  xingming    xue ...

  8. Hibernate全套增删改查+分页

    1.创建一个web工程 2.导入jar包 3.创建Student表 4.创建实体类 package com.entity; public class Student { private Integer ...

  9. 使用 Json.Net 对Json文本进行 增删改查

    JSON 已经成为当前主流交互格式, 如何在C#中使用 Json.Net 对Json文本进行 增删改查呢?见如下代码 #region Create (从零创建) public static strin ...

随机推荐

  1. django -- 用包来组织数据库模型

    默认情况下一个django app的所有模型都保存在一个叫models.py的文件中.这样事实是不方便管理的: 通过包来组织模型是比较方便的. 一.第一步:删除models.py: rm -rf mo ...

  2. Nginx(四):压缩功能详解

    gzip (GNU-ZIP) 是一种压缩技术.经过 gzip 压缩后页面大小可以变为原来的 30%甚至更小. 这样,用户浏览页面的时候速度会快得多.  gzip 的压缩页面需要浏览器和服务器双方都支持 ...

  3. tomcat重启步骤

    tomcat重启步骤1.切换到tomcat目录cd /home/tomcat-home/tomcat-erp/bin2.关闭tomcatsh shutdown.sh3.启动tomcatsh start ...

  4. 在c/c++中浮点数是否为0的判断

    在c/c++中,因为浮点数在内存中的表示是不精确的,会有很微小的误差,所以判断是否为0,就看它的绝对值是不是<=eps. eps可以看成是epsilon的缩写,可以用来表示一个无穷小的量,通常取 ...

  5. C++头文件<bits/stdc++.h>

    在刷题时,总发现有的只写一行头文件 #include <bits/stdc++.h> . 查阅资料后,才知道原来:#include<bits/stdc++.h>包含了目前c++ ...

  6. 基于Zynq平台的EtherCAT主站方案实现

    作者:陈秋苑 谢晓锋 陈海焕 广州虹科电子科技有限公司 摘 要:EtherCAT 是开放的实时以太网通讯协议,由德国倍福自动化有限公司研发.EtherCAT 具有高性能.低成本.容易使用等特点,目前在 ...

  7. 期权、RSU的区别与行权事宜

    科普一下常见的股票.期权.股票增值权.虚拟股票等常见的激励方式,以及在兑换或行权的一些相关问题.股票(Stock): 股票市场也称权益市场,是专门对股票进行公开交易的市场,包括股票的发行和转让,分为一 ...

  8. (转)Unity3d使用心得(2):Unity3d 动态下载动画资源——AnimationClip 的使用 - 斯玛特琦

    引言: 在使用 Unity3d 开发微端.或者网页游戏的时候常常须要将资源打包成 AssetBundle ,然后通过 www 的方式动态的下载资源.今天要分享的是我再动态下载 Animation 骨骼 ...

  9. formidable处理多文件上传

    首先,在html页面中,表单上传文件的控件需要加上multiple选项,或者multiple="multiple". 然后,在nodejs程序中处理post数据的路路由中使用for ...

  10. python使用requests时报错requests.exceptions.SSLError: HTTPSConnectionPool

    报错信息 Traceback (most recent call last): File "<stdin>", line 1, in <module> Fi ...