csdn-tire 0.5

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ rdoc/
6
+ doc/
7
+ .yardoc/
8
+ coverage/
9
+ scratch/
10
+ examples/*.html
11
+ *.log
12
+ .rvmrc
13
+ .rbenv-version
14
+ tags
15
+ nbproject/*
data/.travis.yml ADDED
@@ -0,0 +1,13 @@
1
+ # ---------------------------------------------------------
2
+ # Configuration file for http://travis-ci.org/#!/karmi/tire
3
+ # ---------------------------------------------------------
4
+
5
+ script: "bundle exec rake test:unit"
6
+
7
+ rvm:
8
+ - 1.8.7
9
+ - 1.9.3
10
+ - ree
11
+
12
+ notifications:
13
+ disable: true
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in tire.gemspec
4
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2011 Karel Minarik
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,291 @@
1
+ Tire
2
+ =========
3
+ Ruby client for CSDNSearch.
4
+
5
+ Usage
6
+ -----
7
+
8
+ 配置:
9
+
10
+ ```
11
+ # 设置搜索后端节点数量:
12
+ Tire::Configuration.nodes_count(2)
13
+ ```
14
+
15
+ ```
16
+ # 设置日志:
17
+ Tire::Configuration.logger(STDOUT, :level => "debug")
18
+ ```
19
+
20
+ ```
21
+ # 设置搜索后端URL
22
+ Tire::Configuration.url("http://localhost:9200")
23
+ ```
24
+ 或者在环境变量设置:
25
+ ```
26
+ export ELASTICSEARCH_URL=http://localhost:9200
27
+ ```
28
+
29
+ 注册分片:index.regist_shard(total)
30
+ ```
31
+ [1] pry(main)> index = Tire.index("index_name")
32
+ => #<Tire::Index:0x9cd1564 @name="index_name">
33
+ [2] pry(main)> index.regist_shard(6)
34
+ # 2012-06-05 16:23:44:053 [_regist_shard] ("index_name")
35
+ #
36
+ curl -X PUT "http://192.168.6.35:9400/index_name/_shard" -d '{"cs1":[0,2,4],"cs2":[1,3,5]}'
37
+
38
+ # 2012-06-05 16:23:44:053 [200]
39
+
40
+ => true
41
+ [3] pry(main)>
42
+ ```
43
+
44
+ 查看分片信息:index.shard_info
45
+ ```
46
+ [2] pry(main)> index.shard_info
47
+ # 2012-06-05 16:25:40:748 [_regist_shard_info] ("index_name")
48
+ #
49
+ curl -X GET http://192.168.6.35:9400/index_name
50
+
51
+ # 2012-06-05 16:25:40:748 [200]
52
+
53
+ => [{"host"=>"127.0.0.1:9400", "index"=>"index_name", "shardId"=>0},
54
+ {"host"=>"127.0.0.1:9500", "index"=>"index_name", "shardId"=>1},
55
+ {"host"=>"127.0.0.1:9400", "index"=>"index_name", "shardId"=>2},
56
+ {"host"=>"127.0.0.1:9500", "index"=>"index_name", "shardId"=>3},
57
+ {"host"=>"127.0.0.1:9400", "index"=>"index_name", "shardId"=>4},
58
+ {"host"=>"127.0.0.1:9500", "index"=>"index_name", "shardId"=>5}]
59
+
60
+ ```
61
+
62
+ 删除索引:index.delete
63
+
64
+ ```
65
+ [5] pry(main)> index.delete
66
+ # 2012-06-05 17:07:02:158 [DELETE] ("index_name")
67
+ #
68
+ curl -X DELETE http://192.168.6.35:9400/index_name
69
+
70
+ # 2012-06-05 17:07:02:158 [200]
71
+
72
+ => true
73
+
74
+ ```
75
+
76
+ 创建Mapping:index.create_mapping(type, options)
77
+
78
+ ```
79
+ [4] pry(main)> mapping = {"csdn"=>
80
+ [4] pry(main)* {"_source"=>{"enabled"=>false},
81
+ [4] pry(main)* "properties"=>
82
+ [4] pry(main)* {"title"=>
83
+ [4] pry(main)* {"type"=>"string",
84
+ [4] pry(main)* "term_vector"=>"with_positions_offsets",
85
+ [4] pry(main)* "boost"=>2.0},
86
+ [4] pry(main)* "body"=>{"type"=>"string", "term_vector"=>"with_positions_offsets"},
87
+ [4] pry(main)* "username"=>{"type"=>"string", "index"=>"not_analyzed", "store"=>"no"},
88
+ [4] pry(main)* "id"=>
89
+ [4] pry(main)* {"type"=>"integer", "index"=>"not_analyzed", "include_in_all"=>false},
90
+ [4] pry(main)* "created_at"=>
91
+ [4] pry(main)* {"type"=>"integer", "index"=>"not_analyzed", "include_in_all"=>false}}}}
92
+ => {"csdn"=>
93
+ {"_source"=>{"enabled"=>false},
94
+ "properties"=>
95
+ {"title"=>
96
+ {"type"=>"string",
97
+ "term_vector"=>"with_positions_offsets",
98
+ "boost"=>2.0},
99
+ "body"=>{"type"=>"string", "term_vector"=>"with_positions_offsets"},
100
+ "username"=>{"type"=>"string", "index"=>"not_analyzed", "store"=>"no"},
101
+ "id"=>
102
+ {"type"=>"integer", "index"=>"not_analyzed", "include_in_all"=>false},
103
+ "created_at"=>
104
+ {"type"=>"integer", "index"=>"not_analyzed", "include_in_all"=>false}}}}
105
+ [6] pry(main)> index.create_mapping("csdn", mapping)
106
+ # 2012-06-06 13:22:25:504 [CREATE MAPPING] ("index_name")
107
+ #
108
+ curl -X PUT http://192.168.6.35:9400/index_name/csdn/_mapping -d '{"csdn":{"_source":{"enabled":false},"properties":{"title":{"type":"string","term_vector":"with_positions_offsets","boost":2.0},"body":{"type":"string","term_vector":"with_positions_offsets"},"username":{"type":"string","index":"not_analyzed","store":"no"},"id":{"type":"integer","index":"not_analyzed","include_in_all":false},"created_at":{"type":"integer","index":"not_analyzed","include_in_all":false}}}}'
109
+
110
+ # 2012-06-06 13:22:25:505 [200]
111
+
112
+ => true
113
+ ```
114
+
115
+ 查看Mapping:index.mapping(type)
116
+
117
+ ```
118
+ [1] pry(main)> index = Tire.index("index_name")
119
+ => #<Tire::Index:0x89f22cc @name="index_name">
120
+ [2] pry(main)> index.mapping
121
+ index.mapping
122
+ [2] pry(main)> index.mapping("csdn")
123
+ => {"csdn"=>
124
+ {"_source"=>{"enabled"=>false},
125
+ "properties"=>
126
+ {"title"=>
127
+ {"type"=>"string",
128
+ "term_vector"=>"with_positions_offsets",
129
+ "boost"=>2.0},
130
+ "body"=>{"type"=>"string", "term_vector"=>"with_positions_offsets"},
131
+ "username"=>{"type"=>"string", "index"=>"not_analyzed", "store"=>"no"},
132
+ "id"=>
133
+ {"type"=>"integer", "index"=>"not_analyzed", "include_in_all"=>false},
134
+ "created_at"=>
135
+ {"type"=>"integer", "index"=>"not_analyzed", "include_in_all"=>false}}}}
136
+ [3] pry(main)>
137
+
138
+ ```
139
+
140
+ 批量提交索引数据:index.bulk(type, doc)
141
+
142
+ ```
143
+ [14] pry(main)> doc = <<-EOF
144
+ [14] pry(main)* [
145
+ [14] pry(main)* {"title":"java 是好东西","body":"hey java","id":"1","username":"jack","created_at":2007072323},
146
+ [14] pry(main)* {"title":"this java cool","body":"hey java","id":"2","created_at":2009072323,"username":"robbin"},
147
+ [14] pry(main)* {"title":"this is java cool","body":"hey java","id":"3","created_at":2010072323,"username":"www"},
148
+ [14] pry(main)* {"title":"java is really cool","body":"hey java","id":"4","created_at":2007062323,"username":"google"},
149
+ [14] pry(main)* {"title":"this is wakak cool","body":"hey java","id":"5","created_at":2007062323,"username":"jackde"},
150
+ [14] pry(main)* {"title":"this is java cool","body":"hey java","id":"6","created_at":2007012323,"username":"jackk wa"},
151
+ [14] pry(main)* {"title":"this java really cool","body":"hey java","id":"7","created_at":2002072323,"username":"william"}
152
+ [14] pry(main)* ]
153
+ [14] pry(main)* EOF
154
+ => "[\n {\"title\":\"java 是好东西\",\"body\":\"hey java\",\"id\":\"1\",\"username\":\"jack\",\"created_at\":2007072323},\n {\"title\":\"this java cool\",\"body\":\"hey java\",\"id\":\"2\",\"created_at\":2009072323,\"username\":\"robbin\"},\n {\"title\":\"this is java cool\",\"body\":\"hey java\",\"id\":\"3\",\"created_at\":2010072323,\"username\":\"www\"},\n {\"title\":\"java is really cool\",\"body\":\"hey java\",\"id\":\"4\",\"created_at\":2007062323,\"username\":\"google\"},\n {\"title\":\"this is wakak cool\",\"body\":\"hey java\",\"id\":\"5\",\"created_at\":2007062323,\"username\":\"jackde\"},\n {\"title\":\"this is java cool\",\"body\":\"hey java\",\"id\":\"6\",\"created_at\":2007012323,\"username\":\"jackk wa\"},\n {\"title\":\"this java really cool\",\"body\":\"hey java\",\"id\":\"7\",\"created_at\":2002072323,\"username\":\"william\"}\n]\n"
155
+ [15] pry(main)> hash_doc = JSON.parse doc
156
+ => [{"title"=>"java 是好东西",
157
+ "body"=>"hey java",
158
+ "id"=>"1",
159
+ "username"=>"jack",
160
+ "created_at"=>2007072323},
161
+ {"title"=>"this java cool",
162
+ "body"=>"hey java",
163
+ "id"=>"2",
164
+ "created_at"=>2009072323,
165
+ "username"=>"robbin"},
166
+ {"title"=>"this is java cool",
167
+ "body"=>"hey java",
168
+ "id"=>"3",
169
+ "created_at"=>2010072323,
170
+ "username"=>"www"},
171
+ {"title"=>"java is really cool",
172
+ "body"=>"hey java",
173
+ "id"=>"4",
174
+ "created_at"=>2007062323,
175
+ "username"=>"google"},
176
+ {"title"=>"this is wakak cool",
177
+ "body"=>"hey java",
178
+ "id"=>"5",
179
+ "created_at"=>2007062323,
180
+ "username"=>"jackde"},
181
+ {"title"=>"this is java cool",
182
+ "body"=>"hey java",
183
+ "id"=>"6",
184
+ "created_at"=>2007012323,
185
+ "username"=>"jackk wa"},
186
+ {"title"=>"this java really cool",
187
+ "body"=>"hey java",
188
+ "id"=>"7",
189
+ "created_at"=>2002072323,
190
+ "username"=>"william"}]
191
+ [16] pry(main)> index
192
+ => #<Tire::Index:0xa809bd4
193
+ @name="index_name",
194
+ @options=
195
+ {"csdn"=>
196
+ {"_source"=>{"enabled"=>false},
197
+ "properties"=>
198
+ {"title"=>
199
+ {"type"=>"string",
200
+ "term_vector"=>"with_positions_offsets",
201
+ "boost"=>2.0},
202
+ "body"=>{"type"=>"string", "term_vector"=>"with_positions_offsets"},
203
+ "username"=>{"type"=>"string", "index"=>"not_analyzed", "store"=>"no"},
204
+ "id"=>
205
+ {"type"=>"integer", "index"=>"not_analyzed", "include_in_all"=>false},
206
+ "created_at"=>
207
+ {"type"=>"integer",
208
+ "index"=>"not_analyzed",
209
+ "include_in_all"=>false}}}},
210
+ @response=200 : {"ok":true,"acknowledged":true}>
211
+ [17] pry(main)> index.bulk("csdn", hash_doc)
212
+ # 2012-06-06 13:35:54:133 [BULK] ("index_name")
213
+ #
214
+ curl -X PUT http://192.168.6.35:9400/index_name/csdn/_pulk -d '[{"title":"java 是好东西","body":"hey java","id":"1","username":"jack","created_at":2007072323},{"title":"this java cool","body":"hey java","id":"2","created_at":2009072323,"username":"robbin"},{"title":"this is java cool","body":"hey java","id":"3","created_at":2010072323,"username":"www"},{"title":"java is really cool","body":"hey java","id":"4","created_at":2007062323,"username":"google"},{"title":"this is wakak cool","body":"hey java","id":"5","created_at":2007062323,"username":"jackde"},{"title":"this is java cool","body":"hey java","id":"6","created_at":2007012323,"username":"jackk wa"},{"title":"this java really cool","body":"hey java","id":"7","created_at":2002072323,"username":"william"}]'
215
+
216
+ # 2012-06-06 13:35:54:133 [200]
217
+
218
+ => true
219
+ ```
220
+
221
+ 持久化索引:index.flush
222
+
223
+ ```
224
+ [2] pry(main)> index.flush
225
+ # 2012-06-06 14:44:23:782 [FLUSH] ("index_name")
226
+ #
227
+ curl -X PUT http://192.168.6.35:9400/index_name/_flush
228
+
229
+ # 2012-06-06 14:44:23:782 [200]
230
+
231
+ => true
232
+ ```
233
+
234
+ 刷新索引:index.refresh
235
+
236
+ ```
237
+ [3] pry(main)> index.refresh
238
+ # 2012-06-06 14:44:29:085 [REFRESH] ("index_name")
239
+ #
240
+ curl -X PUT http://192.168.6.35:9400/index_name/_refresh
241
+
242
+ # 2012-06-06 14:44:29:085 [200]
243
+
244
+ => true
245
+ ```
246
+
247
+ 搜索:Tire.search(index, type, payload)
248
+
249
+ ```
250
+ search = Tire.search("bbs", "csdn", '{"query":{"text":{"title":"java"}},"size":10,"from":0}')
251
+ => #<Tire::Search:0xb34e5328
252
+ @indices=["bbs"],
253
+ @path="/bbs/csdn/_search",
254
+ @payload="{\"query\":{\"text\":{\"title\":\"java\"}},\"size\":10,\"from\":0}",
255
+ @types=["csdn"]>
256
+ [28] pry(main)> search.results
257
+ # 2012-06-11 14:34:37:%L [_search] (["bbs"])
258
+ #
259
+ curl -X GET http://192.168.6.35:9400/bbs/csdn/_search -d '{"query":{"text":{"title":"java"}},"size":10,"from":0}'
260
+
261
+ # 2012-06-11 14:34:37:%L [200] (N/A msec)
262
+
263
+ ```
264
+
265
+ 统计:
266
+
267
+ ```
268
+ count = Tire.count("bbs", "csdn", '{"query":{"text":{"title":"java"}},"size":10,"from":0}')
269
+ => #<Tire::Count:0xb34b2860
270
+ @indices=["bbs"],
271
+ @path="/bbs/csdn/_count",
272
+ @payload="{\"query\":{\"text\":{\"title\":\"java\"}},\"size\":10,\"from\":0}",
273
+ @types=["csdn"]>
274
+ [27] pry(main)> p count.results
275
+ # 2012-06-11 15:41:05:%L [_search] (["bbs"])
276
+ #
277
+ curl -X GET http://192.168.6.35:9400/bbs/csdn/_count -d '{"query":{"text":{"title":"java"}},"size":10,"from":0}'
278
+
279
+ # 2012-06-11 15:41:05:%L [200] (N/A msec)
280
+ #
281
+ # {
282
+ # "totalHits": 6
283
+ # }
284
+ ```
285
+ 集群状态:
286
+
287
+ ```
288
+ state = Tire.state.info
289
+
290
+ more_state = Tire.state.info(true)
291
+ ```
data/Rakefile ADDED
@@ -0,0 +1,78 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ task :default => :test
5
+
6
+ require 'rake/testtask'
7
+ Rake::TestTask.new(:test) do |test|
8
+ test.libs << 'lib' << 'test'
9
+ test.test_files = FileList['test/unit/*_test.rb', 'test/integration/*_test.rb']
10
+ test.verbose = true
11
+ # test.warning = true
12
+ end
13
+
14
+ namespace :test do
15
+ Rake::TestTask.new(:unit) do |test|
16
+ test.libs << 'lib' << 'test'
17
+ test.test_files = FileList["test/unit/*_test.rb"]
18
+ test.verbose = true
19
+ end
20
+ Rake::TestTask.new(:integration) do |test|
21
+ test.libs << 'lib' << 'test'
22
+ test.test_files = FileList["test/integration/*_test.rb"]
23
+ test.verbose = true
24
+ end
25
+ end
26
+
27
+ # Generate documentation
28
+ begin
29
+ require 'rdoc'
30
+ require 'rdoc/task'
31
+ Rake::RDocTask.new do |rdoc|
32
+ rdoc.rdoc_dir = 'rdoc'
33
+ rdoc.title = "Tire"
34
+ rdoc.rdoc_files.include('README.markdown')
35
+ rdoc.rdoc_files.include('lib/**/*.rb')
36
+ end
37
+ rescue LoadError
38
+ task :rdoc do
39
+ abort "[!] RDoc gem is not available."
40
+ end
41
+ end
42
+
43
+ # Generate coverage reports
44
+ begin
45
+ require 'rcov/rcovtask'
46
+ Rcov::RcovTask.new do |test|
47
+ test.libs << 'test'
48
+ test.rcov_opts = ['--exclude', 'gems/*']
49
+ test.pattern = 'test/**/*_test.rb'
50
+ test.verbose = true
51
+ end
52
+ rescue LoadError
53
+ task :rcov do
54
+ abort "[!] RCov gem is not available."
55
+ end
56
+ end
57
+
58
+ namespace :web do
59
+
60
+ desc "Update the Github website"
61
+ task :update => :generate do
62
+ current_branch = `git branch --no-color`.split("\n").select { |line| line =~ /^\* / }.to_s.gsub(/\* (.*)/, '\1')
63
+ (puts "Unable to determine current branch"; exit(1) ) unless current_branch
64
+ system "git checkout web"
65
+ system "cp examples/tire-dsl.html index.html"
66
+ system "git add index.html && git co -m 'Updated Tire website'"
67
+ system "git push origin web:gh-pages -f"
68
+ system "git checkout #{current_branch}"
69
+ end
70
+
71
+ desc "Generate the Rocco documentation page"
72
+ task :generate do
73
+ system "rocco examples/tire-dsl.rb"
74
+ html = File.read('examples/tire-dsl.html').gsub!(/>tire\-dsl\.rb</, '>tire.rb<')
75
+ File.open('examples/tire-dsl.html', 'w') { |f| f.write html }
76
+ system "open examples/tire-dsl.html"
77
+ end
78
+ end
data/examples/tire.rb ADDED
@@ -0,0 +1,81 @@
1
+ # -*- encoding : utf-8 -*-
2
+ index = Tire.index("bbs")
3
+ index.delete
4
+ index.regist_shard(6)
5
+
6
+ p index.shard_info
7
+
8
+ mapping = {"csdn"=> {"_source"=>{"enabled"=>false},
9
+ "properties"=>
10
+ {"title"=>
11
+ {"type"=>"string",
12
+ "term_vector"=>"with_positions_offsets",
13
+ "boost"=>2.0},
14
+ "body"=>{"type"=>"string", "term_vector"=>"with_positions_offsets"},
15
+ "username"=>{"type"=>"string", "index"=>"not_analyzed", "store"=>"no"},
16
+ "id"=>
17
+ {"type"=>"integer", "index"=>"not_analyzed", "include_in_all"=>false},
18
+ "created_at"=>
19
+ {"type"=>"integer", "index"=>"not_analyzed", "include_in_all"=>false}
20
+ }
21
+ }
22
+ }
23
+
24
+ index.create_mapping("csdn", mapping)
25
+
26
+ p index.mapping("csdn")
27
+
28
+ doc = [
29
+ {"title"=>"java 是好东西",
30
+ "body"=>"hey java",
31
+ "id"=>"1",
32
+ "username"=>"jack",
33
+ "created_at"=>2007072323},
34
+ {"title"=>"this java cool",
35
+ "body"=>"hey java",
36
+ "id"=>"2",
37
+ "created_at"=>2009072323,
38
+ "username"=>"robbin"},
39
+ {"title"=>"this is java cool",
40
+ "body"=>"hey java",
41
+ "id"=>"3",
42
+ "created_at"=>2010072323,
43
+ "username"=>"www"},
44
+ {"title"=>"java is really cool",
45
+ "body"=>"hey java",
46
+ "id"=>"4",
47
+ "created_at"=>2007062323,
48
+ "username"=>"google"},
49
+ {"title"=>"this is wakak cool",
50
+ "body"=>"hey java",
51
+ "id"=>"5",
52
+ "created_at"=>2007062323,
53
+ "username"=>"jackde"},
54
+ {"title"=>"this is java cool",
55
+ "body"=>"hey java",
56
+ "id"=>"6",
57
+ "created_at"=>2007012323,
58
+ "username"=>"jackk wa"},
59
+ {"title"=>"this java really cool",
60
+ "body"=>"hey java",
61
+ "id"=>"7",
62
+ "created_at"=>2002072323,
63
+ "username"=>"william"}
64
+ ]
65
+
66
+ index.bulk("csdn", doc)
67
+
68
+ index.flush
69
+ index.refresh
70
+
71
+ search = Tire.search("bbs", "csdn", '{"query":{"text":{"title":"java"}},"size":10,"from":0}')
72
+ p search.results
73
+
74
+ count = Tire.count("bbs", "csdn", '{"query":{"text":{"title":"java"}},"size":10,"from":0}')
75
+ p count.results
76
+
77
+ state = Tire.state.info
78
+ p state
79
+
80
+ more_state = Tire.state.info(true)
81
+ p more_state