aliyun_open_search 0.4.0 → 0.5.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 390f7780854a7d2530f8bd7c8081f5d9d4030346
4
- data.tar.gz: c12684f02f84a8431d80e6a87499066723bd64c4
3
+ metadata.gz: cd711e537ab1099eb436c4aba83776bc980a797b
4
+ data.tar.gz: 4058e157e170c2c151b3e1d10a0e427f5813ebbb
5
5
  SHA512:
6
- metadata.gz: a12335e502c1508d68edb4f7e8e028623c64059f7b4760f4b26ea4cf86cf8bde338c879de441cac444514a30631b3ba5cf9b1340bf5e42691302b82858e7cb16
7
- data.tar.gz: 6b16fec3c5710ee7768568511df3ae075bcadfbfce1f2baae59f29d13ec0cd749790fa85328a49e98ad6231d2fb73fda8caf634f0e8289f1db6f891ed2c74fcc
6
+ metadata.gz: e64d13027ac1abda9c8644eb886fbd7ca983035fe858301f850efe4dd090ed2b07af0b514a2a5c7766a12f326fd86de8f788d7562b56fcc393fbb41df1779e6c
7
+ data.tar.gz: a86f711a1476486297dc2b541c347b491fa5b16d69d8502bf191d85e664d9c618baa6918ae5fdd20dbaf2d668da2b5e17c4498a6a2611c50272c07e1de961d8f
data/README.md CHANGED
@@ -65,6 +65,12 @@ params = {
65
65
  "styl_name": "baoma",
66
66
  "acquirer_id": "1"
67
67
  }
68
+ },
69
+ {
70
+ "cmd": "delete",
71
+ "fields": {
72
+ "id": "3"
73
+ }
68
74
  }
69
75
  ]
70
76
  }
@@ -90,6 +96,7 @@ params = {
90
96
  }
91
97
  # 更多例子
92
98
 
99
+ # 1.
93
100
  # params = {
94
101
  # query: [
95
102
  # [
@@ -107,6 +114,25 @@ params = {
107
114
  # fetch_fields: :id
108
115
  # }
109
116
 
117
+ # 2.
118
+ # start = per_page * (current_page - 1)
119
+ # query = [
120
+ # "default:'#{@value}'",
121
+ # "configuration:'#{@value}'",
122
+ # "user_info:'#{@value}'",
123
+ # "authority_roles:'#{@value}'"
124
+ # ].join(" OR ")
125
+
126
+ # query = "query=(" + query + ") AND cars_company_id:'#{@company_id}'"
127
+
128
+ # params = {
129
+ # query: [
130
+ # query,
131
+ # "config=start:#{start},hit:#{@per_page}"
132
+ # ],
133
+ # fetch_fields: :id
134
+ # }
135
+
110
136
  AliyunOpenSearch::Search.new("test", "test2", "test3").execute(params)
111
137
 
112
138
  ```
@@ -136,11 +162,42 @@ AliyunOpenSearch::Search.new("test", "test2", "test3").execute(params)
136
162
  }
137
163
 
138
164
  AliyunOpenSearch::Base.signature(
139
- AliyunOpenSearch::Base.new.basic_params.merge!(custom_params)
165
+ AliyunOpenSearch::Base.basic_params.merge!(custom_params)
140
166
  )
141
167
 
142
168
  ```
143
169
 
170
+ ### 检索
171
+
172
+ ```ruby
173
+ require "aliyun_open_search"
174
+
175
+ scan_service = AliyunOpenSearch::Scan.new("test")
176
+ scan_service = AliyunOpenSearch::Scan.new(["test", "test2"])
177
+
178
+ # AliyunOpenSearch::Scan.new(
179
+ # ["test", "test2"],
180
+
181
+ # 配置:
182
+ # query: "query=default:'abc'", # 默认所有
183
+ # hit: "400", # 默认500, 范围 0~500
184
+ # scroll: "1w",
185
+ # # 用来表示scroll请求的有效期,默认时间单位为ms, 也可以用1m表示1min
186
+ # # 支持的时间单位包括:w=Week, d=Day, h=Hour, m=minute, s=second
187
+ # format: "xml" # 默认 JSON
188
+ # )
189
+
190
+ scan_service.execute # 获取第一页
191
+ scan_service.result # 获取结果
192
+
193
+ # 下一页
194
+ scan_service.execute # 获取第二页
195
+ scan_service.result # 获取结果
196
+
197
+
198
+ ```
199
+
200
+
144
201
  ### 测试
145
202
  ##### 大部分时候, 我们并不需要真正的发出请求, 需要mock掉
146
203
 
@@ -10,14 +10,9 @@ module AliyunOpenSearch
10
10
  end
11
11
 
12
12
  def initialize
13
- @basic_params = {
14
- "Version" => "v2",
15
- "AccessKeyId" => ENV.fetch("ACCESS_KEY_ID"),
16
- "SignatureMethod" => "HMAC-SHA1",
17
- "Timestamp" => Time.now.utc.iso8601,
18
- "SignatureVersion" => "1.0",
19
- "SignatureNonce" => signature_nonce
20
- }
13
+ @basic_params = Base.basic_params
14
+
15
+ @base_url = "#{ENV["OPEN_SEARCH_HOST"]}/search"
21
16
  end
22
17
 
23
18
  def uri(special_base_url = nil, params)
@@ -56,18 +51,27 @@ module AliyunOpenSearch
56
51
  ).strip
57
52
  end
58
53
 
59
- private
54
+ def self.basic_params
55
+ {
56
+ "Version" => "v2",
57
+ "AccessKeyId" => ENV.fetch("ACCESS_KEY_ID"),
58
+ "SignatureMethod" => "HMAC-SHA1",
59
+ "Timestamp" => Time.now.utc.iso8601,
60
+ "SignatureVersion" => "1.0",
61
+ "SignatureNonce" => signature_nonce
62
+ }
63
+ end
60
64
 
61
- def signature_nonce
65
+ def self.signature_nonce
62
66
  # 用户在不同请求间要使用不同的随机数值,建议使用13位毫秒时间戳+4位随机数
63
67
  (Time.now.to_f.round(3) * 1000).to_i.to_s + (1000..9999).to_a.sample.to_s
64
68
  end
65
69
 
66
- def self.format_params(params)
70
+ def self.format_params(method = :get, params)
67
71
  {}.tap do |hash|
68
72
  params.map do |key, value|
69
73
  hash[key.to_s] = if value.is_a?(Array)
70
- value.join("&&")
74
+ method == :get ? value.join("&&") : JSON.generate(value)
71
75
  else
72
76
  value.to_s
73
77
  end
@@ -77,12 +81,12 @@ module AliyunOpenSearch
77
81
 
78
82
  def self.escape(str)
79
83
  CGI.escape(str).gsub(/\!/, "%21")
80
- .gsub(/\'/, "%27")
81
- .gsub(/\(/, "%28")
82
- .gsub(/\)/, "%29")
83
- .gsub(/\*/, "%2A")
84
- .gsub(/\+/, "%20")
85
- .gsub(/%7E/, "~")
84
+ .gsub(/\'/, "%27")
85
+ .gsub(/\(/, "%28")
86
+ .gsub(/\)/, "%29")
87
+ .gsub(/\*/, "%2A")
88
+ .gsub(/\+/, "%20")
89
+ .gsub(/%7E/, "~")
86
90
  end
87
91
  end
88
92
  end
@@ -0,0 +1,65 @@
1
+ module AliyunOpenSearch
2
+ class Scan < Base
3
+ attr_reader :request_id, :scroll_id, :scroll_id, :custom_params, :result
4
+
5
+ def initialize(index_names, options = {})
6
+ super()
7
+
8
+ @index_names = index_names.is_a?(Array) ? index_names.join(";") : index_names
9
+ @scroll = options[:scroll] || "1m"
10
+
11
+ @custom_params = {
12
+ query: [
13
+ options[:query] || "query=''",
14
+ "config=hit:#{options[:hit] || 500},format:#{options[:format] || "json"}"
15
+ ]
16
+ }
17
+
18
+ setup
19
+ end
20
+
21
+ def setup
22
+ params = basic_params.merge(
23
+ Base.format_params(
24
+ custom_params.merge(
25
+ "index_name" => @index_names,
26
+ # "config" => "config=hit:#{@hit},format:#{@format}",
27
+ "scroll" => @scroll,
28
+ "search_type" => "scan"
29
+ )
30
+ )
31
+ )
32
+
33
+ send_request(params)
34
+ end
35
+
36
+ def execute
37
+ params = Base.new.basic_params.merge(
38
+ Base.format_params(
39
+ custom_params.merge(
40
+ "index_name" => @index_names,
41
+ # "config" => "config=hit:#{@hit},format:#{@format}",
42
+ "scroll" => @scroll,
43
+ "scroll_id" => @scroll_id
44
+ )
45
+ )
46
+ )
47
+
48
+ send_request(params)
49
+ end
50
+
51
+ private
52
+
53
+ def send_request(params)
54
+ @result = Net::HTTP.get(uri(params.merge("Signature" => Search.signature(params))))
55
+ # 为了保持与其他方法返回值一致
56
+
57
+ result = JSON.parse(@result)
58
+
59
+ @request_id = result["request_id"]
60
+ @scroll_id = result["result"]["scroll_id"]
61
+
62
+ self
63
+ end
64
+ end
65
+ end
@@ -6,7 +6,6 @@ module AliyunOpenSearch
6
6
  super()
7
7
 
8
8
  @index_names = index_names.join(";")
9
- @base_url = "#{ENV["OPEN_SEARCH_HOST"]}/search"
10
9
  end
11
10
 
12
11
  def execute(custom_params)
@@ -11,7 +11,7 @@ module AliyunOpenSearch
11
11
  end
12
12
 
13
13
  def execute(custom_params)
14
- formatted_custom_params = Base.format_params(custom_params)
14
+ formatted_custom_params = Base.format_params(:post, custom_params)
15
15
 
16
16
  params_with_signature = basic_params.merge(
17
17
  "Signature" => Syncs.signature(basic_params.merge(formatted_custom_params))
@@ -1,3 +1,3 @@
1
1
  module AliyunOpenSearch
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -2,6 +2,7 @@ require "aliyun_open_search/version"
2
2
  require "aliyun_open_search/base"
3
3
  require "aliyun_open_search/syncs"
4
4
  require "aliyun_open_search/search"
5
+ require "aliyun_open_search/scan"
5
6
 
6
7
  module AliyunOpenSearch
7
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aliyun_open_search
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - lcp
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-08-12 00:00:00.000000000 Z
11
+ date: 2015-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -103,6 +103,7 @@ files:
103
103
  - config/application.example.yml
104
104
  - lib/aliyun_open_search.rb
105
105
  - lib/aliyun_open_search/base.rb
106
+ - lib/aliyun_open_search/scan.rb
106
107
  - lib/aliyun_open_search/search.rb
107
108
  - lib/aliyun_open_search/syncs.rb
108
109
  - lib/aliyun_open_search/version.rb