Lucene4.3进阶开发之漫漫修行( 四) - IndexWriterConfig



Lucene4.3进阶开发之漫漫修行( 四) - IndexWriterConfig
IndexWriterConfig这个类并不是一个顶级基类,在它的上面还有一个父类LiveIndexWriterConfig,我们先来分析下这个父类的一些作用,LiveIndexWriterConfig这个类是4.0以后新扩展的父类,在4.0之前并没有这个类,那么引入这个类的作用是什么呢? 

我们先来看下LiveIndexWriterConfig里面的部分源码: 
  1. private final Analyzer analyzer;  
  2.     
  3.   private volatile int maxBufferedDocs;  
  4.   private volatile double ramBufferSizeMB;  
  5.   private volatile int maxBufferedDeleteTerms;  
  6.   private volatile int readerTermsIndexDivisor;  
  7.   private volatile IndexReaderWarmer mergedSegmentWarmer;  
  8.   private volatile int termIndexInterval; // TODO: this should be private to the codec, not settable here  
  9.   
  10.   // modified by IndexWriterConfig  
  11.   /** {@link IndexDeletionPolicy} controlling when commit 
  12.    *  points are deleted. */  
  13.   protected volatile IndexDeletionPolicy delPolicy;  
  14.   
  15.   /** {@link IndexCommit} that {@link IndexWriter} is 
  16.    *  opened on. */  
  17.   protected volatile IndexCommit commit;  
  18.   
  19.   /** {@link OpenMode} that {@link IndexWriter} is opened 
  20.    *  with. */  
  21.   protected volatile OpenMode openMode;  
  22.   
  23.   /** {@link Similarity} to use when encoding norms. */  
  24.   protected volatile Similarity similarity;  
  25.   
  26.   /** {@link MergeScheduler} to use for running merges. */  
  27.   protected volatile MergeScheduler mergeScheduler;  
  28.   
  29.   /** Timeout when trying to obtain the write lock on init. */  
  30.   protected volatile long writeLockTimeout;  
  31.   
  32.   /** {@link IndexingChain} that determines how documents are 
  33.    *  indexed. */  
  34.   protected volatile IndexingChain indexingChain;  
  35.   
  36.   /** {@link Codec} used to write new segments. */  
  37.   protected volatile Codec codec;  
  38.   
  39.   /** {@link InfoStream} for debugging messages. */  
  40.   protected volatile InfoStream infoStream;  
  41.   
  42.   /** {@link MergePolicy} for selecting merges. */  
  43.   protected volatile MergePolicy mergePolicy;  
  44.   
  45.   /** {@code DocumentsWriterPerThreadPool} to control how 
  46.    *  threads are allocated to {@code DocumentsWriterPerThread}. */  
  47.   protected volatile DocumentsWriterPerThreadPool indexerThreadPool;  
  48.   
  49.   /** True if readers should be pooled. */  
  50.   protected volatile boolean readerPooling;  
  51.   
  52.   /** {@link FlushPolicy} to control when segments are 
  53.    *  flushed. */  
  54.   protected volatile FlushPolicy flushPolicy;  
  55.   
  56.   /** Sets the hard upper bound on RAM usage for a single 
  57.    *  segment, after which the segment is forced to flush. */  
  58.   protected volatile int perThreadHardLimitMB;  
  59.   
  60.   /** {@link Version} that {@link IndexWriter} should emulate. */  
  61.   protected final Version matchVersion;  
  62.   
  63.   /** True if segment flushes should use compound file format */  
  64.   protected volatile boolean useCompoundFile = IndexWriterConfig.DEFAULT_USE_COMPOUND_FILE_SYSTEM;  
看过之后,我们就会发现这个类里面,除了版本号和分词器是普通的成员变量外,其他的filed都有一个volite关键字修饰,从这个特点上,我们其实就可以看出点猫腻,这个类的主要作用,除了保存一个全局的配置信息外,其实就是抽象了一些IndexWriterConfig一些通用的全局变量,注意这个全局指的是基于JVM主存可见的,意思就是只要这个类的某个属性发生改变,那么这个变化就会立刻反映在主存中,这时候所有这个类的子类也就是IndexWriterConfig就会立刻获取最新的动态信息,从而做出相应的改变。
其中重要的方法有设置最大的文档数,设置最大的缓冲大小,设置删除合并策略,设置是否开启复合索引,以及设置一些自定义的打分策略等等。 


IndexWriterConfig是LiveIndexWriterConfig的子类,里面的大部分filed都是静态的常量,这个类的作用直接继承自它的父类,也是起到一个全局配置的作用,给IndexWriter提供一系列初始化的配置参数。 

IndexWriterConfig里面的一个静态内部类OpenMode的作用:
CREATE模式:这个模式下,每次新建的索引都会先清空上次索引的目录,然后在新建当前的索引,注意可以不用事先创建索引目录,这个模式一般是测试时候用的。

APPEND模式:这个模式下,每次新添加的索引,会被追加到原来的索引里,有一点需要注意的是,如果这个索引路径不存在的话,这个操作,将会导致报出一个异常,所以,使用此模式前,务必确定你有一个已经创建好的索引。

CREATE_OR_APPEND模式:这个模式就是我们默认 的模式,也是比较安全或者比较通用的模式,如果这个索引不存在,那么在此模式下就会新建一个索引目录,如果已存在,那么在添加文档的时候,直接会以Append的方式追加到索引里,所以此模式下,并不会出现一些意外的情况,所以大多数时候,建议使用此方式,进行构建索引。

Please read full article from Lucene4.3进阶开发之漫漫修行( 四) - IndexWriterConfig

No comments:

Post a Comment

Labels

Algorithm (219) Lucene (130) LeetCode (97) Database (36) Data Structure (33) text mining (28) Solr (27) java (27) Mathematical Algorithm (26) Difficult Algorithm (25) Logic Thinking (23) Puzzles (23) Bit Algorithms (22) Math (21) List (20) Dynamic Programming (19) Linux (19) Tree (18) Machine Learning (15) EPI (11) Queue (11) Smart Algorithm (11) Operating System (9) Java Basic (8) Recursive Algorithm (8) Stack (8) Eclipse (7) Scala (7) Tika (7) J2EE (6) Monitoring (6) Trie (6) Concurrency (5) Geometry Algorithm (5) Greedy Algorithm (5) Mahout (5) MySQL (5) xpost (5) C (4) Interview (4) Vi (4) regular expression (4) to-do (4) C++ (3) Chrome (3) Divide and Conquer (3) Graph Algorithm (3) Permutation (3) Powershell (3) Random (3) Segment Tree (3) UIMA (3) Union-Find (3) Video (3) Virtualization (3) Windows (3) XML (3) Advanced Data Structure (2) Android (2) Bash (2) Classic Algorithm (2) Debugging (2) Design Pattern (2) Google (2) Hadoop (2) Java Collections (2) Markov Chains (2) Probabilities (2) Shell (2) Site (2) Web Development (2) Workplace (2) angularjs (2) .Net (1) Amazon Interview (1) Android Studio (1) Array (1) Boilerpipe (1) Book Notes (1) ChromeOS (1) Chromebook (1) Codility (1) Desgin (1) Design (1) Divide and Conqure (1) GAE (1) Google Interview (1) Great Stuff (1) Hash (1) High Tech Companies (1) Improving (1) LifeTips (1) Maven (1) Network (1) Performance (1) Programming (1) Resources (1) Sampling (1) Sed (1) Smart Thinking (1) Sort (1) Spark (1) Stanford NLP (1) System Design (1) Trove (1) VIP (1) tools (1)

Popular Posts