用户名: 密 码:
您现在的位置:首页 >> SEO开发技巧 >> 内容

lucene的多个类定义

时间:2010/2/24 18:33:36 点击:6731

  核心提示:索引文件里的文件命名有什么规律 引用_9.cfs _9.cfx segments_k segments.gen Java代码 privatefinalsynchronizedStringnewSegmentName(){ return'_'+Integer.toString(segmentInfos...
索引文件里的文件命名有什么规律

引用

_9.cfs
_9.cfx
segments_k
segments.gen


Java代码 复制代码
  1. private final synchronized String newSegmentName(){   
  2.   return "_"+Integer.toString(segmentInfos.counter++,Character.MAX_RADIX);   
  3. }  


将segmentInfos.counter加1后转为36进制。前面加下划线。所以segmentInfos.counter的值表示了segment中总共文档的数量。

文档倒排。这是建立索引内存消耗最大的时刻。除了词条,还需要存储词条位置,频率等信息

Java代码 复制代码
  1. package org.apache.lucene.index;   
  2.   
  3. /**  
  4.  * Licensed to the Apache Software Foundation (ASF) under one or more  
  5.  * contributor license agreements.  See the NOTICE file distributed with  
  6.  * this work for additional information regarding copyright ownership.  
  7.  * The ASF licenses this file to You under the Apache License, Version 2.0  
  8.  * (the "License"); you may not use this file except in compliance with  
  9.  * the License.  You may obtain a copy of the License at  
  10.  *  
  11.  *     http://www.apache.org/licenses/LICENSE-2.0  
  12.  *  
  13.  * Unless required by applicable law or agreed to in writing, software  
  14.  * distributed under the License is distributed on an "AS IS" BASIS,  
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  16.  * See the License for the specific language governing permissions and  
  17.  * limitations under the License.  
  18.  */  
  19.   
  20. import java.io.IOException;   
  21. import java.io.Reader;   
  22. import org.apache.lucene.document.Fieldable;   
  23. import org.apache.lucene.analysis.Token;   
  24. import org.apache.lucene.analysis.TokenStream;   
  25. import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;   
  26. import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;   
  27.   
  28. /**  
  29.  * Holds state for inverting all occurrences of a single  
  30.  * field in the document.  This class doesn't do anything  
  31.  * itself; instead, it forwards the tokens produced by  
  32.  * analysis to its own consumer  
  33.  * (InvertedDocConsumerPerField).  It also interacts with an  
  34.  * endConsumer (InvertedDocEndConsumerPerField).  
  35.  */  
  36.   
  37. final class DocInverterPerField extends DocFieldConsumerPerField {   
  38.   
  39.   final private DocInverterPerThread perThread;   
  40.   final private FieldInfo fieldInfo;   
  41.   final InvertedDocConsumerPerField consumer;   
  42.   final InvertedDocEndConsumerPerField endConsumer;   
  43.   final DocumentsWriter.DocState docState;   
  44.   final FieldInvertState fieldState;   
  45.   
  46.   public DocInverterPerField(DocInverterPerThread perThread, FieldInfo fieldInfo) {   
  47.     this.perThread = perThread;   
  48.     this.fieldInfo = fieldInfo;   
  49.     docState = perThread.docState;   
  50.     fieldState = perThread.fieldState;   
  51.     this.consumer = perThread.consumer.addField(this, fieldInfo);   
  52.     this.endConsumer = perThread.endConsumer.addField(this, fieldInfo);   
  53.   }   
  54.   
  55.   void abort() {   
  56.     consumer.abort();   
  57.     endConsumer.abort();   
  58.   }   
  59.   
  60.   public void processFields(final Fieldable[] fields,   
  61.                             final int count) throws IOException {   
  62.   
  63.     fieldState.reset(docState.doc.getBoost());   
  64.   
  65.     final int maxFieldLength = docState.maxFieldLength;   
  66.   
  67.     final boolean doInvert = consumer.start(fields, count);   
  68.   
  69.     for(int i=0;i<count;i++) {   
  70.   
  71.       final Fieldable field = fields[i];   
  72.   
  73.       // TODO FI: this should be "genericized" to querying   
  74.       // consumer if it wants to see this particular field   
  75.       // tokenized.   
  76.       if (field.isIndexed() && doInvert) {   
  77.   
  78.         if (fieldState.length > 0)   
  79.           fieldState.position += docState.analyzer.getPositionIncrementGap(fieldInfo.name);   
  80.   
  81.         if (!field.isTokenized()) {       // un-tokenized field   
  82.           String stringValue = field.stringValue();   
  83.           final int valueLength = stringValue.length();   
  84.           perThread.singleTokenTokenStream.reinit(stringValue, 0, valueLength);   
  85.           fieldState.attributeSource = perThread.singleTokenTokenStream;   
  86.           perThread.localTokenStream.reset();   
  87.           consumer.start(field);   
  88.   
  89.           boolean success = false;   
  90.           try {   
  91.             consumer.add();   
  92.             success = true;   
  93.           } finally {   
  94.             if (!success)   
  95.               docState.docWriter.setAborting();   
  96.           }   
  97.           fieldState.offset += valueLength;   
  98.           fieldState.length++;   
  99.           fieldState.position++;   
  100.         } else {                                  // tokenized field   
  101.           final TokenStream stream;   
  102.           final TokenStream streamValue = field.tokenStreamValue();   
  103.   
  104.           if (streamValue != null)    
  105.             stream = streamValue;   
  106.           else {   
  107.             // the field does not have a TokenStream,   
  108.             // so we have to obtain one from the analyzer   
  109.             final Reader reader;              // find or make Reader   
  110.             final Reader readerValue = field.readerValue();   
  111.   
  112.             if (readerValue != null)   
  113.               reader = readerValue;   
  114.             else {   
  115.               String stringValue = field.stringValue();   
  116.               if (stringValue == null)   
  117.                 throw new IllegalArgumentException("field must have either TokenStream, String or Reader value");   
  118.               perThread.stringReader.init(stringValue);   
  119.               reader = perThread.stringReader;   
  120.             }   
  121.              
  122.             // Tokenize field and add to postingTable   
  123.             stream = docState.analyzer.reusableTokenStream(fieldInfo.name, reader);   
  124.           }   
  125.   
  126.           // reset the TokenStream to the first token   
  127.           stream.reset();   
  128.   
  129.           try {   
  130.             int offsetEnd = fieldState.offset-1;   
  131.                
  132.             boolean useNewTokenStreamAPI = stream.useNewAPI();   
  133.             Token localToken = null;   
  134.                
  135.             if (useNewTokenStreamAPI) {   
  136.               fieldState.attributeSource = stream;   
  137.             } else {                 
  138.               fieldState.attributeSource = perThread.localTokenStream;   
  139.               localToken = perThread.localToken;   
  140.             }            
  141.                
  142.             consumer.start(field);   
  143.   
  144.             OffsetAttribute offsetAttribute = (OffsetAttribute) fieldState.attributeSource.addAttribute(OffsetAttribute.class);   
  145.             PositionIncrementAttribute posIncrAttribute = (PositionIncrementAttribute) fieldState.attributeSource.addAttribute(PositionIncrementAttribute.class);   
  146.                
  147.             for(;;) {   
  148.   
  149.               // If we hit an exception in stream.next below   
  150.               // (which is fairly common, eg if analyzer   
  151.               // chokes on a given document), then it's   
  152.               // non-aborting and (above) this one document   
  153.               // will be marked as deleted, but still   
  154.               // consume a docID   
  155.               Token token = null;   
  156.   
  157.   
  158. /**   
  159. token.termText 切出的词  
  160. token.startOffset 词的起始位置  
  161. token.endOffset 词的结束位置  
  162. */  
  163.   
  164.               if (useNewTokenStreamAPI) {   
  165.                 if (!stream.incrementToken()) break;   
  166.               } else {   
  167.                 token = stream.next(localToken);   
  168.                 if (token == nullbreak;   
  169.                 perThread.localTokenStream.set(token);   
  170.               }   
  171.                  
  172.               final int posIncr = posIncrAttribute.getPositionIncrement();   
  173.               fieldState.position += posIncr - 1;   
  174.               if (posIncr == 0)   
  175.                 fieldState.numOverlap++;   
  176.   
  177.               boolean success = false;   
  178.               try {   
  179.                 // If we hit an exception in here, we abort   
  180.                 // all buffered documents since the last   
  181.                 // flush, on the likelihood that the   
  182.                 // internal state of the consumer is now   
  183.                 // corrupt and should not be flushed to a   
  184.                 // new segment:   
  185.                 consumer.add();   
  186.                 success = true;   
  187.               } finally {   
  188.                 if (!success)   
  189.                   docState.docWriter.setAborting();   
  190.               }   
  191.               fieldState.position++;   
  192.               offsetEnd = fieldState.offset + offsetAttribute.endOffset();   
  193.               if (++fieldState.length >= maxFieldLength) {   
  194.                 if (docState.infoStream != null)   
  195.                   docState.infoStream.println("maxFieldLength " +maxFieldLength+ " reached for field " + fieldInfo.name + ", ignoring following tokens");   
  196.                 break;   
  197.               }   
  198.             }   
  199.             fieldState.offset = offsetEnd+1;   
  200.           } finally {   
  201.             stream.close();   
  202.           }   
  203.         }   
  204.   
  205.         fieldState.boost *= field.getBoost();   
  206.       }   
  207.     }   
  208.   
  209.     consumer.finish();   
  210.     endConsumer.finish();   
  211.   }   
  212. }  

文章来源:http://www.xinxilong.com

作者:不详 来源:网络
相关评论
发表我的评论
  • 大名:
  • 内容:
  • 论坛群发大师(www.xinxilong.com) © 2008 版权所有 All Rights Resverved.
  • Email:4984672[at]qq.com 沪ICP备12025887号-1
  • Powered by 论坛群发大师