Class: EasySolr::Queryer

Inherits:
Object
  • Object
show all
Includes:
ResultItem, Translate
Defined in:
lib/easy_solr/queryer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Translate

#break_down, #mysql_pri_key, #pri_key_present?, #solr_pri_key

Methods included from ResultItem

#num_found, #result_items, #result_perform

Constructor Details

#initialize(links, r_settings) ⇒ Queryer



10
11
12
13
14
15
# File 'lib/easy_solr/queryer.rb', line 10

def initialize(links, r_settings)
  @solr_settings = r_settings

  @expr                 = ::EasySolr::Expressions.new
  @expr.nn_custom_links = links
end

Instance Attribute Details

#solr_settingsObject

Returns the value of attribute solr_settings.



8
9
10
# File 'lib/easy_solr/queryer.rb', line 8

def solr_settings
  @solr_settings
end

Instance Method Details

#current_solr_connectionObject

Description

获取 Solr 连接



37
38
39
40
41
42
43
44
# File 'lib/easy_solr/queryer.rb', line 37

def current_solr_connection
  if defined? @deal_search_connection
    @deal_search_connection
  else
    solr_url  = "#{solr_settings[:host]}:#{solr_settings[:port]}#{solr_settings[:path]}"
    @deal_search_connection = RSolr.connect :url => solr_url
  end
end

#generate_scope(scope) ⇒ Object

Description

根据主键ID 生成scope


61
62
63
64
65
66
67
68
69
# File 'lib/easy_solr/queryer.rb', line 61

def generate_scope(scope)
  ## 执行顺序
  ##  1、break_down
  ##  2、solr_pri_key || mysql_pri_key
  break_down(@expr.nn_custom_links)

  pri_ids = solr_perform.map{|_item| _item[solr_pri_key].to_i}
  scope.where(" #{ mysql_pri_key } IN (?) ", pri_ids)
end

#solr_performObject

Description

根据所有生成的信息 执行 Solr 模糊查询



22
23
24
25
26
27
28
29
30
# File 'lib/easy_solr/queryer.rb', line 22

def solr_perform
  solr_str    = @expr.query_string_for_solr

  solr_params = { :q => solr_str, :rows => 10 }
  result      = current_solr_connection.get 'select', :params => solr_params

  @expr.destroy!
  result_perform(result).result_items
end

#where(prefix, *values) ⇒ Object

Description

巧妙运用 where 将



51
52
53
54
# File 'lib/easy_solr/queryer.rb', line 51

def where(prefix, *values)
  @expr.solr_where(prefix, *values)
  self
end