Sha256: f1c8d821ea2b142aae1abc9c93c2117c9a243398610737db4b45a9c554e9847e

Contents?: true

Size: 1.73 KB

Versions: 1

Compression:

Stored size: 1.73 KB

Contents

# make lucene/solr a bit more rubyis
module Solrium
  include_class 'org.apache.lucene.index.TermEnum'
  include_class 'org.apache.lucene.index.Term'
  include_class 'org.apache.lucene.document.Document'
  include_class 'org.apache.lucene.document.Field'      
  include_class 'org.apache.lucene.document.Field'      
  
  include_class 'org.apache.lucene.search.Query'
  include_class 'org.apache.lucene.search.BooleanQuery'
    
  #sugar, sugar... honey, honey.
  Query.class_eval do    
    def |(other)
      boolean_query(self, other, org.apache.lucene.search.BooleanClause::Occur::SHOULD)
    end
    
    def &(other)
      boolean_query(self, other, org.apache.lucene.search.BooleanClause::Occur::MUST)
    end
    
    def boolean_query(a,b,occur)
      bq = BooleanQuery.new
      bq.add(a, occur)
      bq.add(b, occur)
      bq
    end
  end
      
  Document.class_eval do
    def [](n)
      self.getField(n)
    end
    
    def inspect
      "Document: " + to_hash.inspect
    end
    
    def to_hash
      fields.inject({}) do |h, f|
        h[f.name] = f.stringValue()
        h
      end
    end
  end
  
  Field.class_eval do
    def inspect
      "#{name}: #{to_s}"
    end
    
    def to_s
      self.stringValue()
    end
  end
  
  # lets you use Term['foo'] ==> Term.new('default', foo)
  Term.class_eval do
    def self.[](t)
      new('default', t)
    end
  end
   
  #make TermEnum a bit friendlier to use          
  class TermEnum
    include Enumerable

    def each
     while self.next()
       yield(self.term())
     end
    end

    def to_java_array
     elements = to_a
     array = java.lang.reflect.Array.newInstance(Term.java_class, elements.length)
     elements.each_with_index { |e,i| array[i] = e }
     array
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
solrium-0.1.0 lib/solr_ext.rb