标签:
域.fnm的文件结构:
Header,FieldsCount, <FieldName,FieldNumber, FieldBits,DocValuesBits,DocValuesGen,Attributes> FieldsCount,Footer
要了解域的元数据信息,还要了解以下几点:
同.si文件相同,.fnm同样有Lucene46FieldInfosFormat(包含Lucene46SegmentInfoReader,Lucene46FieldInfosWriter),以及FieldInfo
看下Lucene46SegmentInfoReader读取.fnm文件部分代码,Lucene46FieldInfosWriter内容差不多就不再介绍。
package org.apache.lucene.codecs.lucene46; /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.IOException; import java.util.Collections; import java.util.Map; import org.apache.lucene.codecs.CodecUtil; import org.apache.lucene.codecs.FieldInfosReader; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.FieldInfo; import org.apache.lucene.index.FieldInfos; import org.apache.lucene.index.IndexFileNames; import org.apache.lucene.index.FieldInfo.DocValuesType; import org.apache.lucene.index.FieldInfo.IndexOptions; import org.apache.lucene.store.Directory; import org.apache.lucene.store.IOContext; import org.apache.lucene.store.IndexInput; import org.apache.lucene.util.IOUtils; /** * Lucene 4.6 FieldInfos reader. * * @lucene.experimental * @see Lucene46FieldInfosFormat */ final class Lucene46FieldInfosReader extends FieldInfosReader { /** Sole constructor. */ public Lucene46FieldInfosReader() { } @Override public FieldInfos read(Directory directory, String segmentName, String segmentSuffix, IOContext context) throws IOException { final String fileName = IndexFileNames.segmentFileName(segmentName, segmentSuffix, Lucene46FieldInfosFormat.EXTENSION); IndexInput input = directory.openInput(fileName, context); boolean success = false; try { CodecUtil.checkHeader(input, Lucene46FieldInfosFormat.CODEC_NAME, Lucene46FieldInfosFormat.FORMAT_START, Lucene46FieldInfosFormat.FORMAT_CURRENT);//检查头信息,包括magic,codecname,version final int size = input.readVInt(); //read in the size 域数量 FieldInfo infos[] = new FieldInfo[size]; for (int i = 0; i < size; i++) { String name = input.readString();//域名称 final int fieldNumber = input.readVInt();//域的编号 byte bits = input.readByte();//域标志位,表示该域的特性 boolean isIndexed = (bits & Lucene46FieldInfosFormat.IS_INDEXED) != 0;// bits & 0000 0001 最低位表示是否索引 boolean storeTermVector = (bits & Lucene46FieldInfosFormat.STORE_TERMVECTOR) != 0;// bits & 0000 0010 倒数第二位表示是否存储词向量 boolean omitNorms = (bits & Lucene46FieldInfosFormat.OMIT_NORMS) != 0;// bits & 0001 0000 第四位表示是否保存标准化因子 boolean storePayloads = (bits & Lucene46FieldInfosFormat.STORE_PAYLOADS) != 0;// bits & 0010 0000 第三位表示是否存储payload final IndexOptions indexOptions; if (!isIndexed) { indexOptions = null; } else if ((bits & Lucene46FieldInfosFormat.OMIT_TERM_FREQ_AND_POSITIONS) != 0) {// bits & 0100 0000 第二位表示是否保存词频和位置信息 1为不存储
//只索引 indexOptions = IndexOptions.DOCS_ONLY; } else if ((bits & Lucene46FieldInfosFormat.OMIT_POSITIONS) != 0) {// bits & 1000 0000 最高位表是否保存位置信息 1为不存储
//索引和词频 indexOptions = IndexOptions.DOCS_AND_FREQS; } else if ((bits & Lucene46FieldInfosFormat.STORE_OFFSETS_IN_POSTINGS) != 0) {// bits & 0000 0100 是否存储偏移量
//索引、词频、位置、偏移量 indexOptions = IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS; } else {
//默认情况,索引、词频、位置 indexOptions = IndexOptions.DOCS_AND_FREQS_AND_POSITIONS; } // DV Types are packed in one byte
// docValue
byte val = input.readByte(); final DocValuesType docValuesType = getDocValuesType(input, (byte) (val & 0x0F));//低4位表示DocValus类型 final DocValuesType normsType = getDocValuesType(input, (byte) ((val >>> 4) & 0x0F));//高4位表示标准类型 final long dvGen = input.readLong();//docValues版本号 final Map<String,String> attributes = input.readStringStringMap(); infos[i] = new FieldInfo(name, isIndexed, fieldNumber, storeTermVector, omitNorms, storePayloads, indexOptions, docValuesType, normsType, Collections.unmodifiableMap(attributes)); infos[i].setDocValuesGen(dvGen); } if (input.getFilePointer() != input.length()) { throw new CorruptIndexException("did not read all bytes from file \"" + fileName + "\": read " + input.getFilePointer() + " vs size " + input.length() + " (resource: " + input + ")"); } FieldInfos fieldInfos = new FieldInfos(infos); success = true; return fieldInfos; } finally { if (success) { input.close(); } else { IOUtils.closeWhileHandlingException(input); } } } private static DocValuesType getDocValuesType(IndexInput input, byte b) throws IOException { if (b == 0) { return null; } else if (b == 1) { return DocValuesType.NUMERIC; } else if (b == 2) { return DocValuesType.BINARY; } else if (b == 3) { return DocValuesType.SORTED; } else if (b == 4) { return DocValuesType.SORTED_SET; } else { throw new CorruptIndexException("invalid docvalues byte: " + b + " (resource=" + input + ")"); } } }
(from 追风的蓝宝)
Solr标准的索引方式是反向索引,它将所有在Document里找到的term放到一起组成一个链表,而每一个term后面又跟着一个term出现过的document的链表以及出现过的次数。在上面的图中显示其原理。这是查询非常迅速,当用户查询某一个term时,已经有准备好的term到document的映射表了。
但是当涉及到sorting(排序), faceting(面搜索), and highlighting(以及高亮)逆向索引就变得不是那么高效了。比如faceting查询,首先得找出每一个document中出现的每一个term,然后使得每一个docID进行排序然后放入faceting list里面。对于Solr来说,这些是在内存中进行的,当document以及term多的时候,就会变得比较慢。
以facet查询为例:如果商品分类中field:category含有手机phone,照相机camera,电脑computer
统计每个分类下的商品数量,在solr查询中增加请求参数?q=*:*&facet=true&facet.field=category
这个查询的过程是solr的facetcomponent组件会先统计field:category这个field下所有term的在文档中出现的次数
因此在Lucene4.2里引入了DocValue,它是行导向的结构,在建索引的时候形成document到term的映射,它使得faceting, sorting, and grouping 查询更加快速。
要使用它得在schema.xml上设置:
<
field
name
=
"manu_exact"
type
=
"string"
indexed
=
"false"
stored
=
"false"
docValues
=
"true"
/>
DocValue只对一些特定的类型有效,比如:
DocValue具有以下优点以及缺点
Lucene有四个基础字段类型可以使用docvalues。目前Solr使用了其中三种:
例如,假设有3个这样的文档:
doc[0] = 1005
doc[1] = 1006
doc[2] = 1005
在这个例子中,每个文档仅需要一个bit。
例如,假设有3个这样的文档:
doc[0] = “aardvark”
doc[1] = “beaver”
doc[2] = “aardvark”
值 “aardvark” 被映射成0,”beaver”映射成1, 建立两个数据结构如下:
doc[0] = 0
doc[1] = 1
doc[2] = 0
term[0] = “aardvark”
term[1] = “beaver”
例如,假设有3个这样的文档:
doc[0] = “cat”, “aardvark”, “beaver”, “aardvark”
doc[1] =
doc[2] = “cat”
值 “aardvark” 被映射成0,”beaver”映射成1, “cat”映射成2,建立两个数据结构如下:
doc[0] = [0, 1, 2]
doc[1] = []
doc[2] = [2]
term[0] = “aardvark”
term[1] = “beaver”
term[2] = “cat”
标签:
原文地址:http://www.cnblogs.com/miniqiang/p/4425898.html