package com.hope.lucene;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StoredField;
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.junit.Before;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.File;

/**
* @author newcityman
* @date 2020/1/15 - 17:22
*/
public class IndexManager {
private IndexWriter indexWriter;

@Before
public void init() throws Exception {
//创建一个IndexWriter对象,需要使用IKAnalyzer作为分词器
indexWriter = new IndexWriter(FSDirectory.open(new File("G:\\workspace_idea3\\lucene\\temp\\index").toPath()),
new IndexWriterConfig(new IKAnalyzer()));
}

/**
* 添加索引
* @throws Exception
*/
@Test
public void addDocument() throws Exception {

//创建一个Document对象
Document document = new Document();
//向Document对象中添加Field域
document.add(new TextField("name", "新添加的文件", Field.Store.YES));
document.add(new TextField("content", "新添加的文件内容", Field.Store.NO));
document.add(new StoredField("path", "G:\\workspace_idea3\\lucene\\temp\\index"));
//把文档写入索引库
indexWriter.addDocument(document);
//关闭索引库
indexWriter.close();
}

/**
* 删除索引
* @throws Exception
*/
@Test
public void deleteAllDocument() throws Exception {
//删除所有文档
indexWriter.deleteAll();
//关闭索引库
indexWriter.close();
}

/**
* 根据条件删除索引
*/
@Test
public void deleteDocumentByQuery() throws Exception{
indexWriter.deleteDocuments(new Term("content","apache"));
indexWriter.close();
}

/**
* 修改索引
*/
@Test
public void updateDocument() throws Exception{
Document document = new Document();
document.add(new TextField("name","更新后的内容1", Field.Store.YES));
document.add(new TextField("name2","更新后的内容2", Field.Store.YES));
document.add(new TextField("name3","更新后的内容3", Field.Store.YES));
indexWriter.updateDocument(new Term("name","spring"),document);
indexWriter.close();
}
}

lucene索引的增、删、改的更多相关文章

  1. iOS sqlite3 的基本使用(增 删 改 查)

    iOS sqlite3 的基本使用(增 删 改 查) 这篇博客不会讲述太多sql语言,目的重在实现sqlite3的一些基本操作. 例:增 删 改 查 如果想了解更多的sql语言可以利用强大的互联网. ...

  2. ADO.NET 增 删 改 查

    ADO.NET:(数据访问技术)就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中 也可以将数据库中的数据提取到内存中供程序调用 ADO.NET所有数据访 ...

  3. 第18课-数据库开发及ado.net 连接数据库.增.删.改向表中插入数据并且返回自动编号.SQLDataReade读取数据

    第18课-数据库开发及ado.net 连接数据库.增.删.改向表中插入数据并且返回自动编号.SQLDataReade读取数据 ADO.NET 为什么要学习? 我们要搭建一个平台(Web/Winform ...

  4. C# ADO.NET (sql语句连接方式)(增,删,改)

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  5. 好用的SQL TVP~~独家赠送[增-删-改-查]的例子

    以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化.  本系列主要是针对T-SQL的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础] ...

  6. iOS FMDB的使用(增,删,改,查,sqlite存取图片)

    iOS FMDB的使用(增,删,改,查,sqlite存取图片) 在上一篇博客我对sqlite的基本使用进行了详细介绍... 但是在实际开发中原生使用的频率是很少的... 这篇博客我将会较全面的介绍FM ...

  7. MVC EF 增 删 改 查

    using System;using System.Collections.Generic;using System.Linq;using System.Web;//using System.Data ...

  8. django ajax增 删 改 查

    具于django ajax实现增 删 改 查功能 代码示例: 代码: urls.py from django.conf.urls import url from django.contrib impo ...

  9. StringBuilder修改字符串内容,增,删,改,插

    package seday01;/** * 字符串不变对象特性只针对字符串重用,并没有考虑修改操作的性能.因此String不适合频繁修改内容. * 若有频繁修改操作,使用StringBuilder来完 ...

  10. python基础中的四大天王-增-删-改-查

    列表-list-[] 输入内存储存容器 发生改变通常直接变化,让我们看看下面列子 增---默认在最后添加 #append()--括号中可以是数字,可以是字符串,可以是元祖,可以是集合,可以是字典 #l ...

随机推荐

  1. Vue.js教程 2.体验Vue

    Vue.js教程 2.体验Vue <!DOCTYPE html> <html lang="en"> <head> <meta charse ...

  2. GoLang设计模式14 - 状态模式

    状态模式,顾名思义,是一种基于有限状态机制的设计模式.在这种设计模式中,行为是由相应的状态来决定的.接下来我们会用一个售卖机的例子来说明下状态模式.为了便于说明,我们把场景简化一下,假设有一台售卖机只 ...

  3. SVD专题1 算子的奇异值分解——矩阵形式的推导

    目录 SVD专题1 算子的奇异值分解--矩阵形式的推导 前言 Preface 几点说明 预备知识 Prerequisite 1.1 极分解 Polar Decomposition 1.2 等距同构 U ...

  4. c++学习笔记目录

    chapter name menu 一 从c到c++ 1.引用2.const关键词的用法3.动态内存分配4.内联函数5.函数重载6.函数的缺省参数7.结构化程序设计的不足8.面向对象的程序设计 二 类 ...

  5. 【Mysql】表锁 行锁 记录锁 间隙锁

    Mysql中的锁 基于锁的属性分类:共享锁.排他锁. 基于锁的状态分类:意向共享锁.意向排它锁 根据锁的粒度分类:全局锁.页锁.表级锁.行锁(记录锁.间隙锁.和临键锁),实际上的锁就这些,上面两种分类 ...

  6. Django笔记&教程 7-1 基于类的视图(Class-based views)介绍

    Django 自学笔记兼学习教程第7章第1节--基于类的视图(Class-based views)介绍 点击查看教程总目录 1 介绍 Class-based views (CBVs) are view ...

  7. xpath的chrome插件安装,xpath基本语法

    xpath插件安装: 注意:提前安装xpath插件 (1)打开chrome浏览器 (2)点击右上角小圆点 (3)更多工具 (4)扩展程序 (5)拖拽xpath插件到扩展程序中 (6)如果crx文件失效 ...

  8. WPF嵌入Unity3D之后,unity3D程序的键盘和鼠标事件无法触发(3D程序的焦点无法激活)的解决方案

    目前最通用的客户端调用3D的方式,就是WPF程序通过Process启动Unity3D的exe进程,直接上代码: //开启3D进程 internal void Create3DProcess(strin ...

  9. PAT A1103—DFS

     Integer Factorization The K−P factorization of a positive integer N is to write N as the sum of the ...

  10. [luogu4331]数字序列

    令$a'_{i}=a_{i}+n-i$.$b'_{i}=b_{i}+n-i$,代价仍然是$\sum_{i=1}^{n}|a'_{i}-b'_{i}|$,但条件变为了$b'_{i}\le b'_{i+1 ...