Class: Shoes::Search

Inherits:
Object
  • Object
show all
Includes:
FTSearch
Defined in:
lib/shoes/search.rb

Overview

require 'ftsearchrt'

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Search) initialize(fields = [:uri, :body])

Returns a new instance of Search



8
9
10
11
12
13
14
15
# File 'lib/shoes/search.rb', line 8

def initialize fields = [:uri, :body]
  field_infos = FTSearch::FieldInfos.new
  fields.each do |name|
    field_infos.add_field :name => name,
      :analyzer => FTSearch::Analysis::SimpleIdentifierAnalyzer.new
  end
  @index = FTSearch::FragmentWriter.new :path => nil, :field_infos => field_infos
end

Instance Attribute Details

- (Object) index (readonly)

Returns the value of attribute index



7
8
9
# File 'lib/shoes/search.rb', line 7

def index
  @index
end

Instance Method Details

- (Object) add_document(hsh)



16
17
18
# File 'lib/shoes/search.rb', line 16

def add_document hsh
  @index.add_document hsh
end

- (Object) find_all(terms, show = 20, prob_sort = false)



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/shoes/search.rb', line 26

def find_all terms, show = 20, prob_sort = false
  h = Hash.new{|h,k| h[k] = 0}
  weights = Hash.new(1.0)
  weights[0] = 10000000   # :uri
  weights[1] = 10000000  # :body
  hits = @sa.find_all terms
  size = hits.size
  if prob_sort && size > 10000
    iterations = 50 * Math.sqrt(size)
    offsets = @sa.lazyhits_to_offsets(hits)
    weight_arr = weights.sort_by{|id,w| id}.map{|_,v| v}
    sorted = @dm.rank_offsets_probabilistic(offsets, weight_arr, iterations)
  else
    offsets = @sa.lazyhits_to_offsets(hits)
    sorted = @dm.rank_offsets(offsets, weights.sort_by{|id,w| id}.map{|_,v| v})
  end
  sorted[0..show].map do |doc_id, count|
    [@dm.document_id_to_uri(doc_id), count]
  end
end

- (Object) finish!



19
20
21
22
23
24
25
# File 'lib/shoes/search.rb', line 19

def finish!
  @index.finish!

  @ft = FulltextReader.new :io => StringIO.new(@index.fulltext_writer.data)
  @sa = SuffixArrayReader.new @ft, nil, :io => StringIO.new(@index.suffix_array_writer.data)
  @dm = DocumentMapReader.new :io => StringIO.new(@index.doc_map_writer.data)
end