Class: SolrInputDocument

Inherits:
Object
  • Object
show all
Defined in:
lib/jruby_streaming_update_solr_server.rb

Overview

SolrInputDocument is a wrapper around the Java SolrInputDocument. In addition to the methods below, you can call the java methods directly. Common ones are:

  • ‘#clear` to empty the document

  • ‘#size` to get the number of k-v pairs

  • ‘#boost` and `#boost=` to get and set the document-level boost

Instance Method Summary collapse

Instance Method Details

#<<(fv) ⇒ Array<String>

An alternate syntax for #add.

Examples:

Add some fields

doc = SolrInputDocument.new
doc << ['title', 'Mein Kopf'] #=> ['Mein Kopf']
doc << ['title', 'My Head!']  #=> ['Mein Kopf', 'My Head!']
doc << ['author', ['Bill', 'Mike', 'Molly']] #=> ['Bill', 'Mike', 'Molly']

Parameters:

  • fv (Array<String>)

    A two-element array of Strings of the form [field, value] or [field, [value1, value2, …]]

Returns:

  • (Array<String>)

    the list of current values for the field in fv



194
195
196
# File 'lib/jruby_streaming_update_solr_server.rb', line 194

def << fv
  self.add(fv[0], fv[1])
end

#[](field) ⇒ Array<String>

Get a list of the currently-set values for the passed field

Note that this will always return either nil (not found) or an array, even of one element

Parameters:

  • field (String)

    The field whose values you want (as String)

Returns:

  • (Array<String>)

    An array of values (or nil on not found)



205
206
207
208
209
# File 'lib/jruby_streaming_update_solr_server.rb', line 205

def [] field
  f = self.get(field)
  return nil if (f == nil)
  return f.values.to_a
end

#[]=(field, value) ⇒ Undefined

Set the value(s) for the given field, destroying any values that were already in there

Note that this is destructive; see #<< to add multiple values to a field

Examples:

doc = SolrInputDocument.new
doc['id'] = 1 #=> [1]
doc['author'] = 'Mike' #=> ['Mike']
doc['author'] = 'Bill' #=> ['Bill']
doc['author'] #=> ['Bill']

Parameters:

  • field (String)

    The solr field you’re setting the value of

  • value (String, Array<String>)

    The value or array of values to set

Returns:

  • (Undefined)


226
227
228
229
# File 'lib/jruby_streaming_update_solr_server.rb', line 226

def []= field, value
  self.removeField(field)
  self.add(field, value) unless value.nil?
end

#add(field, val, boost = nil) ⇒ Undefined

Add a value to a field. Will add all elements of an array in turn

Parameters:

  • field (String)

    The field to add a value or values to

  • val (String, Numeric, Array)

    The value or array of values to add.

  • boost (Float) (defaults to: nil)

    The boost for this field

Returns:

  • (Undefined)


172
173
174
175
176
177
178
179
180
# File 'lib/jruby_streaming_update_solr_server.rb', line 172

def add(field, val, boost=nil)
  return if val == nil
  if val.is_a? Array
    val.each {|v| self.add(field, v)}
  else
    self.addField(field, val)
  end
  self.boost = boost if boost
end

#delete(key) ⇒ Array<String>

Delete a key/value pair

Parameters:

  • key (String)

    The key

Returns:

  • (Array<String>)

    The removed values (or nil)



313
314
315
316
317
# File 'lib/jruby_streaming_update_solr_server.rb', line 313

def delete key
  rv = self[key]
  self.removeField key
  return rv
end

#fieldBoost(field) ⇒ Float

get the field boost

Parameters:

  • field (String)

    The field whose boost you want

Returns:

  • (Float)

    The boost



157
158
159
160
161
162
163
164
# File 'lib/jruby_streaming_update_solr_server.rb', line 157

def fieldBoost field
  f = self.get(field)
  if f
    return f.getBoost
  else
    return nil
  end
end

#has_key?(field) ⇒ Boolean

Does this doc contain the given key?

Parameters:

  • field (String)

    The field whose presence you want to check

Returns:

  • (Boolean)

    True if the key is present



294
295
296
# File 'lib/jruby_streaming_update_solr_server.rb', line 294

def has_key? field
  return self.containsKey(field)
end

#has_value?(val) ⇒ Boolean

Does this doc have the given value?

Parameters:

  • value (String)

    to look for

Returns:

  • (Boolean)


302
303
304
305
306
307
# File 'lib/jruby_streaming_update_solr_server.rb', line 302

def has_value? val
  self.keys.each do |k|
    return true if self[k].include? val
  end
  return false
end

#keysArray<String>

Get the list of keys for this document

Returns:

  • (Array<String>)

    The list of keys



275
276
277
# File 'lib/jruby_streaming_update_solr_server.rb', line 275

def keys
  return self.keySet.to_a
end

#merge!(h) ⇒ Object Also known as: additive_merge!

Add keys and values from a hash or hash-like object to the document without removing any already-added values.

field=>value or field=>

Examples:

Merge a hash into an existing document

doc = SolrInputDocument.new
doc << ['author', 'Bill']
h = {}
h['author'] = 'Mike'
h['id'] = 1
h['copies'] = ['Grad reference', 'Long-term storage']
doc.merge! h
doc['id'] #=> 1
doc['author'] #=> ['Bill', 'Mike']
doc['copies'] #=> ['Grad reference', 'Long-term storage']

Parameters:

  • h (#each_pair)

    A set of field=>value pairs, probably in a Hash. Can be either



250
251
252
253
254
255
256
257
# File 'lib/jruby_streaming_update_solr_server.rb', line 250

def merge! h
  unless h.respond_to? :each_pair
    raise ArgumentError, "Argument must respond to #each_pair"
  end
  h.each_pair do |k,v|
    self.add(k, v)
  end
end

#oldvaluesArray<String] An array of all the values in all the keys (flattened out)

Get the values as an array

Returns:

  • (Array<String] An array of all the values in all the keys (flattened out))

    Array<String] An array of all the values in all the keys (flattened out)



282
# File 'lib/jruby_streaming_update_solr_server.rb', line 282

alias_method :oldvalues, :values

#setFieldBoost(field, boost) ⇒ Float

Boost a field. Does nothing if the field does not exist

Parameters:

  • field (String)

    The name of the field

  • boost (Float)

    The new boost

Returns:

  • (Float)

    the new boost



143
144
145
146
147
148
149
150
151
# File 'lib/jruby_streaming_update_solr_server.rb', line 143

def setFieldBoost field, boost
  f = self.get(field)
  if f
    f.setBoost(boost)
    return boost
  else
    return nil
  end
end

#to_sObject

pretty-print

Returns:

  • A string representation of the fields and values. Presumes the id is named ‘id’



263
264
265
266
267
268
269
# File 'lib/jruby_streaming_update_solr_server.rb', line 263

def to_s
  rv = []
  self.keys.sort.each do |k|
    rv << [self['id'][0], k, self[k].join('|')].join("\t")
  end
  return rv.join("\n")
end

#valuesObject



283
284
285
286
287
288
# File 'lib/jruby_streaming_update_solr_server.rb', line 283

def values
  rv = self.keys.map {|k| self[k]}
  rv.uniq!
  rv.flatten!
  return rv
end