1. 索引库的操作

保持数据库与索引库的同步

说明:在一个系统中,如果索引功能存在,那么数据库和索引库应该是同时存在的。这个时候需要保证索引库的数据和数据库中的数据保持一致性。可以在对数据库进行增、删、改操作的同时对索引库也进行相应的操作。这样就可以保证数据库与索引库的一致性。

工具类DocumentUtils

在对索引库进行操作时,增、删、改过程要把一个JavaBean封装成Document,而查询的过程是要把一个Document转化成JavaBean。在进行维护的工作中,要反复进行这样的操作,所以我们有必要建立一个工具类来重用代码。

public class DocumentUtils {
	/**
	 * 从Article转化为Document
	 * @param article
	 * @return
	 */
	public static Document article2Document(Article article){
		Document document = new Document();
		Field idField = new Field("id",article.getId().toString(),Store.YES,Index.NOT_ANALYZED);
		Field titleField = new Field("title",article.getTitle(),Store.YES,Index.ANALYZED);
		Field contentField = new Field("content",article.getContent(),Store.YES,Index.ANALYZED);
		document.add(idField);
		document.add(titleField);
		document.add(contentField);
		return document;
	}
	/**
	 * 从Document转化为Article
	 * @param document
	 * @return
	 */
	public static Article document2Article(Document document){
		Article article = new Article();
		article.setId(Long.parseLong(document.get("id")));
		article.setTitle(document.get("title"));
		article.setContent(document.get("content"));
		return article;
	}
}

什么情况下使用Index.NOT_ANALYZED

当这个属性的值代表的是一个不可分割的整体,例如 ID

什么情况下使用Index.ANALYZED

当这个属性的值代表的是一个可分割的整体

LuceneUtils

LuceneUtils这个类把Directory和Analyzer进行了包装。因为在创建IndexWriter时,需要用到这两个类,而管理索引库的操作都要用到IndexWriter这个类,所以我们对Directory和Analyzer进行了包装

public class LuceneUtils {
	public static Directory directory = null;
	public static Analyzer analyzer = null;
	static {
		try {
			directory = FSDirectory.open(new File("./indexDir"));
			analyzer = new StandardAnalyzer(Version.LUCENE_30);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

管理索引库

 public class ArticleIndex {
	//增加
	@Test
	public void testCreateIndex() throws Exception{
		Article article = new  Article();
		article.setId(1L);
		article.setTitle("lucene可以做搜索引擎");
		article.setContent("baidu,google都是很好的搜索引擎");

		IndexWriter indexWriter = new IndexWriter(LuceneUtils.directory,LuceneUtils.analyzer,MaxFieldLength.LIMITED);
		indexWriter.addDocument(DocumentUtils.article2Document(article));
		indexWriter.close();
	}

	@Test
	public void testSearchIndex() throws Exception{
		IndexSearcher indexSearcher = new IndexSearcher(LuceneUtils.directory);
		QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_30, new String[]{"title","content"}, LuceneUtils.analyzer);
		Query query = queryParser.parse("baidu");
		TopDocs topDocs = indexSearcher.search(query, 2);
		ScoreDoc[] scoreDocs = topDocs.scoreDocs;
		List<Article> articleList = new ArrayList<Article>();
		for(ScoreDoc scoreDoc:scoreDocs){
			Document document =  indexSearcher.doc(scoreDoc.doc);
			Article article = DocumentUtils.document2Article(document);
			articleList.add(article);
		}

		for(Article article:articleList){
			System.out.println(article.getId());
			System.out.println(article.getTitle());
			System.out.println(article.getContent());
		}
	}

	/**
	 * 一般情况下索引库的删除用关键词
	 * @throws Exception
	 */
	@Test
	public void testDeleteIndex() throws Exception{
		IndexWriter indexWriter = new IndexWriter(LuceneUtils.directory,LuceneUtils.analyzer,MaxFieldLength.LIMITED);
		//indexWriter.deleteAll()删除所有的索引值
		/**
		 * term就为关键词对象
		 * ID的索引保存类型为Index.NOT_ANALYZED,直接写ID也可以删除。
		 * title如果为Index.NOT_ANALYZED,那么关键词就不行,要整个内容才可以删除。
		 */
		Term term = new Term("title", "lucene");
		indexWriter.deleteDocuments(term);
		indexWriter.close();
	}

	/**
	 * 修改
	 *    先删除后增加
	 * lucene的更新操作与数据库的更新操作是不一样的。
	 * 因为在更新的时候,有可能变换了关键字的位置,这样分词器对关键字还得重新查找,
	 * 而且还得在目录和内容中替换,这样做的效率比较低,所以lucene的更新操作是删除和增加两步骤来完成的。
	 */
	@Test
	public void testUpdateIndex() throws Exception{
		IndexWriter indexWriter = new IndexWriter(LuceneUtils.directory,LuceneUtils.analyzer,MaxFieldLength.LIMITED);
		Term term = new Term("title", "lucene");
		Article article = new  Article();
		article.setId(1L);
		article.setTitle("lucene可以做搜索引擎");
		article.setContent("修改后的内容");
		/**
		 * term是用删除的
		 * document是用于增加的
		 */
		indexWriter.updateDocument(term, DocumentUtils.article2Document(article));
		indexWriter.close();
	}
}

项目下载:http://pan.baidu.com/s/1eQekos6

lucene索引库的增删改查操作的更多相关文章

  1. django-数据库的增删改查操作

    django-数据库的增删改查操作 1.添加用户记录 def login(request): 增加用户记录 使用save()方法来增加记录 username = UserInfo(username=' ...

  2. Lucene 07 - 对Lucene的索引库进行增删改查

    目录 1 添加索引 2 删除索引 2.1 根据Term删除索引 2.2 删除全部索引(慎用) 3 更新索引 数据保存在关系型数据库中, 需要实现增.删.改.查操作; 索引保存在索引库中, 也需要实现增 ...

  3. 【OF框架】新建库表及对应实体,并实现简单的增删改查操作,封装操作标准WebApi

    准备 搭建好项目框架及数据库,了解框架规范. 1.数据库表和实体一一对应,表名实体名名字相同,用小写,下划线连接.字段名用驼峰命名法,首字母大写. 2.实体放在Entities目录下,继承Entity ...

  4. 【Python + Mysql】之用pymysql库连接Mysql数据库并进行增删改查操作

    用pip下载pymysql并引用 具体请参考文章: <Python之MySQL数据库增删改查操作> <python3.6 使用 pymysql 连接 Mysql 数据库及 简单的增删 ...

  5. MySQL数据库的权限问题操作及基本增删改查操作

    前面我们讲了mysql的基本内容,现在我们详细的了解一下mysql中的具体操作. what's the SQl SQL(Structured Query Language 即结构化查询语言) SQL语 ...

  6. (转)SQLite数据库增删改查操作

    原文:http://www.cnblogs.com/linjiqin/archive/2011/05/26/2059182.html SQLite数据库增删改查操作 一.使用嵌入式关系型SQLite数 ...

  7. 05_Elasticsearch 单模式下API的增删改查操作

    05_Elasticsearch 单模式下API的增删改查操作 安装marvel 插件: zjtest7-redis:/usr/local/elasticsearch-2.3.4# bin/plugi ...

  8. Elasticsearch 单模式下API的增删改查操作

    <pre name="code" class="html">Elasticsearch 单模式下API的增删改查操作 http://192.168. ...

  9. Android SQLite 数据库 增删改查操作

    Android SQLite 数据库 增删改查操作 转载▼ 一.使用嵌入式关系型SQLite数据库存储数据 在Android平台上,集成了一个嵌入式关系型数据库--SQLite,SQLite3支持NU ...

随机推荐

  1. 【阿里聚安全·安全周刊】互联网时代人类还有被遗忘的权利吗 | Android与中兴

    本周七个关键词:互联网时代丨中兴和Android丨安卓厂商和安全补丁丨移动支付安全丨泰国移动运营商泄密丨格式化硬盘的恶意程序丨代码签名滥用 -1-   [互联网] 互联网时代 人类还有被遗忘的权利吗 ...

  2. Discuz! 7.x 反射型xss

    引用:http://wooyun.jozxing.cc/static/bugs/wooyun-2014-084097.html 在/include/global.func.php 文件中 第1036- ...

  3. P3928 SAC E#1 - 一道简单题 Sequence2

    题目背景 小强和阿米巴是好朋友. 题目描述 小强喜欢数列.有一天,他心血来潮,写下了三个长度均为n的数列. 阿米巴也很喜欢数列.但是他只喜欢其中一种,波动数列. 阿米巴把他的喜好告诉了小强.小强便打算 ...

  4. ●poj 1474 Video Surveillance

    题链: http://poj.org/problem?id=1474 题解: 计算几何,半平面交 半平面交裸题,快要恶心死我啦... (了无数次之后,一怒之下把onleft改为onright,然后还加 ...

  5. 【USACO】干草金字塔

    题目描述 贝西要用干草包堆出一座金字塔.干草包会从传送带上陆续运来,依次出现 N 包,每包干草可 以看做是一个二维平面上的一个长方形,第 i 包干草的宽度是 W i ,长度统一为 1. 金字塔的修建有 ...

  6. ●BZOJ 4698 Sdoi2008 Sandy的卡片

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=4698 题解: 后缀数组,二分这个题还是比较套路的.首先依据题意,把各个串差分以后,用分割符号 ...

  7. 例10-3 uva10375(唯一分解定理)

    题意:已知C(m,n) = m!/(n!(m-n)!),已知p,q,r,s,求C(p,q)/C(r,s) 思路: 全部分解成质因子,相乘则加,除则减 #include <iostream> ...

  8. hihocoder1257(构造)(2015北京ACM/ICPC)

    题意: 给你n条蛇,a[i]的长度为i,要求组成一个矩形.奇数蛇可折叠奇数次,偶数蛇折叠偶数次,然后按蛇的次序输出 (即一条蛇的输出只能是一个方向的) 2 3 1 2 1 3 2 3 1 1 2 1 ...

  9. Python中str字符串的功能介绍

    Str字符串的功能介绍 1. 字符串的操作 字符串的连接操作 符号: + 格式:str1 + str2 例如:str1 = 'I Love' str2 = 'You!' print(str1 + st ...

  10. 阿里Java研发工程师实习面经

    十分幸运 拿到阿里云的offer,感谢周围无数人对我的支持和鼓励,所以写篇面经希望可以帮助大家. 面试中,运气占很大一部分的,所以你们若是没有通过,一定不要气馁,继续加油. 每个努力的人 都值得钦佩, ...