`
tonybo2006
  • 浏览: 9248 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

Lucene建立索引的例子

阅读更多
/** * @author tonybo2006 * @version 2008/11/14 */ public class TextFileIndexer { private String dataFilePath = null; private String indexFilePath = null; private File dataFile = null; /** * @param dataFilePath */ public TextFileIndexer(String dataFilePath, String indexFilePath) { this.dataFilePath = dataFilePath; this.dataFile = new File(dataFilePath); this.indexFilePath = indexFilePath; } /** * @return StandardAnalyzer */ private Analyzer getAnalyzer() { return new StandardAnalyzer(); } /** * @return doc Document */ private IndexWriter getDocument(IndexWriter indexWriter) { try { if (dataFile.isDirectory()) { File[] files = dataFile.listFiles(); for (int i = 0; i < files.length; i++) { Document doc = new Document(); System.out.println("File " + files[i].getCanonicalPath() + "正在被索引...."); // file path doc.add(new Field("path", files[i].getAbsolutePath(),Field.Store.YES, Field.Index.NOT_ANALYZED)); // file modified time doc.add(new Field("modified", DateTools.timeToString(files[i].lastModified(), DateTools.Resolution.MINUTE), Field.Store.YES,Field.Index.NOT_ANALYZED)); // file content doc.add(new Field("contents", new FileReader(files[i]))); indexWriter.addDocument(doc); } } else { Document doc = new Document(); System.out.println(dataFile.getCanonicalPath() + "正在被索引...."); // file path doc.add(new Field("path", dataFilePath, Field.Store.YES,Field.Index.NOT_ANALYZED)); // file modified time doc.add(new Field("modified", DateTools.timeToString(dataFile.lastModified(), DateTools.Resolution.MINUTE),Field.Store.YES, Field.Index.NOT_ANALYZED)); // file content doc.add(new Field("contents", new FileReader(new File(dataFilePath)))); indexWriter.addDocument(doc); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return indexWriter; } /** * 建立索引。 */ public void createIndex() { try { IndexWriter indexWriter = new IndexWriter(indexFilePath,getAnalyzer(), true, IndexWriter.MaxFieldLength.UNLIMITED); long startTime = new Date().getTime(); System.out.println("开始索引……"); getDocument(indexWriter); // 测试一下索引的时间 long endTime = new Date().getTime(); System.out.println("索引完成。花费了" + (endTime - startTime) + " 毫秒来把文档增加到索引里面去!" + indexFilePath); indexWriter.optimize(); indexWriter.close(); } catch (CorruptIndexException e) { e.printStackTrace(); } catch (LockObtainFailedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * test */ public static void main(String[] args) throws Exception { String dataFilePath = "D:\\s";//数据文件路径 String indexFilePath = "D:\\index";//索引文件路径 TextFileIndexer textFileIndexer = new TextFileIndexer(dataFilePath,indexFilePath); textFileIndexer.createIndex(); } }
分享到:
评论

相关推荐

    Lucene操作数据库例子

    《Lucene操作数据库实战...通过将数据库内容建立索引,Lucene能够提供高效的全文搜索功能,适用于各种需要快速检索的应用场景。在实际应用中,可以根据需求调整Analyzer的选择,优化索引策略,以达到最佳的搜索性能。

    lucene检索小例子

    在建立索引后,"lucene检索小例子"中的搜索部分使用Analyzer、Query和IndexSearcher等组件来执行查询。Analyzer用于对查询字符串进行相同的预处理,Query对象代表了用户的查询意图,而IndexSearcher则负责在索引中...

    Lucene_索引数据库.doc

    通过Lucene为数据库建立索引,不仅可以提高搜索速度,还可以增强搜索功能,如支持模糊搜索、多字段组合搜索等。在实际项目中,可以根据具体需求灵活配置索引和查询策略,以达到最佳的搜索效果。

    lucene3 例子

    Lucene3包含了对文本的分词、标准化处理(如去除停用词、词干提取)以及建立倒排索引的能力,这些都是全文搜索的关键步骤。倒排索引允许快速定位包含特定关键词的文档,大大提高了搜索效率。 博文链接中提到的是一...

    SQLServer+Lucene.Net例子

    1. **数据抽取**:从SQL Server中读取需要建立索引的数据,这可以通过ADO.NET或者其他数据访问技术实现。 2. **创建索引**:使用Lucene.NET API对抽取的数据创建倒排索引。 3. **索引更新**:监听SQL Server中的数据...

    Lucene5+HanLP分词例子

    3. **创建索引**:在创建索引时,使用我们自定义的`Analyzer`对输入的文本进行分词,然后将分词结果作为`Document`的一部分存储到`IndexWriter`中,从而建立索引。 4. **执行查询**:当用户提交查询时,同样使用...

    lucene+中文IK分词器 例子

    总结来说,"lucene3.5 + ik中文分词器例子"是一个展示如何使用Lucene进行中文全文检索的示例,它涵盖了从数据抓取、分词处理、索引建立到查询执行的全过程。通过这个实例,开发者可以更好地理解和掌握Lucene与IK分词...

    lucene搜索的简单入门例子源代码

    4. **建立索引**:完成所有文档的添加后,记得调用`commit()`方法保存索引。 5. **搜索索引**:使用`IndexSearcher`来执行查询。首先创建一个`QueryParser`,指定要搜索的字段和分词器,然后用`parse(String query)...

    一个经典Lucene入门模块及例子解析

    首先,我们来看Lucene如何建立数据的索引。这通常涉及以下几个步骤: 1. **索引创建**:使用 `IndexWriter` 对象来创建或更新Lucene索引。在示例代码中,`IndexWriter` 初始化时传入了索引路径、分析器(在这里是 `...

    Lucene例子

    4. **分词器(Tokenizer)**: 分词器负责将输入的文本切分成一个个独立的词元(Token),这是建立索引的基础。 5. **分析器(Analyzer)**: 分析器包含分词器和其他处理步骤,如去除停用词、词形还原等,用于标准化...

    lucene 搜索引擎小例子

    3. **索引创建**:创建一个 `Directory` 实例(如使用 `FSDirectory` 存储在文件系统中),实例化 `Analyzer`,然后使用 `IndexWriter` 建立索引。 4. **查询执行**:创建 `IndexReader` 和 `IndexSearcher` 来读取...

    【大搜集:lucene学习资料】---<下载不扣分,回帖加1分,欢迎下载,童叟无欺>

    lucene为数据库搜索建立增量索引.txt lucene数据库索引.txt 新闻系统全文检索的思绪.txt lucene学习笔记 1 .txt lucene学习笔记 2.txt lucene学习笔记 3 .txt lucene入门实战.txt Lucene 的学习 .txt ...

    lucene例子

    1. **Searcher**:索引建立后,需要Searcher来执行查询。通常使用IndexSearcher,它负责执行查询、计算相关度并返回结果。 2. **Query构造**:Lucene提供多种Query类,如TermQuery、BooleanQuery、WildcardQuery等...

    lucene 例子以及所需jar包

    通过学习这个例子,你可以了解到如何使用 Lucene 进行文本索引和搜索,这对于开发需要全文搜索功能的应用来说非常有价值。记得阅读提供的安装说明,了解如何正确地导入和运行示例,从而更好地理解和掌握 Lucene 的...

    lucene 3.4基本应用

    本篇文章将深入探讨Lucene 3.4版本的基础应用,包括如何建立索引、更新索引以及执行查询。 ### 一、建立索引 在Lucene中,建立索引是搜索的第一步。首先,我们需要创建一个`IndexWriter`实例,这是负责写入索引的...

    lucene in action 书中例子源码

    索引构建过程中涉及分词、建立倒排索引和存储字段等步骤。 3. **文档模型**:`Document`类代表一个待索引的文档,包含多个`Field`,每个`Field`都有其特定的属性(如是否被索引、是否被存储等)。 4. **查询解析**...

    lucene2.9.1所有最新开发包及源码及文档

    2. 全文搜索的两个工作: 建立索引文件,搜索索引. 3. Lucene的索引文件逻辑结构 1) 索引(Index)由若干块(片段)(Segment)组成 ★2) 块由若干文档(Document)组成: 一个文件映射成一个文档。数据库表中的一条记录...

    lucene实现企业产品检索

    Lucene的核心是建立索引,将原始文本数据转化为结构化的、便于查询的数据结构。这个过程包括文本分析(Tokenization)、词干提取(Stemming)、去停用词(Stopword Removal)等步骤。在这个例子中,我们采用庖丁解牛...

    基于lucene.net 和highlighter插件的简单例子(对excel特殊处理)

    一旦索引建立完成,我们就可以使用QueryParser来构造用户输入的搜索查询,并执行搜索。 搜索结果返回后,Highlighter插件就派上用场了。我们可以对每个匹配的文档片段应用Highlighter,找出包含搜索关键词的行,并...

Global site tag (gtag.js) - Google Analytics