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. Android开发环境搭建篇详尽的教程实例汇

    原文链接:http://android.eoe.cn/topic/android_sdk 一.android开发环境搭建图文教程整理篇: 1.Android开发环境搭建全程演示(jdk+eclip+a ...

  2. JVM调优——之CMS GC日志分析

    最近在学习JVM和GC调优,今天总结下CMS的一些特点和要点,让我们先简单的看下整个堆年轻代和年老代的垃圾收集器组合(以下配合java8完美支持,其他版本可能稍有不同),其中标红线的则是我们今天要着重 ...

  3. 从头开始学习vue-router

    一.前言 要学习vue-router就要先知道这里的路由是什么?为什么我们不能像原来一样直接用标签编写链接哪?vue-router如何使用?常见路由操作有哪些?等等这些问题,就是本篇要探讨的主要问题. ...

  4. tengine 的优化

    查服务器CPU的核数 : [root@c01 conf]# grep processor /proc/cpuinfo |wc -l 4 [root@c01 conf]# grep -c process ...

  5. rsync 实现文件同步 (重要数据通过rsyncr把数据同步到不同的两台服务器上,这样可以防止服务器的硬盘故障导致数据丢失) 客户端同步时如果要排某个目录

    rsync是unix系统下的数据镜像 备份工具,一般linux系统都 自带: # rpm -qa|grep rsync rsync-3.0.9-17.el7.x86_64 服务器端:10.100.0. ...

  6. .NET MVC+ EF+LINQ 多表联查VIEW显示列表

    1.VIEW 页面显示代码 <link href="~/Content/bootstrap.css" rel="stylesheet" /> < ...

  7. 每日英语:America The Vulgar

    'What's celebrity sex, Dad?' It was my 7-year-old son, who had been looking over my shoulder at my c ...

  8. 每日英语:Redfin Real-Estate Firm Gets Cold Shoulder in Silicon Valley

    "I used to think I was this made man," says entrepreneur Glenn Kelman. "That's what t ...

  9. 【socket】小项目-智能点餐系统

    系统说明 前段时间做的一个智能点餐系统,从0开始,用时3天,其中调bug(内存拷贝)调了一天,囧,现记一些架构文档 这个系统涉及到的知识点还是挺多的 典型的c/s模式,socket通信 多线程操作 数 ...

  10. Spring Boot项目配置RabbitMQ集群

    //具体参看了配置的源码 org.springframework.boot.autoconfigure.amqp.RabbitProperties //RabbitMQ单机 spring:   rab ...