`
Mr_Tank_
  • 浏览: 22589 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

【Lucene】基本索引操作

阅读更多

最近在学Lucene,参考书籍为《Lucene in action 中文版》,这里的代码例子也是参考里面的【有些出入,不过不少很大】,欢迎各位大神们拍砖;至于一些介绍什么的大家可以参考一下前面说的参考书;程序用到的包我是在官网那里下的,也把他的参考文档下了【英文版】,看文档给写代码提供了不少帮助;

 

1、Lucene重要的类:

(1)Document;(2)Field;(3)Store;(4)Index;

(5)IndexWriter;(6)Directory;(7)IndexReader;(8)Query;等

2、向索引添加文档;添加文档的方法有两个:

(1)addDocument(Document);(2)addDocument(Document,Analyzer);

3、更新索引中的文档:

IndexReader提供了两个方法用于更新索引中的文档:

(1)updateDocument(Term,Document);

(2)updateDocument(Term,Document,Analyzer);

4、删除索引中的文档

 

5、下面是示例代码【人比较懒只写了添加和更新两个操作】:

 

package com.tan.code;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.cjk.CJKAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;

public class IndexingTest {

	// 準備數據
	protected String[] ids = { "1", "2" };
	protected String[] unindexed = { "Netherlands", "Italy" };
	protected String[] unstored = { "Amsterdam has lots of bridges",
			"Venice has lots of cancals" };
	protected String[] text = { "Amsterda", "Venice" };
	private String indexpath = "C:/index";
	File file = new File(indexpath);
	private Directory directory;

	@SuppressWarnings("deprecation")
	public void setUp() throws IOException {

		directory = new SimpleFSDirectory(file);

		// 索引位置在内存
		// directory = new RAMDirectory();
		// 创建IndexWriter对象
		IndexWriter indexWriter = getIndexWriter();

		for (int i = 0; i < ids.length; i++) {
			Document document = new Document();
			document.add(new Field("id", ids[i], Field.Store.YES,
					Field.Index.NOT_ANALYZED));
			document.add(new Field("country", unindexed[i], Field.Store.NO,
					Field.Index.NOT_ANALYZED));
			document.add(new Field("contents", unstored[i], Field.Store.NO,
					Field.Index.NOT_ANALYZED));
			document.add(new Field("city", text[i], Field.Store.YES,
					Field.Index.NOT_ANALYZED));
			indexWriter.addDocument(document);
		}
		// IndexReader[] ir = { IndexReader.open(directory) };
		// indexWriter.addIndexes(ir);
		// indexWriter.optimize();
		// indexWriter.addIndexes();
		indexWriter.close();
	}

	private IndexWriter getIndexWriter() throws IOException {
		return new IndexWriter(directory, new IndexWriterConfig(
				Version.LUCENE_44, new CJKAnalyzer(Version.LUCENE_44)));
		// return new IndexWriter(directory,new WhitespaceAnalyzer(),
		// IndexWriter.MaxFieldLength.UNLIMITED);
	}

	public int getHitsCount(String filedname, String searchString)
			throws IOException {

		directory = new SimpleFSDirectory(file);
		IndexReader indexReader = IndexReader.open(directory);
		// IndexSearcher searcher = new IndexSearcher(directory);
		IndexSearcher indexSearcher = new IndexSearcher(indexReader);
		Term term = new Term(filedname, searchString);

		// 建立Term查尋
		Query query = new TermQuery(term);
		TopDocs topDocs = indexSearcher.search(query, null, 10);
		ScoreDoc[] scoreDocs = topDocs.scoreDocs;
		return scoreDocs.length;

	}

	@SuppressWarnings("deprecation")
	public void updateIndex() throws IOException {

		directory = new SimpleFSDirectory(file);
		IndexWriter indexWriter = getIndexWriter();
		Document document = new Document();
		document.add(new Field("id", "1", Field.Store.YES,
				Field.Index.NOT_ANALYZED));
		document.add(new Field("country", "China", Field.Store.NO,
				Field.Index.NOT_ANALYZED));
		document.add(new Field("contents", "Beijing has lots of people",
				Field.Store.NO, Field.Index.NOT_ANALYZED));
		document.add(new Field("city", "Beijing", Field.Store.YES,
				Field.Index.NOT_ANALYZED));
		indexWriter.updateDocument(new Term("id", "1"), document);
		indexWriter.close();
	}

}


6、测试代码:

 

package com.tan.test;

import static org.junit.Assert.*;

import java.io.IOException;

import org.junit.Test;

import com.tan.code.IndexingTest;

public class MyTest {

	@Test
	public void test() throws IOException {
		//fail("Not yet implemented");
		IndexingTest indexingTest=new IndexingTest();
		//indexingTest.setUp();
		//System.out.println(indexingTest.getHitsCount("city", "Amsterda"));
		
		//updatetest
		indexingTest.updateIndex();
		//确认旧文档已经删除
		assertEquals(0, indexingTest.getHitsCount("city", "Amsterdam"));
		//确认新文档被索引
		assertEquals(1, indexingTest.getHitsCount("city", "Beijing"));
	}

}


7、源代码已经上传到了我的资源里面,有兴趣可以去下载,下载链接http://download.csdn.net/detail/mr_tank_/6017929

【敬请批评指正】

 

 

分享到:
评论

相关推荐

    Lucene索引的基本操作

    为了确保索引操作的正确性,通常会编写测试代码。例如,我们可以创建一个简单的测试类`L_IndexingTest`,使用`DirectoryReader`和`IndexSearcher`来查询索引: ```java public class L_IndexingTest { public ...

    Lucene之删除索引

    在深入探讨Lucene删除索引这一主题之前,我们先来理解一下Lucene的基本概念。Lucene是一个开源的全文搜索引擎库,由Apache软件基金会开发。它提供了高性能、可扩展的搜索和索引功能,广泛应用于各种应用程序中,如...

    Lucene读取索引文件

    首先,我们需要了解Lucene索引的基本结构。一个Lucene索引是由多个文件组成的,包括但不限于 segments文件、.del文件(删除文档标记)、.tii和.tis文件(Term Info Index和Term Info postings)、.frx、.fdx、.fdt、...

    Lucene索引器实例

    完成索引操作后,记得关闭`IndexWriter`以释放资源。 **6. 搜索索引** - 创建`IndexReader`和`IndexSearcher`:它们分别用于读取索引和执行搜索。 - 创建`QueryParser`:解析用户输入的查询字符串并生成`Query`对象...

    lucene实现索引查询

    以上就是使用Lucene实现索引查询的基本流程。实际应用中,可能还需要考虑错误处理、性能优化、多线程支持、查询结果的排序和过滤等功能。理解并熟练运用这些知识,能帮助你构建高效、灵活的全文搜索系统。

    lucene 索引小示例

    本篇文章将通过一个简单的小示例,深入探讨Lucene的核心概念和操作流程。 首先,我们需要理解Lucene的索引机制。索引是Lucene处理文档的关键步骤,它将文本数据转换为一种结构化的、可快速搜索的形式。在创建索引时...

    lucene全文检索简单索引和搜索实例

    一、Lucene基本概念 1. 文档(Document):在Lucene中,一个文档代表了要索引的信息源,它可以是网页、电子邮件、PDF文档等。文档由多个字段(Field)组成,每个字段具有不同的含义和处理方式。 2. 字段(Field)...

    lucene索引查看工具及源码

    它允许用户以图形化的方式查看、操作和分析 Lucene 索引。 Luke 提供了诸如查看文档字段、搜索索引、查看倒排索引结构等功能,对于开发者来说是了解和调试 Lucene 索引的利器。 Luke 的源码也公开在 GitHub 上,这...

    lucene并行索引

    倒排索引的基本思想是为每个文档中的每个词建立索引,并记录该词出现在哪些文档中及其位置信息。 - **倒排索引**:对于每个单词或词条,都会有一个包含该词条出现过的所有文档ID的列表。这种方式与传统的文档索引...

    Lucene 索引的简单使用

    Lucene基本概念 - **文档(Document)**:在Lucene中,一个文档代表你要索引的信息单元,它可以包含多个字段(Field)。 - **字段(Field)**:字段是文档的组成部分,每个字段都有特定的类型(如文本、日期等)...

    Lucene索引和查询

    本项目中的代码旨在展示如何利用Lucene对多个文件夹下的数据进行索引创建和查询操作。 首先,我们需要了解Lucene的基本概念。Lucene的核心思想是将文本数据转换为结构化的索引,以便于快速查找相关文档。这个过程...

    lucene索引结构原理.docx

    而在Lucene中,基本单位是Document,它同样由多个字段组成,但Lucene索引的是这些字段的内容,以加速文本检索。 - **索引构建**:Lucene支持增量索引和批量索引,可以处理数据源的小幅变化或大规模数据。数据库通常...

    lucene索引文件格式介绍

    Lucene 是一个流行的开源全文搜索引擎库,其核心功能之一就是构建高效的索引。索引文件格式是Lucene实现快速搜索的关键。...通过理解和掌握这种格式,开发者能够更好地定制和优化Lucene的索引操作,提升搜索性能。

    Lucene建立索引

    - 它不仅支持基本的关键词搜索,还提供了高级的搜索特性,如短语搜索、布尔逻辑操作、近似搜索、模糊搜索等。 2. **倒排索引** - 倒排索引是Lucene的核心数据结构,它将每个文档中出现的单词映射到包含这些单词的...

    lucene 4.7.2 Demo

    在索引管理方面,Lucene 4.7.2提供了对索引的增删改操作。新增文档只需添加新的Document到IndexWriter,删除操作则通过Term或Query指定删除条件,修改则涉及读取旧文档、更新Field并重新写入。这些操作都需要谨慎...

    NLuke Lucene.Net索引管理

    5. **文档添加与删除**:虽然NLuke主要是一个查看工具,但也可以用来测试索引操作,如添加新的文档或者删除已存在的文档,这对于开发和测试索引功能非常实用。 6. **优化与段合并**:NLuke允许执行索引的优化操作,...

    luke lucene索引查看

    Luke能够处理这种情况,展示所有段的信息,并支持合并或优化索引操作。 9. **版本兼容性**:尽管Luke 0.8.1是较早的版本,但它仍然可以用于理解早期Lucene版本的索引结构,这对于维护旧项目或者研究历史版本的变更...

    Lucene索引管理器(基于Luke修改而来)

    而Luke是Lucene的可视化工具,它允许开发者查看和操作Lucene索引,包括文档内容、字段、分词结果等。本文将深入探讨基于Luke修改的Lucene索引管理器,以及它在源码分析和工具应用方面的重要作用。 首先,我们了解下...

    lucene.net 2.9.2 实现索引生成,修改,查询,删除实例

    Lucene.NET 2.9.2 是一个基于Apache Lucene的全文检索库,适用于.NET Framework。这个库提供了高效、可扩展的全文检索功能...通过理解和实践这些基本操作,开发者可以构建出高效的搜索解决方案,满足不同场景下的需求。

    Lucene4.10.3索引+查询

    - **索引(Index)**:索引是Lucene处理大量数据的关键,它将文本数据转换为可搜索的形式。索引过程包括分词(Tokenization)、词干提取(Stemming)、去除停用词(Stop Word Removal)等步骤。 - **分词器...

Global site tag (gtag.js) - Google Analytics