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(client_class_name) ⇒ Queryer

Returns a new instance of Queryer.



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

def initialize(client_class_name)
  nn_object             = Object.const_get(client_class_name)
  @client_class_name    = nn_object.table_name
  @solr_settings        = nn_object::SOLR_SEVICE_CONFIG
  @expr                 = ::EasySolr::Expressions.new
  @expr.nn_custom_links = nn_object::LINKS
end

Instance Attribute Details

#client_class_nameObject

Returns the value of attribute client_class_name.



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

def client_class_name
  @client_class_name
end

#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 连接



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

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


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

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

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

#generate_scope_str(scope) ⇒ Object

Description

根据主键生成 scope 的字符串形式



77
78
79
80
81
# File 'lib/easy_solr/queryer.rb', line 77

def generate_scope_str(scope)
  break_down(@expr.nn_custom_links)

  " #{ client_class_name }.#{ mysql_pri_key } IN (?) "
end

#performObject

Description

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



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

def 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 将



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

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