Class: MartSearch::Index

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/martsearch/index.rb

Overview

Class representation for a Solr Index service used in MartSearch.

Author:

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Utils

#build_http_client, #convert_array_to_hash

Constructor Details

- (Index) initialize(config)



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/martsearch/index.rb', line 20

def initialize( config )
  @config        = config
  @url           = @config[:url]
  @primary_field = @config[:schema][:unique_key].to_sym
  @http_client   = build_http_client()
  
  # Placeholders
  @current_results       = {}
  @paginated_results     = []
  @current_results_total = 0
  @current_page          = 1
  @grouped_terms         = {}
end

Instance Attribute Details

- (Object) config (readonly)

Returns the value of attribute config



17
18
19
# File 'lib/martsearch/index.rb', line 17

def config
  @config
end

- (Object) current_page

Returns the value of attribute current_page



18
19
20
# File 'lib/martsearch/index.rb', line 18

def current_page
  @current_page
end

- (Object) current_results (readonly)

Returns the value of attribute current_results



17
18
19
# File 'lib/martsearch/index.rb', line 17

def current_results
  @current_results
end

- (Object) current_results_total

Returns the value of attribute current_results_total



18
19
20
# File 'lib/martsearch/index.rb', line 18

def current_results_total
  @current_results_total
end

- (Object) paginated_results (readonly)

Returns the value of attribute paginated_results



17
18
19
# File 'lib/martsearch/index.rb', line 17

def paginated_results
  @paginated_results
end

- (Object) primary_field (readonly)

Returns the value of attribute primary_field



17
18
19
# File 'lib/martsearch/index.rb', line 17

def primary_field
  @primary_field
end

- (Object) url

Returns the value of attribute url



18
19
20
# File 'lib/martsearch/index.rb', line 18

def url
  @url
end

Instance Method Details

- (Integer) count(query)

Function to submit a query to the search index, and return back the number of docs/results found.



144
145
146
147
148
149
# File 'lib/martsearch/index.rb', line 144

def count( query )
  MartSearch::Controller.instance().logger.debug("[MartSearch::Index] ::count - running count( '#{query}' )")
  data = index_request({ "q" => query, "rows" => 0 })
  MartSearch::Controller.instance().logger.debug("[MartSearch::Index] ::count - running count( '#{query}' ) - DONE")
  return data["response"]["numFound"]
end

- (Boolean) is_alive?

Simple heartbeat function to determine if the index service is alive.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/martsearch/index.rb', line 39

def is_alive?
  MartSearch::Controller.instance().logger.debug("[MartSearch::Index] ::is_alive? - running is_alive?()")
  clear_instance_variables
  res = @http_client.get_response( URI.parse("#{@url}/admin/ping?wt=ruby") )

  if res.code != "200"
    raise MartSearch::IndexUnavailableError, "Index HTTP error #{res.code}"
  else
    data = eval(res.body)
    if data["status"] === "OK"
      return true
    else
      raise MartSearch::IndexUnavailableError, "Index Error: #{res.body}"
    end
  end
end

- (Array) paginate_results(results)

Utility function to return paginated data results.



154
155
156
157
158
159
160
# File 'lib/martsearch/index.rb', line 154

def paginate_results( results )
  MartSearch::Controller.instance().logger.debug("[MartSearch::Index] ::paginate_results - running paginate_results()")
  results = WillPaginate::Collection.create( @current_page, @config[:docs_per_page], @current_results_total ) do |pager|
     pager.replace( results )
  end
  return results
end

- (Array) quick_search(query, page = 1)

Function to perform a query against the index and return the processed results. This is called quick_search as it bypasses all of the default martsearch post-search processing actions.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/martsearch/index.rb', line 116

def quick_search( query, page=1 )
  MartSearch::Controller.instance().logger.debug("[MartSearch::Index] ::quick_search - running quick_search( '#{query}', '#{page}' )")
  # Calculate the start page
  start_doc = 0
  if page > 1
    start_doc = ( page - 1 ) * @config[:docs_per_page]
  end

  data = index_request(
    {
      "q"     => query,
      "sort"  => @config[:sort_results_by],
      "start" => start_doc,
      "rows"  => @config[:docs_per_page]
    }
  )
  
  data.recursively_symbolize_keys!
  
  MartSearch::Controller.instance().logger.debug("[MartSearch::Index] ::quick_search - running quick_search( '#{query}', '#{page}' ) - DONE")
  return data[:response][:docs]
end

- (Hash) search(query, page = 1)

Function to submit a query to the search index and return the processed response object.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/martsearch/index.rb', line 62

def search( query, page=1 )
  MartSearch::Controller.instance().logger.debug("[MartSearch::Index] ::search - running search( '#{query}', '#{page}' )")
  clear_instance_variables
  
  # Calculate the start page
  start_doc = 0
  if page > 1
    start_doc = ( page - 1 ) * @config[:docs_per_page]
  end
  
  data = index_request(
    {
      "q"                       => query,
      "sort"                    => @config[:sort_results_by],
      "start"                   => start_doc,
      "rows"                    => @config[:docs_per_page],
      "hl"                      => true,
      "hl.fl"                   => '*',
      "hl.usePhraseHighlighter" => true,
      "hl.simple.pre"           => '<span class="highlight">',
      "hl.simple.post"          => '</span>'
    }
  )
  
  data.recursively_symbolize_keys!
  
  if start_doc == 0
    @current_page = 1
  else
    @current_page = ( start_doc / @config[:docs_per_page] ) + 1
  end
  
  data[:response][:docs].each do |doc|
    @current_results[ doc[ @primary_field ].to_sym ] = {
      :index               => doc,
      :search_explaination => data[:highlighting].stringify_keys![ doc[ @primary_field ] ]
    }
  end
  
  @current_results_total = data[:response][:numFound]
  @paginated_results     = paginate_results( data[:response][:docs] )
  
  MartSearch::Controller.instance().logger.debug("[MartSearch::Index] ::search - running search( '#{query}', '#{page}' ) - DONE")
  return @current_results
end