github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/core/index/model/fieldInfo.go (about)

     1  package model
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  type FieldInfo struct {
     8  	// Field's name
     9  	Name string
    10  	// Internal field number
    11  	Number int32
    12  
    13  	indexed      bool
    14  	docValueType DocValuesType
    15  
    16  	// True if any document indexed term vectors
    17  	storeTermVector bool
    18  
    19  	normType      DocValuesType
    20  	omitNorms     bool
    21  	indexOptions  IndexOptions
    22  	storePayloads bool
    23  
    24  	*AttributesMixin
    25  
    26  	dvGen int64
    27  }
    28  
    29  func NewFieldInfo(name string, indexed bool, number int32,
    30  	storeTermVector, omitNorms, storePayloads bool,
    31  	indexOptions IndexOptions, docValues, normsType DocValuesType,
    32  	dvGen int64, attributes map[string]string) *FieldInfo {
    33  
    34  	assert(indexOptions > 0)
    35  	assert(indexOptions <= INDEX_OPT_DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS)
    36  
    37  	fi := &FieldInfo{Name: name, indexed: indexed, Number: number, docValueType: docValues}
    38  	fi.AttributesMixin = &AttributesMixin{attributes}
    39  	if indexed {
    40  		fi.storeTermVector = storeTermVector
    41  		fi.storePayloads = storePayloads
    42  		fi.omitNorms = omitNorms
    43  		fi.indexOptions = indexOptions
    44  		if !omitNorms {
    45  			fi.normType = normsType
    46  		}
    47  	} // for non-indexed fields, leave defaults
    48  	fi.dvGen = dvGen
    49  	fi.checkConsistency()
    50  	return fi
    51  }
    52  
    53  func (info *FieldInfo) checkConsistency() {
    54  	if !info.indexed {
    55  		assert(!info.storeTermVector)
    56  		assert(!info.storePayloads)
    57  		assert(!info.omitNorms)
    58  		assert(int(info.normType) == 0)
    59  		assert(int(info.indexOptions) == 0)
    60  	} else {
    61  		assert(int(info.indexOptions) != 0)
    62  		if info.omitNorms {
    63  			assert(int(info.normType) == 0)
    64  		}
    65  		// Cannot store payloads unless positions are indexed:
    66  		assert(int(info.indexOptions) >= int(INDEX_OPT_DOCS_AND_FREQS_AND_POSITIONS) || !info.storePayloads)
    67  	}
    68  
    69  	assert(info.dvGen == -1 || int(info.docValueType) != 0)
    70  }
    71  
    72  func (info *FieldInfo) Update(ft IndexableFieldType) {
    73  	info.update(ft.Indexed(), false, ft.OmitNorms(), false, ft.IndexOptions())
    74  }
    75  
    76  func (info *FieldInfo) update(indexed, storeTermVector, omitNorms, storePayloads bool, indexOptions IndexOptions) {
    77  	info.indexed = info.indexed || indexed // once indexed, always indexed
    78  	if indexed {                           // if updated field data is not for indexing, leave the updates out
    79  		info.storeTermVector = info.storeTermVector || storeTermVector // once vector, always vector
    80  		info.storePayloads = info.storePayloads || storePayloads
    81  		if info.omitNorms != omitNorms {
    82  			info.omitNorms = true // if one require omitNorms at least once, it remains off for life
    83  			info.normType = 0
    84  		}
    85  		if info.indexOptions != indexOptions {
    86  			if info.indexOptions == 0 {
    87  				info.indexOptions = indexOptions
    88  			} else {
    89  				// downgrade
    90  				if info.indexOptions > indexOptions {
    91  					info.indexOptions = indexOptions
    92  				}
    93  			}
    94  			if info.indexOptions < INDEX_OPT_DOCS_AND_FREQS_AND_POSITIONS {
    95  				// cannot store payloads if we don't store positions:
    96  				info.storePayloads = false
    97  			}
    98  		}
    99  	}
   100  	info.checkConsistency()
   101  }
   102  
   103  func (info *FieldInfo) SetDocValueType(v DocValuesType) {
   104  	assert2(int(info.docValueType) != 0 && info.docValueType != v,
   105  		"cannot change DocValues type from %v to %v for field '%v'",
   106  		info.docValueType, v, info.Name)
   107  	info.docValueType = v
   108  	info.checkConsistency()
   109  }
   110  
   111  /* Returns IndexOptions for the field, or 0 if the field is not indexed */
   112  func (info *FieldInfo) IndexOptions() IndexOptions { return info.indexOptions }
   113  
   114  /* Returns true if this field has any docValues. */
   115  func (info *FieldInfo) HasDocValues() bool {
   116  	return int(info.docValueType) != 0
   117  }
   118  
   119  /* Returns DocValueType of the docValues. This may be 0 if the fiel dhas no docValues. */
   120  func (info *FieldInfo) DocValuesType() DocValuesType {
   121  	return info.docValueType
   122  }
   123  
   124  func (info *FieldInfo) DocValuesGen() int64 {
   125  	return info.dvGen
   126  }
   127  
   128  /* Returns DocValuesType of the norm. This may be 0 if the field has no norms. */
   129  func (info *FieldInfo) NormType() DocValuesType {
   130  	return info.normType
   131  }
   132  
   133  func (info *FieldInfo) SetNormValueType(typ DocValuesType) {
   134  	assert2(info.normType == DocValuesType(0) || info.normType == typ,
   135  		"cannot change Norm type from %v to %v for field '%v'",
   136  		info.normType, typ, info.Name)
   137  	info.normType = typ
   138  	info.checkConsistency()
   139  }
   140  
   141  /* Returns true if norms are explicitly omitted for this field */
   142  func (info *FieldInfo) OmitsNorms() bool { return info.omitNorms }
   143  
   144  /* Returns true if this field actually has any norms. */
   145  func (info *FieldInfo) HasNorms() bool { return int(info.normType) != 0 }
   146  
   147  /* Returns true if this field is indexed. */
   148  func (info *FieldInfo) IsIndexed() bool { return info.indexed }
   149  
   150  /* Returns true if any payloads exist for this field. */
   151  func (info *FieldInfo) HasPayloads() bool { return info.storePayloads }
   152  
   153  /* Returns true if any term vectors exist for this field. */
   154  func (info *FieldInfo) HasVectors() bool { return info.storeTermVector }
   155  
   156  /*
   157  Puts a codec attribute value.
   158  
   159  This is a key-value mapping for the field that the codec can use to
   160  store additional metadata, and will be available to the codec when
   161  reading the segment via Attribute()
   162  
   163  If a value already exists ofr the field, it will be replaced with the
   164  new value.
   165  */
   166  func (fi *FieldInfo) PutAttribute(key, value string) string {
   167  	if fi.attributes == nil {
   168  		fi.attributes = make(map[string]string)
   169  	}
   170  	var v string
   171  	v, fi.attributes[key] = fi.attributes[key], value
   172  	return v
   173  }
   174  
   175  func (fi *FieldInfo) String() string {
   176  	return fmt.Sprintf("%v-%v, isIndexed=%v, docValueType=%v, hasVectors=%v, normType=%v, omitNorms=%v, indexOptions=%v, hasPayloads=%v, attributes=%v",
   177  		fi.Number, fi.Name, fi.indexed, fi.docValueType, fi.storeTermVector, fi.normType, fi.omitNorms, fi.indexOptions, fi.storePayloads, fi.attributes)
   178  }
   179  
   180  type Int32Slice []int32
   181  
   182  func (p Int32Slice) Len() int           { return len(p) }
   183  func (p Int32Slice) Less(i, j int) bool { return p[i] < p[j] }
   184  func (p Int32Slice) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
   185  
   186  // index/FieldInfo.java
   187  
   188  type IndexOptions int
   189  
   190  const (
   191  	INDEX_OPT_DOCS_ONLY                                = IndexOptions(1)
   192  	INDEX_OPT_DOCS_AND_FREQS                           = IndexOptions(2)
   193  	INDEX_OPT_DOCS_AND_FREQS_AND_POSITIONS             = IndexOptions(3)
   194  	INDEX_OPT_DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS = IndexOptions(4)
   195  )
   196  
   197  type DocValuesType int
   198  
   199  const (
   200  	DOC_VALUES_TYPE_NUMERIC        = DocValuesType(1)
   201  	DOC_VALUES_TYPE_BINARY         = DocValuesType(2)
   202  	DOC_VALUES_TYPE_SORTED         = DocValuesType(3)
   203  	DOC_VALUES_TYPE_SORTED_SET     = DocValuesType(4)
   204  	DOC_VALUES_TYPE_SORTED_NUMERIC = DocValuesType(5)
   205  )
   206  
   207  // index/IndexableFieldType.java
   208  
   209  /**
   210   * Describes the properties of a field.
   211   */
   212  type IndexableFieldType interface {
   213  	/** True if this field should be indexed (inverted) */
   214  	Indexed() bool
   215  	/** True if the field's value should be stored */
   216  	Stored() bool
   217  	/**
   218  	 * True if this field's value should be analyzed by the
   219  	 * {@link Analyzer}.
   220  	 * <p>
   221  	 * This has no effect if {@link #indexed()} returns false.
   222  	 */
   223  	Tokenized() bool
   224  	/**
   225  	 * True if this field's indexed form should be also stored
   226  	 * into term vectors.
   227  	 * <p>
   228  	 * This builds a miniature inverted-index for this field which
   229  	 * can be accessed in a document-oriented way from
   230  	 * {@link IndexReader#getTermVector(int,String)}.
   231  	 * <p>
   232  	 * This option is illegal if {@link #indexed()} returns false.
   233  	 */
   234  	StoreTermVectors() bool
   235  	/**
   236  	 * True if this field's token character offsets should also
   237  	 * be stored into term vectors.
   238  	 * <p>
   239  	 * This option is illegal if term vectors are not enabled for the field
   240  	 * ({@link #storeTermVectors()} is false)
   241  	 */
   242  	StoreTermVectorOffsets() bool
   243  
   244  	/**
   245  	 * True if this field's token positions should also be stored
   246  	 * into the term vectors.
   247  	 * <p>
   248  	 * This option is illegal if term vectors are not enabled for the field
   249  	 * ({@link #storeTermVectors()} is false).
   250  	 */
   251  	StoreTermVectorPositions() bool
   252  
   253  	/**
   254  	 * True if this field's token payloads should also be stored
   255  	 * into the term vectors.
   256  	 * <p>
   257  	 * This option is illegal if term vector positions are not enabled
   258  	 * for the field ({@link #storeTermVectors()} is false).
   259  	 */
   260  	StoreTermVectorPayloads() bool
   261  
   262  	/**
   263  	 * True if normalization values should be omitted for the field.
   264  	 * <p>
   265  	 * This saves memory, but at the expense of scoring quality (length normalization
   266  	 * will be disabled), and if you omit norms, you cannot use index-time boosts.
   267  	 */
   268  	OmitNorms() bool
   269  
   270  	/** {@link IndexOptions}, describing what should be
   271  	 * recorded into the inverted index */
   272  	IndexOptions() IndexOptions
   273  
   274  	/**
   275  	 * DocValues {@link DocValuesType}: if non-null then the field's value
   276  	 * will be indexed into docValues.
   277  	 */
   278  	DocValueType() DocValuesType
   279  }