-
- //org.apache.lucene.index => DocumentsWriter.java
- /* Invert one occurrence of one field in the document */
- public void invertField(Fieldable field, Analyzer analyzer, final int maxFieldLength) throws IOException, AbortException {
- if (length>0)
- position += analyzer.getPositionIncrementGap(fieldInfo.name);
- if (!field.isTokenized()) { // un-tokenized field 不用分成的field
- String stringValue = field.stringValue();
- final int valueLength = stringValue.length();
- Token token = localToken;
- token.clear();
- char[] termBuffer = token.termBuffer();
- if (termBuffer.length < valueLength)
- termBuffer = token.resizeTermBuffer(valueLength);
- stringValue.getChars(0, valueLength, termBuffer, 0);
- token.setTermLength(valueLength);
- token.setStartOffset(offset);
- token.setEndOffset(offset + stringValue.length());
- addPosition(token);
- offset += stringValue.length();
- length++;
- } else { // tokenized field 要分词的
- final TokenStream stream;
- final TokenStream streamValue = field.tokenStreamValue();
- if (streamValue != null)
- stream = streamValue;
- else {
- // the field does not have a TokenStream,
- // so we have to obtain one from the analyzer
- final Reader reader; // find or make Reader
- final Reader readerValue = field.readerValue();
- if (readerValue != null)
- reader = readerValue;
- else {
- String stringValue = field.stringValue();
- if (stringValue == null)
- throw new IllegalArgumentException("field must have either TokenStream, String or Reader value");
- stringReader.init(stringValue);
- reader = stringReader;
- }
- // Tokenize field and add to postingTable
- stream = analyzer.reusableTokenStream(fieldInfo.name, reader);
- }
- // reset the TokenStream to the first token
- stream.reset();
- try {
- offsetEnd = offset-1;
- for(;;) {
- Token token = stream.next(localToken); //在这里分词
- if (token == null) break;
- position += (token.getPositionIncrement() - 1);
- addPosition(token);
- if (++length >= maxFieldLength) {
- if (infoStream != null)
- infoStream.println("maxFieldLength " +maxFieldLength+ " reached for field " + fieldInfo.name + ", ignoring following tokens");
- break;
- }
- }
- offset = offsetEnd+1;
- } finally {
- stream.close();
- }
- }
- boost *= field.getBoost();
- }
- static DirectoryIndexReader open(final Directory directory, final boolean closeDirectory, final IndexDeletionPolicy deletionPolicy) throws CorruptIndexException, IOException {
- return (DirectoryIndexReader) new SegmentInfos.FindSegmentsFile(directory) { //具体化FindSegmentsFile这个抽象类,它是SegmentInfos的子类
- protected Object doBody(String segmentFileName) throws CorruptIndexException, IOException {
- SegmentInfos infos = new SegmentInfos();
- infos.read(directory, segmentFileName);
- DirectoryIndexReader reader;
- if (infos.size() == 1) { // index is optimized
- reader = SegmentReader.get(infos, infos.info(0), closeDirectory);
- } else {
- reader = new MultiSegmentReader(directory, infos, closeDirectory);
- }
- reader.setDeletionPolicy(deletionPolicy);
- return reader;
- }
- }.run();//具体化后就new了个对象,然后立即调用run(), run()里面会调用这个具体化了的doBody().
- }