Class: Fiona7::FacetBuilder

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

Instance Method Summary collapse

Constructor Details

#initialize(facet_params, query, klass) ⇒ FacetBuilder



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/fiona7/facet_builder.rb', line 5

def initialize(facet_params, query, klass)
  @query        = query
  @klass        = klass

  @attribute    = facet_params[:attribute]

  @limit        = [facet_params[:limit] || 10, 100].min # default = 10, limit <= 100
  @fake_limit   = 100
  @include_objs = facet_params[:include_objs] || false

  @known_values = Set.new
  @facets       = []
  @next_facets  = []
end

Instance Method Details

#buildObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fiona7/facet_builder.rb', line 20

def build
  @facets << fetch_more_facets until limit_reached or !more_facets_available

  # adjust totals
  for index in 0.upto(@facets.length-2)
    next_facet_total = @facets[index+1].first[:total] rescue 0
    @facets[index].each do |facet|
      facet[:total] -= next_facet_total
    end
  end

  # flatten
  @facets.flatten!

  # sort
  @facets.sort_by! {|f| f[:total] }.reverse!

  # delete superfluous elements
  @facets.slice!(@limit, @facets.length)

  # add included objs
  if @include_objs
    @facets.each do |facet|
      include_objs_query = @query.dup
      include_objs_query << {field: @attribute, operator: :__in__, value: facet[:value]}
      search = VeritySearchEngine.new(@klass, include_objs_query, 0, @include_objs, @attribute, :desc)
      facet[:results] = search.results.map {|o_id| {id: o_id.to_s}}
      # this gives a true total!
      facet[:total] = search.total
    end
  else
    @facets.each do |facet|
      facet[:results] = []
    end
  end

  @facets
end