easy_solr 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/easy_solr/expressions.rb +70 -0
- data/lib/easy_solr/query_item.rb +57 -0
- data/lib/easy_solr/queryer.rb +72 -0
- data/lib/easy_solr/result_item.rb +60 -0
- data/lib/easy_solr/translate.rb +67 -0
- data/lib/easy_solr.rb +8 -0
- metadata +66 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8c744f500b1cbe59b2add0bf1b26a6e1c0a7439a
|
4
|
+
data.tar.gz: f2bda1c3ab56b50d9ec333a599654f92c33e6e8d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6bfa181cf8ed35aa05ff1676ebacd02a19915ff8dbb60cad11180a05ecb833a9c3c2fd2736a01376500a2f8c6b0fbb282c3e08ed7184c92fa6e74b58b2a7aea1
|
7
|
+
data.tar.gz: 445d26b471ba9bf09f5f5f7257c354a492bcefb2d572fdec7a205f3d6e3d5c7dc5c2293658a2502fe86b1f1806d6c7bba26054d97eae10a7240986a88dfe6b59
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module EasySolr
|
3
|
+
class Expressions
|
4
|
+
attr_accessor :nn_custom_links
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@nn_solr_query_string = ' '
|
8
|
+
@nn_prefix_expression = []
|
9
|
+
@nn_params_array = []
|
10
|
+
@nn_query_string = ''
|
11
|
+
end
|
12
|
+
|
13
|
+
##
|
14
|
+
# ==== Description
|
15
|
+
#
|
16
|
+
# 拼接用户传递的参数,将其转换为Solr可以识别的
|
17
|
+
# 字符串
|
18
|
+
#
|
19
|
+
def solr_where(prefix, *params)
|
20
|
+
prefix_array = prefix.split("and")
|
21
|
+
|
22
|
+
params.each_with_index do |_key, _index|
|
23
|
+
@nn_prefix_expression << \
|
24
|
+
::EasySolr::QueryItem.new(nn_custom_links, prefix_array[_index], _key)
|
25
|
+
end
|
26
|
+
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
##
|
32
|
+
# ==== Description
|
33
|
+
#
|
34
|
+
# 生成 Solr 可以解析的字符串
|
35
|
+
#
|
36
|
+
# ==== Return
|
37
|
+
#
|
38
|
+
# 例如,将
|
39
|
+
# solr_params = expr.solr_where(" title = ? and id = ? ", '奥迪', '620460').
|
40
|
+
# solr_where(" second_editor_id = ? ", '138')
|
41
|
+
#
|
42
|
+
# 生成
|
43
|
+
#
|
44
|
+
# title: 奥迪 id: 620460 second_editor_id: 138
|
45
|
+
#
|
46
|
+
def query_string_for_solr
|
47
|
+
the_whole_solr_query_string = ' '
|
48
|
+
|
49
|
+
@nn_prefix_expression.each do |_query_item|
|
50
|
+
the_whole_solr_query_string.concat(_query_item.traslate_to_solr_str)
|
51
|
+
end
|
52
|
+
|
53
|
+
the_whole_solr_query_string
|
54
|
+
end
|
55
|
+
|
56
|
+
##
|
57
|
+
# ==== Description
|
58
|
+
#
|
59
|
+
# 每一次查询之后, 清除全部的 传入参数
|
60
|
+
#
|
61
|
+
def destroy!
|
62
|
+
@nn_solr_query_string = ' '
|
63
|
+
@nn_prefix_expression = []
|
64
|
+
@nn_params_array = []
|
65
|
+
@nn_query_string = ''
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module EasySolr
|
3
|
+
class QueryItem
|
4
|
+
|
5
|
+
attr_accessor :nn_custom_links
|
6
|
+
|
7
|
+
def initialize(custom_links, query_string, value)
|
8
|
+
@nn_solr_query_string = query_string
|
9
|
+
@nn_solr_value = value
|
10
|
+
@nn_custom_links = custom_links
|
11
|
+
end
|
12
|
+
|
13
|
+
##
|
14
|
+
# ==== Description
|
15
|
+
#
|
16
|
+
# 最小单元的转化,将MySQL语法转转换成 Solr 可识别的查询字符串
|
17
|
+
#
|
18
|
+
# 第一步,参考 ::EasySolr::Expressions#solr_where
|
19
|
+
# expr.solr_where(" title = ? and id = ? ", '奥迪', '620460')
|
20
|
+
# 转换成 "title = ?", '奥迪'
|
21
|
+
#
|
22
|
+
# 第二步
|
23
|
+
# "title = ?", '奥迪'
|
24
|
+
# 转换成 "title : 奥迪"
|
25
|
+
#
|
26
|
+
# 第三步
|
27
|
+
# "title : 奥迪"
|
28
|
+
# 转换成
|
29
|
+
# "title_texts : 奥迪"
|
30
|
+
#
|
31
|
+
# 第四步, 参考 ::EasySolr::Expressions#query_string_for_solr
|
32
|
+
# 组装所有QueryItem 生成 Solr 可用字符串
|
33
|
+
#
|
34
|
+
def traslate_to_solr_str
|
35
|
+
@nn_solr_query_string.gsub!("=", ':').gsub!("?", @nn_solr_value)
|
36
|
+
self.replace_with_custom_links
|
37
|
+
end
|
38
|
+
|
39
|
+
##
|
40
|
+
# ==== Description
|
41
|
+
#
|
42
|
+
# FIXME
|
43
|
+
# 这里使用gsub 还是有点风险的, 会导致 错误的覆盖
|
44
|
+
#
|
45
|
+
def replace_with_custom_links
|
46
|
+
nn_custom_links.each_pair do |_link, _value|
|
47
|
+
if @nn_solr_query_string.include? _link
|
48
|
+
@nn_solr_query_string.gsub!(_link, _value[:solr_name])
|
49
|
+
break
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
@nn_solr_query_string.concat(" ")
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
@@ -0,0 +1,72 @@
|
|
1
|
+
|
2
|
+
# encoding: utf-8
|
3
|
+
module EasySolr
|
4
|
+
class Queryer
|
5
|
+
include ::EasySolr::ResultItem
|
6
|
+
include ::EasySolr::Translate
|
7
|
+
|
8
|
+
attr_accessor :solr_settings
|
9
|
+
|
10
|
+
def initialize(links, r_settings)
|
11
|
+
@solr_settings = r_settings
|
12
|
+
|
13
|
+
@expr = ::EasySolr::Expressions.new
|
14
|
+
@expr.nn_custom_links = links
|
15
|
+
end
|
16
|
+
|
17
|
+
##
|
18
|
+
# ==== Description
|
19
|
+
#
|
20
|
+
# 根据所有生成的信息 执行 Solr 模糊查询
|
21
|
+
#
|
22
|
+
def solr_perform
|
23
|
+
solr_str = @expr.query_string_for_solr
|
24
|
+
|
25
|
+
solr_params = { :q => solr_str, :rows => 10 }
|
26
|
+
result = current_solr_connection.get 'select', :params => solr_params
|
27
|
+
|
28
|
+
@expr.destroy!
|
29
|
+
result_perform(result).result_items
|
30
|
+
end
|
31
|
+
|
32
|
+
##
|
33
|
+
# ==== Description
|
34
|
+
#
|
35
|
+
# 获取 Solr 连接
|
36
|
+
#
|
37
|
+
def current_solr_connection
|
38
|
+
if defined? @deal_search_connection
|
39
|
+
@deal_search_connection
|
40
|
+
else
|
41
|
+
solr_url = "#{solr_settings[:host]}:#{solr_settings[:port]}#{solr_settings[:path]}"
|
42
|
+
@deal_search_connection = RSolr.connect :url => solr_url
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
##
|
47
|
+
# ==== Description
|
48
|
+
#
|
49
|
+
# 巧妙运用 where 将
|
50
|
+
#
|
51
|
+
def where(prefix, *values)
|
52
|
+
@expr.solr_where(prefix, *values)
|
53
|
+
self
|
54
|
+
end
|
55
|
+
|
56
|
+
##
|
57
|
+
# ==== Description
|
58
|
+
#
|
59
|
+
# 根据主键ID 生成scope
|
60
|
+
#
|
61
|
+
def generate_scope(scope)
|
62
|
+
## 执行顺序
|
63
|
+
## 1、break_down
|
64
|
+
## 2、solr_pri_key || mysql_pri_key
|
65
|
+
break_down(@expr.nn_custom_links)
|
66
|
+
|
67
|
+
pri_ids = solr_perform.map{|_item| _item[solr_pri_key].to_i}
|
68
|
+
scope.where(" #{ mysql_pri_key } IN (?) ", pri_ids)
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
|
2
|
+
module EasySolr
|
3
|
+
module ResultItem
|
4
|
+
|
5
|
+
@nn_num_found = nil
|
6
|
+
@nn_items = []
|
7
|
+
|
8
|
+
##
|
9
|
+
# ==== Description
|
10
|
+
#
|
11
|
+
# 解析下面的返回值
|
12
|
+
# {
|
13
|
+
# "responseHeader"=>
|
14
|
+
# {
|
15
|
+
# "status"=>0,
|
16
|
+
# "QTime"=>62,
|
17
|
+
# "params"=>{"q"=>"title_texts: 奥迪", "rows"=>"10", "wt"=>"ruby"}
|
18
|
+
# },
|
19
|
+
# "response"=>
|
20
|
+
# { "numFound"=>171, "start"=>0,
|
21
|
+
# "docs"=>[
|
22
|
+
# {"deal_id"=>"921384", "tb_sub_tag_id_its"=>74, "begin_date_ls"=>1424448000, "nick_name_texts"=>"金思盈玩具特卖店", "type_texts"=>"Deal", "second_editor_name_texts"=>"折800运营chenfang 素素", "brand_id_its"=>9866, "tb_tag_id_its"=>7, "end_date_ls"=>1425657600, "cancel_status_its"=>1, "wangwang_texts"=>"金思盈玩具专营店", "source_type_its"=>1, "complete_status_its"=>1, "title_texts"=>"金思盈奥迪Q5遥控车 拾美奥迪Q5遥控车", "suspended_status_its"=>1, "second_editor_id_its"=>762, "candidate_deal_type_id_its"=>23, "_version_"=>1499865062339248132},
|
23
|
+
# {"deal_id"=>"921386", "tb_sub_tag_id_its"=>74, "begin_date_ls"=>1424448000, "nick_name_texts"=>"金思盈玩具特卖店", "type_texts"=>"Deal", "second_editor_name_texts"=>"折800运营chenfang 素素", "brand_id_its"=>9866, "tb_tag_id_its"=>7, "end_date_ls"=>1425657600, "cancel_status_its"=>1, "wangwang_texts"=>"金思盈玩具专营店", "source_type_its"=>1, "complete_status_its"=>1, "title_texts"=>"金思盈奥迪Q7遥控车 拾美奥迪Q7遥控车", "suspended_status_its"=>1, "second_editor_id_its"=>762, "candidate_deal_type_id_its"=>23, "_version_"=>1499865062339248133}
|
24
|
+
# ]
|
25
|
+
# }
|
26
|
+
# }
|
27
|
+
#
|
28
|
+
|
29
|
+
##
|
30
|
+
# ==== Description
|
31
|
+
#
|
32
|
+
# 获取返回值,该值为所有匹配的记录条数
|
33
|
+
#
|
34
|
+
# ==== Return
|
35
|
+
#
|
36
|
+
# 匹配记录条数
|
37
|
+
#
|
38
|
+
def num_found
|
39
|
+
@nn_num_found
|
40
|
+
end
|
41
|
+
|
42
|
+
##
|
43
|
+
# ==== Description
|
44
|
+
#
|
45
|
+
# 返回记录
|
46
|
+
#
|
47
|
+
def result_items
|
48
|
+
@nn_items
|
49
|
+
end
|
50
|
+
|
51
|
+
def result_perform(result)
|
52
|
+
@nn_num_found = result["response"]["numFound"]
|
53
|
+
@nn_items = result["response"]["docs"]
|
54
|
+
|
55
|
+
self
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module EasySolr
|
3
|
+
module Translate
|
4
|
+
|
5
|
+
@nn_mysql_primary_key = nil
|
6
|
+
@nn_solr_primary_key = nil
|
7
|
+
@pri_key = false
|
8
|
+
|
9
|
+
##
|
10
|
+
# ==== Description
|
11
|
+
#
|
12
|
+
# 获取用户配置信息中MySQL表主键
|
13
|
+
#
|
14
|
+
def mysql_pri_key
|
15
|
+
@nn_mysql_primary_key
|
16
|
+
end
|
17
|
+
|
18
|
+
##
|
19
|
+
# ==== Description
|
20
|
+
#
|
21
|
+
# 获取用户配置信息中Solr 主键
|
22
|
+
#
|
23
|
+
def solr_pri_key
|
24
|
+
@nn_solr_primary_key
|
25
|
+
end
|
26
|
+
|
27
|
+
alias solr_primary_key solr_pri_key
|
28
|
+
alias mysql_primary_key mysql_pri_key
|
29
|
+
|
30
|
+
##
|
31
|
+
# ==== Description
|
32
|
+
#
|
33
|
+
# 用户 MySQL 和 Solr 对应关系中主键是否设置
|
34
|
+
#
|
35
|
+
def pri_key_present?
|
36
|
+
self.break_down
|
37
|
+
@pri_key
|
38
|
+
end
|
39
|
+
|
40
|
+
##
|
41
|
+
# ==== Description
|
42
|
+
#
|
43
|
+
# 插接配置并
|
44
|
+
#
|
45
|
+
# ==== Attention
|
46
|
+
#
|
47
|
+
# 主键配置以第一次配置为主
|
48
|
+
# 设置
|
49
|
+
# @nn_solr_primary_key solr主键
|
50
|
+
# @nn_mysql_primary_key mysql主键
|
51
|
+
# @pri_key 主键是否存在
|
52
|
+
#
|
53
|
+
def break_down(links)
|
54
|
+
links.each_pair do |_key, _value|
|
55
|
+
if _value[:primary]
|
56
|
+
@nn_solr_primary_key = _value[:solr_name]
|
57
|
+
@nn_mysql_primary_key = _key
|
58
|
+
@pri_key = true
|
59
|
+
|
60
|
+
break
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
data/lib/easy_solr.rb
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
# $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
# $LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require File.dirname(__FILE__) + "/easy_solr/expressions"
|
4
|
+
require File.dirname(__FILE__) + "/easy_solr/query_item"
|
5
|
+
require File.dirname(__FILE__) + "/easy_solr/result_item"
|
6
|
+
require File.dirname(__FILE__) + "/easy_solr/translate"
|
7
|
+
require File.dirname(__FILE__) + "/easy_solr/queryer"
|
8
|
+
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: easy_solr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Hao Wang
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rsolr
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.0.12
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.0.12
|
27
|
+
description: EasySolr 是一个基于Solr 的检索框架
|
28
|
+
email:
|
29
|
+
- wanghao293@126.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- lib/easy_solr.rb
|
35
|
+
- lib/easy_solr/expressions.rb
|
36
|
+
- lib/easy_solr/query_item.rb
|
37
|
+
- lib/easy_solr/queryer.rb
|
38
|
+
- lib/easy_solr/result_item.rb
|
39
|
+
- lib/easy_solr/translate.rb
|
40
|
+
homepage: http://yannini.me
|
41
|
+
licenses:
|
42
|
+
- MIT
|
43
|
+
metadata: {}
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options:
|
46
|
+
- "--main"
|
47
|
+
- README.md
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
requirements: []
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 2.2.2
|
63
|
+
signing_key:
|
64
|
+
specification_version: 4
|
65
|
+
summary: EasySolr 非常棒
|
66
|
+
test_files: []
|