`

lucene3.6.0索引操作的学习笔记

 
阅读更多

索引操作:
删除索引
indexreader:delete document,删除索引是在indexreader类进行
numDoc,maxDoc,删除索引是在内存先进行索引删除,合并索引后才能更新到磁盘,当删除一个document时,numDoc能及时更新,而maxDoc得等到合并索引后才会更新。
恢复被删除的索引:
undelete方法
更新索引:
删除之后再插入

批量操作


对document某些field加权,降权,
对整个Document加权降权:
Document.java类的下面这个方法:
  /** Sets a boost factor for hits on any field of this document.  This value
   * will be multiplied into the score of all hits on this document.
   *
   * <p>The default value is 1.0.
   * 
   * <p>Values are multiplied into the value of {@link Fieldable#getBoost()} of
   * each field in this document.  Thus, this method in effect sets a default
   * boost for the fields of this document.
   *
   * @see Fieldable#setBoost(float)
   */
  public void setBoost(float boost) {
    this.boost = boost;
  }

  对某些Field加权降权:
AbstractField.java类的下面这个方法:
  /** Sets the boost factor hits on this field.  This value will be
   * multiplied into the score of all hits on this this field of this
   * document.
   *
   * <p>The boost is multiplied by {@link org.apache.lucene.document.Document#getBoost()} of the document
   * containing this field.  If a document has multiple fields with the same
   * name, all such values are multiplied together.  This product is then
   * used to compute the norm factor for the field.  By
   * default, in the {@link
   * org.apache.lucene.search.Similarity#computeNorm(String,
   * FieldInvertState)} method, the boost value is multiplied
   * by the {@link
   * org.apache.lucene.search.Similarity#lengthNorm(String,
   * int)} and then
   * rounded by {@link org.apache.lucene.search.Similarity#encodeNormValue(float)} before it is stored in the
   * index.  One should attempt to ensure that this product does not overflow
   * the range of that encoding.
   *
   * @see org.apache.lucene.document.Document#setBoost(float)
   * @see org.apache.lucene.search.Similarity#computeNorm(String, FieldInvertState)
   * @see org.apache.lucene.search.Similarity#encodeNormValue(float)
   */
  public void setBoost(float boost) {
    this.boost = boost;
  }


lucene内部存储的都是字符串,如果要存储时间,数字需要确保自然顺序和字典顺序,默认是按照字典顺寻的,所以需要对日期和数字进行处理保证字典顺序。


排序:
IndexSearcher.java里面可以通过传入sort对象指定按照某些field进行排序
  /** Search implementation with arbitrary sorting.  Finds
   * the top <code>n</code> hits for <code>query</code>, applying
   * <code>filter</code> if non-null, and sorting the hits by the criteria in
   * <code>sort</code>.
   * 
   * <p>NOTE: this does not compute scores by default; use
   * {@link IndexSearcher#setDefaultFieldSortScoring} to
   * enable scoring.
   *
   * @throws BooleanQuery.TooManyClauses
   */
  @Override
  public TopFieldDocs search(Query query, Filter filter, int n,
                             Sort sort) throws IOException {
    return search(createNormalizedWeight(query), filter, n, sort);
  }

Sort.java可以传入多个Field对象:
  /** Sets the sort to the given criteria. */
  public void setSort(SortField field) {
    this.fields = new SortField[] { field };
  }

  /** Sets the sort to the given criteria in succession. */
  public void setSort(SortField... fields) {
    this.fields = fields;
  }
  
SortField.java具体某个排序字段的规则:可以指定哪个字段,并给定字段的类型进行排序,具体的type值可以参考SortField.java类里面定义的静态变量,用于排序的字段必须能够转化为整型,浮点型,字符串型的对象。
  /** Creates a sort, possibly in reverse, by terms in the given field with the
   * type of term values explicitly given.
   * @param field  Name of field to sort by.  Can be <code>null</code> if
   *               <code>type</code> is SCORE or DOC.
   * @param type   Type of values in the terms.
   * @param reverse True if natural order should be reversed.  降序还是升序
   */
  public SortField(String field, int type, boolean reverse) {
    initFieldType(field, type);
    this.reverse = reverse;
  }


 /** Sort using term values as Strings.  Sort values are String and lower
   * values are at the front. */
  public static final int STRING = 3;

  /** Sort using term values as encoded Integers.  Sort values are Integer and
   * lower values are at the front. */
  public static final int INT = 4;

  /** Sort using term values as encoded Floats.  Sort values are Float and
   * lower values are at the front. */
  public static final int FLOAT = 5;

  /** Sort using term values as encoded Longs.  Sort values are Long and
   * lower values are at the front. */
  public static final int LONG = 6;

  /** Sort using term values as encoded Doubles.  Sort values are Double and
   * lower values are at the front. */
  public static final int DOUBLE = 7;
......


控制索引过程
调整索引性能
1.添加document时,先缓存在内存中,达到一定条件后刷新到磁盘
  mergeFactor 默认10,控制段的合并频率和大小  内存大,可以设置得大一点,减少磁盘io,但是索引文件一多,检索速度会变慢,内存小需要设置得小一点
  maxMergeDocs MAX  控制每个段的大小
  minMergeDocs 10  控制索引时RAM使用的总量

当jvm分配的内存比较大时,适当提高mergefactor和minMergeDocs可以提高索引过程的速度,

设置LogMergePolicy.java来调整索引性能
public abstract class LogMergePolicy extends MergePolicy {

  /** Defines the allowed range of log(size) for each
   *  level.  A level is computed by taking the max segment
   *  log size, minus LEVEL_LOG_SPAN, and finding all
   *  segments falling within that range. */
  public static final double LEVEL_LOG_SPAN = 0.75;

  /** Default merge factor, which is how many segments are
   *  merged at a time */
  public static final int DEFAULT_MERGE_FACTOR = 10;

  /** Default maximum segment size.  A segment of this size
   *  or larger will never be merged.  @see setMaxMergeDocs */
  public static final int DEFAULT_MAX_MERGE_DOCS = Integer.MAX_VALUE;

  /** Default noCFSRatio.  If a merge's size is >= 10% of
   *  the index, then we disable compound file for it.
   *  @see #setNoCFSRatio */
  public static final double DEFAULT_NO_CFS_RATIO = 0.1;

  protected int mergeFactor = DEFAULT_MERGE_FACTOR;

  protected long minMergeSize;
  protected long maxMergeSize;

文件打开个数:
lucene默认打开的文件最大数个数:
(1+mergeFactor)*FilesPerSegement
linux下可以通过ulimit -u 设置打开文件描述符个数

内存所以RAMDirectory.java
1.可以先把doc存到内存索引中,然后隔一段时间或者插入一定量的document之后通过把RAMDirectory内存中的索引通过IndexWriter刷新到磁盘,从而达到把RAMDirectory当做内存缓存器实现对索引的批处理
并行索引多个索引文件
1.可以通过RAMDirectory对多个CPU,磁盘或者机器进行并行索引,如果是同一台机器,可以通过多线程同步方式最后利用IndexWriter的addIndexes(Directorys[])进行索引合并再写入磁盘,如果是不同机器,则可以通过把单台机器的索引进行分发到同一台机器后后再进行合并。


限制域的大小:maxFieldLength  可以使用LimitTokenCountAnalyzer.java这个analyzer来处理域的大小

IndexWriter 的optimize()方法来优化索引。只能提高检索速度,不能提高插入doc的速度。

在同一个时间,只能有一个进程修改一个索引文件。lucene利用文件的锁机制来防止这种由并发修改索引文件导致的问题。

优化索引文件最好是在索引过程结束之后,并且在此后一段时间不会被修改。


lucene对索引文件的并发访问规则:
1.只读操作可以并行操作。
2.在索引被修改的时候,仍然可以进行多个数量的只读操作。
3.在某一时间,只能允许一个修改索引的操作。也就是说在同一个时间,一个索引文件只能由一个indexWriter或者一个indexReader打开。

indexReader和indexWriter是多线程安全的。
1.indexReader对象从索引删除一个document时,indexWriter不能向其中添加文档。
2.indexWriter在进行合并或者优化时,indexReader也不能删除文档。

lucene索引锁机制,文件write.lock,commit.lock
IndexWriter.java关于文件锁的处理API:
  /**
   * Returns <code>true</code> iff the index in the named directory is
   * currently locked.
   * @param directory the directory to check for a lock
   * @throws IOException if there is a low-level IO error
   */
  public static boolean isLocked(Directory directory) throws IOException {
    return directory.makeLock(WRITE_LOCK_NAME).isLocked();
  }

  /**
   * Forcibly unlocks the index in the named directory.
   * <P>
   * Caution: this should only be used by failure recovery code,
   * when it is known that no other process nor thread is in fact
   * currently accessing this index.
   */
  public static void unlock(Directory directory) throws IOException {
    directory.makeLock(IndexWriter.WRITE_LOCK_NAME).release();
  }

可以通过设置IndexWriter的infoStream来查看索引操作的信息。
 
分享到:
评论

相关推荐

    lucene-3.6.0

    6. **多线程支持**:Lucene 3.6.0支持多线程索引和查询,通过适当的同步机制保证了并发环境下的正确性。 7. **JAR文件**:Lucene 3.6.0的发布包含了一系列JAR文件,如core库、示例程序、测试工具等,方便开发者直接...

    lucene-3.6.0.zip

    《Apache Lucene 3.6.0:搜索引擎技术的核心解析》 Apache Lucene是一个高性能、全文本搜索库,被广泛应用于各种搜索引擎的开发中。3.6.0版本是Lucene的一个重要里程碑,它提供了丰富的功能和改进,使得开发者能够...

    lucene 3.6.0 源代码

    lucene-core-3.6.0-sources 绝对可用

    lucene索引非txt文档笔记

    为了实现对这些非文本格式的文档进行搜索,我们需要首先将其转换为纯文本,然后才能用 Lucene 进行索引。 对于 Word 文档,Java 提供了 POI 和 TextMining 两个库来完成转换。POI 是一个用于读写 Microsoft Office ...

    Lucene索引的基本操作

    **Lucene索引的基本操作** Lucene是一款由Apache软件基金会开发的全文检索库,它提供了高效、可扩展的全文检索功能。在Java开发环境中,Lucene是广泛使用的文本搜索工具,能够帮助开发者构建复杂的搜索引擎。本文将...

    Lucene建索引及查询关键字

    在Eclipse环境中运用java,Lucene建索引及查询关键字

    Lucene实现索引和查询的实例讲解

    Lucene实现索引和查询的实例讲解Lucene实现索引和查询的实例讲解

    lucene-3.6.0 api 手册

    lucene-3.6.0 api 手册, 最新的 , lucene 是个好东东, 一直在用, 之前还在使用3.1的,发现已经到3.6了, 落后啊

    基于lucene技术的增量索引

    **基于Lucene技术的增量索引** 在信息技术领域,全文搜索引擎是处理大量数据查询的关键工具。Apache Lucene是一个开源的全文检索库,被广泛应用于构建高效、可扩展的搜索功能。本文将深入探讨如何利用Lucene实现...

    lucene做索引查询流程

    lucene 做索引查询流程,来自《lucene in action》

    Lucene5学习之增量索引(Zoie)

    总结起来,Lucene5学习之增量索引(Zoie)涉及到的关键技术点包括: 1. 基于Lucene的增量索引解决方案:Zoie系统。 2. 主从复制架构:Index Provider和Index User的角色。 3. 数据变更追踪:通过变更日志实现增量索引...

    Lucene创建索引步骤

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

    Lucene读取索引文件

    《深入理解Lucene:解析索引文件的读取》 Lucene,作为一款强大的全文搜索引擎库,被广泛应用于各类信息检索系统中。它的核心功能之一就是构建和读取索引文件,以高效地进行文本搜索。本文将深入探讨Lucene如何读取...

    Lucene之删除索引

    首先,删除索引是Lucene中一个关键的操作,因为随着时间的推移,数据可能会更新或过时,此时就需要删除旧的索引信息。Lucene的索引删除过程并不像传统的文件系统删除那么简单,它涉及到对倒排索引结构的修改。 1. *...

    lucene2.4+nutch学习笔记三:lucene 在多个文本文档里找出包含一些关键字的文档

    《Lucene 2.4与Nutch学习笔记:在多文本文档中搜索关键词》 Lucene是一个高性能、全文本搜索引擎库,它为开发者提供了在Java应用程序中实现全文搜索功能的基本工具。Nutch则是一个开源的网络爬虫项目,用于抓取...

    lucene实现索引查询

    在Java开发中,Lucene 提供了丰富的API来简化这些操作。以下是关于使用Lucene实现索引查询的详细知识: ### 一、创建索引 创建索引是Lucene的核心过程,它涉及到以下步骤: 1. **定义索引目录**:首先,你需要...

    lucene索引结构原理

    **Lucene索引结构原理** Lucene是Apache软件基金会的开放源代码全文搜索引擎库,它为Java开发人员提供了强大的文本搜索功能。理解Lucene的索引结构原理对于优化搜索性能和设计高效的搜索应用至关重要。 首先,我们...

    lucene 索引小示例

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

    基于lucene的索引与搜索

    本文档主要探讨了基于Apache Lucene的索引与搜索技术,这是构建高效全文搜索引擎的关键组成部分。Lucene是一个开源的Java库,提供了强大的文本分析、索引和搜索功能,被广泛应用于各种信息检索系统中。 第一章 引言...

    lucene索引查看程序及代码

    通过阅读和分析源代码,我们可以学习到如何操作Lucene索引,以及如何构建类似的工具。 总结而言,luke作为Lucene索引的可视化工具,极大地便利了开发者对索引的理解和调试。无论是初学者还是经验丰富的开发人员,都...

Global site tag (gtag.js) - Google Analytics