Sha256: 3a7df857680f01f21c73dcd7be364abe056188538cecba02b4a79167a0706886

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

require 'java'

require File.join(File.dirname(__FILE__), 'solr_ext')

module Solrium
  module Queries
    
    include_class 'org.apache.lucene.queryParser.QueryParser'      
    include_class 'org.apache.lucene.search.MultiPhraseQuery'
    include_class 'org.apache.lucene.search.WildcardTermEnum'
    
    include_class 'org.apache.lucene.search.BooleanQuery'
    include_class 'org.apache.lucene.search.WildcardQuery'
    include_class 'org.apache.lucene.search.TermQuery'
            
      
    # convenience search method
    def search(q, max=10, &block)      
      q = q.is_a?(org.apache.lucene.search.Query) ? q : parse(q.to_s)
       
      block ||= lambda do |doc, score, searcher|       
        [doc, score]
      end
         
      with_searcher do |searcher|  
        searcher.search(q, nil, max).scoreDocs.map do |sd|
          block.call(searcher.doc(sd.doc), sd.score, searcher)
        end
      end
    end
    
    def parse(q, default='default')
      QueryParser.new(default, analyzer).parse(q)
    end
        
    #a wildcard phrase query
    def wildcard_pq(*terms)      
      return MultiPhraseQuery.new if terms.empty?
            
      if terms.size == 1
        #only one term, do simple term+wildcard query
        bq = BooleanQuery.new
        bq.add(TermQuery.new(Term[terms[0]]), org.apache.lucene.search.BooleanClause::Occur::SHOULD)
        bq.add(WildcardQuery.new(Term[terms[0] + "*"]), org.apache.lucene.search.BooleanClause::Occur::SHOULD)
        return bq
      end
        
      mpq = MultiPhraseQuery.new
        
      terms[0..-2].each { |t| mpq.add(Term[t]) }

      with_searcher do |s|
        completions = WildcardTermEnum.new(s.reader, Term["#{terms[-1]}*"]).to_java_array
        return MultiPhraseQuery.new if completions.length == 0
        mpq.add(completions) 
      end
      mpq
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
jberkel-solrium-0.1.0 lib/queries.rb
solrium-0.1.0 lib/queries.rb