`
Tonyguxu
  • 浏览: 271679 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

【Lucene】更合理地使用Document和Field

 
阅读更多

 

writer = ...; //#1
PreparedStatement pstmt = conn.prepareStatement(selectSql);
ResultSet	rs = pstmt.executeQuery();
Document doc = null;
while (rs.next()) {
	doc = new Document(); //#2
	doc.add(new Field(ConstantsUtil.ROW_ID, rs.getString("rowid"), Field.Store.YES,Field.Index.UN_TOKENIZED)); //#3
	doc.add(new Field(ConstantsUtil.FD_COMMAND_ID, String.valueOf(rs.getLong(ConstantsUtil.DB_COMMAND_ID)),
Field.Store.YES, Field.Index.UN_TOKENIZED));
	if (rs.getString(ConstantsUtil.DB_DEST_ID) != null)
		doc.add(new Field(ConstantsUtil.FD_DEST_ID, rs.getString(ConstantsUtil.DB_DEST_ID), Field.Store.YES,
								Field.Index.TOKENIZED));
	if (rs.getString(ConstantsUtil.DB_SRC_ID) != null)
		doc.add(new Field(ConstantsUtil.FD_SRC_ID, rs.getString(ConstantsUtil.DB_SRC_ID), Field.Store.YES,
								Field.Index.TOKENIZED));
	doc.add(new Field(ConstantsUtil.FD_UP_MSG_ID, String.valueOf(rs.getLong(ConstantsUtil.DB_UP_MSG_ID)),
Field.Store.YES, Field.Index.UN_TOKENIZED));
	doc.add(new Field(ConstantsUtil.FD_CREATED_DATE, DateTools.dateToString(rs.getTimestamp(ConstantsUtil.DB_CREATED_DATE), DateTools.Resolution.MINUTE), Field.Store.YES,
Field.Index.UN_TOKENIZED));
	if (rs.getString(ConstantsUtil.DB_STATION_ID) != null)
		doc.add(new Field(ConstantsUtil.FD_STATION_ID, rs.getString(ConstantsUtil.DB_STATION_ID),Field.Store.YES, Field.Index.UN_TOKENIZED));
	writer.addDocument(doc); //#4
}

以上设计、编码存在一些问题:

1.对于ResultSet的一行就实例一个Document。How to make indexing faster 建议重用Document 和 Field实例。 ——性能?

 

2.数据库一个字段对应一个Field,简单地将需要的字段对应成Field然后 add到Document里(没有理解Docuemt、Field、全文检索,lucene里的Field和数据库中的字段是不是一样的? ),并且Field的值也直接来自数据库中的值

 

如果以后需要把数据库中其他字段的值也加入到索引里,该怎么做?按上面的思路,只能把需要的字段构造相应的Field然后add到Document里,需要修改这里的代码,增加doc.add(field) 。        ——修改:灵活性,增加新需求:可扩展性?

并且如果构造一个Filed的value需要在从数据库取出的原始值基础上改造(比如截取数字的部分值)or 新需求需要修改原先的获得值的方法,还是需要对上面代码做修改。          ——修改灵活性

 

lucene里的Field和数据库中的字段是不是一样的?

不是。

 

Re-use Document and Field instances

http://www.lucidimagination.com/search/link?url=http://wiki.apache.org/lucene-java/HowTo 写道
Re-use Document and Field instances As of Lucene 2.3 there are new setValue(...) methods that allow you to change the value of a Field. This allows you to re-use a single Field instance across many added documents, which can save substantial GC cost. It's best to create a single Document instance, then add multiple Field instances to it, but hold onto these Field instances and re-use them by changing their values for each added document. For example you might have an idField, bodyField, nameField, storedField1, etc. After the document is added, you then directly change the Field values (idField.setValue(...), etc), and then re-add your Document instance.

Note that you cannot re-use a single Field instance within a Document, and, you should not change a Field's value until the Document containing that Field has been added to the index. See Field for details.

 

基于原有的设计,如果遇到以下问题该如何处理?

1.不想有这么多的Field,即Field不应该是与数据库中的字段一一对应,如content域,想让数据库中若干个字段的值合在一块构成一个content域。

 

2.如果一个Field的值不能直接拿数据库中的值,而是需要做些处理(可能是格式上的也可能是跟业务有关的)。

 

3.需求变更:需要修改某个Field值的获取,比如原先是截取某数字前4位,现在想截取前6位。

 

4.新增字段索引需求:需要对数据库中某字段的值建索引(该字段的值原先不在索引里)。

 

TODO:结合《java与模式》3.1 软件系统的可维护性 来思考以上问题。

 

如果基于原来的设计,由数据库中一行数据获得某个Field的值,在该类中一个方法里(getValue(bean))处理,然后将处理结果返回。如果需要修改field的值则需要修改方法,如果要增加某个field,则增加document.add(new Field(name,getValue(),xx))同时增加相应的获取value的方法getValue。

 

重构:Field对象的创建和值的获取

利用一个继承结构负责Field的创建和值的获取。

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    lucene,lucene教程,lucene讲解

    org.apache.lucene.document.Field Directory类代表一个Lucene索引的位置。它是一个抽象类. 其中的两个实现: 第一个是 FSDirectory,它表示一个存储在文件系统中的索引的位置。 第二个是 RAMDirectory,它表示一...

    lucene基本包

    Lucene一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎,部分文本分析引擎

    java Lucene初级教程

    将源中需要的信息加入Document的各个Field中,并把需要索引的Field索引起来,把需要存储的Field存储起来。  将索引写入存储器,存储器可以是内存或磁盘。 2.2读出流程  用户提供搜索关键词,经过analyzer处理。 对...

    Apache Lucene全文检索和IKAnalyzer分词工具类

    import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.queryParser.MultiFieldQueryParser; import org.apache....

    luke--查看lucene 索引文件的工具

    查看lucene索引文件的document和field

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

    开源全文搜索工具包Lucene2.9.1的使用。 1. 搭建Lucene的开发环境:在classpath中添加lucene-core-2.9.1.jar包 2. 全文搜索的两个工作: 建立索引文件,搜索索引. 3. Lucene的索引文件逻辑结构 1) 索引(Index)由...

    lucene2.9.1完整DEMO及开发文档

    doc.add(new Field("path", src.getCanonicalPath(), Field.Store.YES, Field.Index.NOT_ANALYZED)); StringBuilder sb = new StringBuilder(); BufferedReader br = new BufferedReader(new FileReader(src)...

    Lucene创建索引步骤

    Lucene创建索引步骤: 1、创建Directory(索引位置) 2、创建IndexWrite(写入索引) 3、创建Document对象 4、为Document添加Field(相当于添加属性:类似于表与字段的关系) 5、通过IndexWriter添加文档到索引中

    Lucene查询工具LQT.zip

    Lucene Query Tool (lqt) 是一个命令行工具用来执行 Lucene 查询并对结果进行格式化输出。 使用方法: $ ./lqt usage: LuceneQueryTool [options] --analyzer <arg> for query, (KeywordAnalyzer | ...

    Lucene 源码解析

    FileReaderAll函数用来从文件中读取字符串,默认编码为“GBK”。...Document的构造函数为空,StringField、TextField和Field的构造函数也很简单。下面重点分析IndexWriter的addDocument函数,代码如下

    基于lucene的搜索引擎总结

    Field:每个Document包含一个或多个不同命名的Field,每个Field对应一段数据,这些数据在搜索过程中可能会被查询或在索引中被检索 全文索引/搜索 Lucene索引代码示例: Directory dir = FSDirectory.getDirectory...

    集成Lucene和HBase

    Lucene简介Lucene中可搜索的实体都表现为文档(document),它由字段(field)和值(value)组成。每个字段值都由一个或多个可搜索的元素——即词汇(term)——组成。Lucene搜索基于反向索引,其中包含了关于可搜索...

    Lucene中文分词组件 JE-Analysis 1.4.0

    import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.Hits; import org.apache....

    最新Lucene教程

    3、document提供对Document和Field的各种操作的支持。 4、index是最重要的包,用于向Lucene提供建立索引时各种操作的支持 5、queryParser提供检索时的分析支持 6、search负责检索 7、store提供对索引存储的支持 ...

    开放源代码的全文检索引擎Lucene

    第一节 全文检索系统与Lucene简介··· 3 一、 什么是全文检索与全文检索系统?··· 3 二、 什么是Lucene?··· 4 三、 Lucene的应用、特点及优势··· 4 四、 本文的重点问题与cLucene项目··· 5 第二...

    clucene-core lucene c

    总得来说用Lucene来进行建立 和搜索和操作数据库是差不多的(有点像),Document可以看作是 数据库的一行记录,Field可以看作是数据库的字段。用lucene实 现搜索引擎就像用JDBC实现连接数据库一样简单。

    WordAnalytics:基于HanLP和Lucene的全文索引和搜索库

    基于HanLP和Lucene的全文索引和搜索库。 安装 您可以在使用Cube Repo 原料药 创建索引目录 WordAnalytics analytics = new WordAnalytics ( new File ( " datadirectory " )); 新增文件 Document document ...

    重要lucene2.0 学习文档

    Lucene是apache组织的一个...总得来说用Lucene来进行建立和搜索和操作数据库是差不多的,Document可以看作是数据库的一行记录,Field可以看作是数据库的字段。用lucene实现搜索引擎就像用JDBC实现连接数据库一样简单。

    learn-lucene:lucene学习

    lucene_learnlucene学习day_01:索引创建的步骤:创建directory创建IndexWriter创建Document为Document添加Field通过IdexUriter添加文档到索引中搜索的步骤:创建directory创建IndexReader根据IndexReader创建...

Global site tag (gtag.js) - Google Analytics