Class: Searcher

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

Defined Under Namespace

Classes: Analyzers, Filters

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Searcher



4
5
6
7
8
9
10
11
12
13
# File 'lib/searcher.rb', line 4

def initialize options = {}
  @options = options

  @query = options[:query]
  @q = options[:q]

  @search_group = options[:search_group] || "pages,attachments"
  @type = options[:type]
  @comparator = options[:comparator] || "AND"
end

Instance Attribute Details

#resultsObject (readonly)

Returns the value of attribute results.



2
3
4
# File 'lib/searcher.rb', line 2

def results
  @results
end

Class Method Details

.search(options = {}) ⇒ Object



15
16
17
18
19
20
# File 'lib/searcher.rb', line 15

def self.search options = {}
  searcher = Searcher.new options
  searcher.search

  searcher
end

Instance Method Details

#results_with_hitsObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/searcher.rb', line 43

def results_with_hits 
  grouped_results = {}

  hits = @results.hits
  hits.group_by(&:_type).each do |type, values|
    klass = type.classify.constantize
    ids = values.collect{|value| value._source.id }
    grouped_results[type] = klass.find(ids).group_by(&:id)
  end

  hits.each do |hit|
    yield grouped_results[hit._type][hit._source.id].first, hit
  end
end

#searchObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/searcher.rb', line 22

def search
  q = @query

  if @q
    q = @q
  else
    q = q.split(" ").join(" #{@comparator} ")
  end

  if @type
    q = "(#{q}) AND type:#{@type}"
  end

  client = Page.__elasticsearch__.client
  query = client.search index: search_group, 
    size: 30,
    q: q

  @results = Hashie::Mash.new query['hits']
end

#search_groupObject



58
59
60
# File 'lib/searcher.rb', line 58

def search_group
  @search_group
end