Class: Ferret::Index::FieldInfos

Inherits:
Object
  • Object
show all
Defined in:
lib/ferret/index/field_infos.rb

Overview

Access to the Field Info file that describes document fields and whether or not they are indexed. Each segment has a separate Field Info file. Objects of this class are thread-safe for multiple readers, but only one thread can be adding documents at a time, with no other reader or writer threads accessing this object.

Constant Summary collapse

NOT_A_FIELD =

-1 in java int

0xffffffff

Instance Method Summary collapse

Constructor Details

#initialize(dir = nil, name = nil) ⇒ FieldInfos

Construct a FieldInfos object using the directory and the name of the file InputStream

dir

The directory to open the InputStream from

name

The name of the file to open the InputStream from in the Directory



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ferret/index/field_infos.rb', line 17

def initialize(dir = nil, name = nil)
  @fi_array = []
  @fi_hash = {}
  if dir and dir.exists?(name)
    input = dir.open_input(name)
    begin
      read(input)
    ensure
      input.close()
    end
  end
end

Instance Method Details

#[](index) ⇒ Object

Retrieve the field_info object by either field number or field name.



97
98
99
100
101
102
103
104
105
106
# File 'lib/ferret/index/field_infos.rb', line 97

def [](index)
  if index.is_a? Integer
    if index == NOT_A_FIELD || index < 0 # < 0 is for C extensions
      return FieldInfo.new("", false, NOT_A_FIELD, false)
    end
    return @fi_array[index]
  else
    return @fi_hash[index]
  end
end

#add(name, indexed = true, store_term_vector = false, store_position = false, store_offset = false) ⇒ Object

If the field is not yet known, adds it. If it is known, checks to make sure that the indexed flag is the same as was given previously for this field. If not - marks it as being indexed. Same goes for the TermVector parameters.

name

The name of the field

indexed

true if the field is indexed

store_term_vector

true if the term vector should be stored

store_position

true if the positions should be stored

store_offset

true if the offsets should be stored



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ferret/index/field_infos.rb', line 64

def add(name,
        indexed = true,
        store_term_vector = false,
        store_position = false,
        store_offset = false)
  fi = @fi_hash[name]
  if (fi == nil)
    fi = add_internal(name, indexed, store_term_vector, store_position, store_offset)
  else
    if (fi.indexed? != indexed)
      fi.indexed = true             # once indexed, always index
    end
    if (fi.store_term_vector? != store_term_vector)
      fi.store_term_vector = true   # once vector, always vector
    end
    if (fi.store_positions? != store_position)
      fi.store_position = true # once vector, always vector
    end
    if (fi.store_offsets? != store_offset)
      fi.store_offset = true   # once vector, always vector
    end
  end
  return fi
end

#add_doc_fields(doc) ⇒ Object Also known as: <<

Automatically adds all of the fields from the document if they haven’t been added already. Or it will update the values.



32
33
34
35
36
37
38
39
40
# File 'lib/ferret/index/field_infos.rb', line 32

def add_doc_fields(doc)
  doc.all_fields.each do |field|
    add(field.name,
        field.indexed?,
        field.store_term_vector?,
        field.store_positions?,
        field.store_offsets?)
  end
end

#add_fields(names, indexed = true, store_term_vector = false, store_position = false, store_offset = false) ⇒ Object

Calls the 5 param add method to add all the names in the collection



44
45
46
47
48
49
50
51
52
# File 'lib/ferret/index/field_infos.rb', line 44

def add_fields(names, 
              indexed = true,
              store_term_vector = false,
              store_position = false,
              store_offset = false)
  names.each do |name|
    add(name, indexed, store_term_vector, store_position, store_offset)
  end
end

#eachObject

Iterate through the field_info objects



116
117
118
# File 'lib/ferret/index/field_infos.rb', line 116

def each()
  @fi_array.each() {|fi| yield(fi) }
end

#each_with_indexObject

Iterate through the field_info objects including the index



121
122
123
# File 'lib/ferret/index/field_infos.rb', line 121

def each_with_index()
  @fi_array.each_with_index() {|fi, i| yield(fi, i) }
end

#field_number(name) ⇒ Object

Returns the number of the field that goes by the field name that is passed. If there is no field of this name then -1 is returned



91
92
93
94
# File 'lib/ferret/index/field_infos.rb', line 91

def field_number(name)
  fi = @fi_hash[name]
  return fi ? fi.number : NOT_A_FIELD
end

#has_vectors?Boolean

Return true if any of the fields have store_term_vector? set to true

Returns:

  • (Boolean)


134
135
136
137
# File 'lib/ferret/index/field_infos.rb', line 134

def has_vectors?()
  @fi_array.each() { |fi| return true if fi.store_term_vector? }
  return false
end

#name(index) ⇒ Object



108
109
110
111
112
113
# File 'lib/ferret/index/field_infos.rb', line 108

def name(index)
  if index == NOT_A_FIELD || index < 0 # < 0 is for C extensions
    return ""
  end
  return self[index].name
end

#sizeObject

Get the number of field_infos in this object.

NOTE: There is a default empty field always added at the start. This may later be used to set the default values for a field.



129
130
131
# File 'lib/ferret/index/field_infos.rb', line 129

def size()
  return @fi_array.size()
end

#write_to_dir(dir, name) ⇒ Object

Write the field_infos to a file specified by name in dir.

dir

the directory to write the fieldinfos to

name

the name of the file to write to.



143
144
145
146
147
148
149
150
# File 'lib/ferret/index/field_infos.rb', line 143

def write_to_dir(dir, name)
  output = dir.create_output(name)
  begin
    write(output)
  ensure
    output.close()
  end
end