csdn-tire 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,56 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Tire
3
+ class Cluster
4
+ extend Utils
5
+
6
+ def self.url
7
+ "#{Configuration.url}/cluster"
8
+ end
9
+
10
+ # [{"host":"192.168.6.35","master":false,"online":false,"port":9500},{"host":"192.168.6.35","master":false,"online":true,"port":9400}]
11
+ def self.state(more = false)
12
+ desc_url = more ? "#{url}/_state?more=true" : "#{url}/_state?more=false"
13
+ response = Configuration.client.get(desc_url)
14
+ if response.success?
15
+ MultiJson.decode(response.body)
16
+ else
17
+ []
18
+ end
19
+ ensure
20
+ curl = %Q|curl -X GET #{desc_url}|
21
+ logged('CLUSTER_STATE', curl)
22
+ end
23
+
24
+ # ["news","blog","ask"]
25
+ def self.index
26
+ desc_url = "#{url}/_index"
27
+
28
+ response = Configuration.client.get(desc_url)
29
+ if response.failure?
30
+ STDERR.puts "[REQUEST FAILED] \n"
31
+ raise response.to_s
32
+ end
33
+
34
+ MultiJson.decode(response.body)
35
+ ensure
36
+ curl = %Q|curl -X GET #{desc_url}|
37
+ logged('CLUSTER INDEX', curl)
38
+ end
39
+
40
+ # {"cs2":{"host":"192.168.6.35","master":false,"online":false,"port":9500},"cs1":{"host":"192.168.6.35","master":false,"online":true,"port":9400}}
41
+ def self.host
42
+ path = "#{url}/_host"
43
+
44
+ response = Configuration.client.get(path)
45
+ if response.failure?
46
+ STDERR.puts "[REQUEST FAILED] \n"
47
+ raise response.to_s
48
+ end
49
+
50
+ MultiJson.decode(response.body)
51
+ ensure
52
+ curl = %Q|curl -X GET #{path}|
53
+ logged('CLUSTER HOST', curl)
54
+ end
55
+ end
56
+ end
data/lib/tire/dsl.rb CHANGED
@@ -18,9 +18,5 @@ module Tire
18
18
  Count.new(names, types, payload)
19
19
  end
20
20
 
21
- def state
22
- State.new
23
- end
24
-
25
21
  end
26
22
  end
data/lib/tire/index.rb CHANGED
@@ -2,7 +2,6 @@
2
2
  module Tire
3
3
  class Index
4
4
  include Utils
5
- extend Utils
6
5
 
7
6
  attr_reader :name, :response
8
7
 
@@ -10,21 +9,6 @@ module Tire
10
9
  @name = name
11
10
  end
12
11
 
13
- def self.list
14
- url = "#{Configuration.url}/cluster/_index"
15
-
16
- @response = Configuration.client.get(url)
17
- if @response.failure?
18
- STDERR.puts "[REQUEST FAILED] \n"
19
- raise @response.to_s
20
- end
21
-
22
- MultiJson.decode(@response.body)
23
- ensure
24
- curl = %Q|curl -X GET #{url}|
25
- logged('Index list', curl)
26
- end
27
-
28
12
  def url
29
13
  "#{Configuration.url}/#{@name}"
30
14
  end
data/lib/tire/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  module Tire
3
- VERSION = "0.6.1"
3
+ VERSION = "0.7.0"
4
4
  end
data/lib/tire.rb CHANGED
@@ -16,7 +16,7 @@ require 'tire/http/response'
16
16
  require 'tire/http/client'
17
17
  require 'tire/search'
18
18
  require 'tire/count'
19
- require 'tire/state'
19
+ require 'tire/cluster'
20
20
  require 'tire/click_log'
21
21
  require 'tire/index'
22
22
  require 'tire/dsl'
@@ -0,0 +1,31 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'test_helper'
3
+
4
+ module Tire
5
+
6
+ class ClusterIntegrationTest < Test::Unit::TestCase
7
+ include Test::Integration
8
+
9
+ context "cluster" do
10
+ should "get host" do
11
+ assert_kind_of Hash, Tire::Cluster.host
12
+ end
13
+
14
+ should "get index list" do
15
+ assert_kind_of Array, Tire::Cluster.index
16
+ end
17
+
18
+ should "get short state" do
19
+ assert_kind_of Array, Tire::Cluster.state
20
+ assert_kind_of Hash, Tire::Cluster.state.first
21
+ end
22
+
23
+ should "get more state" do
24
+ assert_kind_of Array, Tire::Cluster.state(true)
25
+ assert_kind_of Hash, Tire::Cluster.state(true).first
26
+ assert_not_nil Tire::Cluster.state(true).first["blog"]
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -50,12 +50,6 @@ module Tire
50
50
  setup_bulk
51
51
  assert @index.refresh
52
52
  end
53
-
54
- should "get index list" do
55
- @list = Tire::Index.list
56
- assert_kind_of Array, @list
57
- end
58
-
59
53
  end
60
54
 
61
55
  end
@@ -10,13 +10,21 @@ module Tire
10
10
  should "search results" do
11
11
  setup_bulk
12
12
  @index.refresh
13
- search = Tire.search(INDEX, TYPE, '{"query":{"text":{"title":"java"}},"size":4,"from":0}')
13
+
14
+ max_size = 4
15
+ query = "java"
16
+
17
+ search = Tire.search(INDEX, TYPE, %Q!{"query":{"text":{"title":"#{query}"}},"size":#{max_size},"from":0}!)
18
+
14
19
  results = search.results
15
20
  assert_kind_of Hash, results
16
21
  assert results["total"] > 0
17
22
  assert_kind_of Array, results["hits"]
18
- assert_equal 6, results["total"]
19
- assert_equal 4, results["hits"].size
23
+
24
+ hits_count = DOC.select{|item| item["title"] =~ /#{query}/}.size
25
+
26
+ assert_equal hits_count, results["total"]
27
+ assert_equal max_size, results["hits"].size
20
28
  end
21
29
  end
22
30
  end
data/test/test_helper.rb CHANGED
@@ -24,6 +24,59 @@ module Test::Integration
24
24
  URL = "http://192.168.6.35:9400"
25
25
  INDEX = "articles_test"
26
26
  TYPE = "type_test"
27
+ DOC = [
28
+ {"title"=>"java 是好东西",
29
+ "body"=>"hey java",
30
+ "id"=>"1",
31
+ "username"=>"jack",
32
+ "created_at"=>2007072323},
33
+ {"title"=>"this java cool",
34
+ "body"=>"hey java",
35
+ "id"=>"2",
36
+ "created_at"=>2009072323,
37
+ "username"=>"robbin"},
38
+ {"title"=>"this is java cool",
39
+ "body"=>"hey java",
40
+ "id"=>"3",
41
+ "created_at"=>2010072323,
42
+ "username"=>"www"},
43
+ {"title"=>"java is really cool",
44
+ "body"=>"hey java",
45
+ "id"=>"4",
46
+ "created_at"=>2007062323,
47
+ "username"=>"google"},
48
+ {"title"=>"this is wakak cool",
49
+ "body"=>"hey java",
50
+ "id"=>"5",
51
+ "created_at"=>2007062323,
52
+ "username"=>"jackde"},
53
+ {"title"=>"this is java cool",
54
+ "body"=>"hey java",
55
+ "id"=>"6",
56
+ "created_at"=>2007012323,
57
+ "username"=>"jackk wa"},
58
+ {"title"=>"this java really cool",
59
+ "body"=>"hey java",
60
+ "id"=>"7",
61
+ "created_at"=>2002072323,
62
+ "username"=>"william"}
63
+ ]
64
+
65
+ MAPPING = {TYPE => {"_source"=>{"enabled"=>false},
66
+ "properties"=>
67
+ {"title"=>
68
+ {"type"=>"string",
69
+ "term_vector"=>"with_positions_offsets",
70
+ "boost"=>2.0},
71
+ "body"=>{"type"=>"string", "term_vector"=>"with_positions_offsets"},
72
+ "username"=>{"type"=>"string", "index"=>"not_analyzed", "store"=>"no"},
73
+ "id"=>
74
+ {"type"=>"integer", "index"=>"not_analyzed", "include_in_all"=>false},
75
+ "created_at"=>
76
+ {"type"=>"integer", "index"=>"not_analyzed", "include_in_all"=>false}
77
+ }
78
+ }
79
+ }
27
80
 
28
81
  def setup
29
82
  Tire::Configuration.url(URL)
@@ -35,65 +88,13 @@ module Test::Integration
35
88
  end
36
89
 
37
90
  def setup_mapping
38
- mapping = {TYPE => {"_source"=>{"enabled"=>false},
39
- "properties"=>
40
- {"title"=>
41
- {"type"=>"string",
42
- "term_vector"=>"with_positions_offsets",
43
- "boost"=>2.0},
44
- "body"=>{"type"=>"string", "term_vector"=>"with_positions_offsets"},
45
- "username"=>{"type"=>"string", "index"=>"not_analyzed", "store"=>"no"},
46
- "id"=>
47
- {"type"=>"integer", "index"=>"not_analyzed", "include_in_all"=>false},
48
- "created_at"=>
49
- {"type"=>"integer", "index"=>"not_analyzed", "include_in_all"=>false}
50
- }
51
- }
52
- }
53
91
  setup_shard
54
- @index.create_mapping(TYPE, mapping)
92
+ @index.create_mapping(TYPE, MAPPING)
55
93
  end
56
94
 
57
95
  def setup_bulk
58
- doc = [
59
- {"title"=>"java 是好东西",
60
- "body"=>"hey java",
61
- "id"=>"1",
62
- "username"=>"jack",
63
- "created_at"=>2007072323},
64
- {"title"=>"this java cool",
65
- "body"=>"hey java",
66
- "id"=>"2",
67
- "created_at"=>2009072323,
68
- "username"=>"robbin"},
69
- {"title"=>"this is java cool",
70
- "body"=>"hey java",
71
- "id"=>"3",
72
- "created_at"=>2010072323,
73
- "username"=>"www"},
74
- {"title"=>"java is really cool",
75
- "body"=>"hey java",
76
- "id"=>"4",
77
- "created_at"=>2007062323,
78
- "username"=>"google"},
79
- {"title"=>"this is wakak cool",
80
- "body"=>"hey java",
81
- "id"=>"5",
82
- "created_at"=>2007062323,
83
- "username"=>"jackde"},
84
- {"title"=>"this is java cool",
85
- "body"=>"hey java",
86
- "id"=>"6",
87
- "created_at"=>2007012323,
88
- "username"=>"jackk wa"},
89
- {"title"=>"this java really cool",
90
- "body"=>"hey java",
91
- "id"=>"7",
92
- "created_at"=>2002072323,
93
- "username"=>"william"}
94
- ]
95
96
  setup_mapping
96
- @index.bulk(TYPE, doc)
97
+ @index.bulk(TYPE, DOC)
97
98
  end
98
99
 
99
100
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csdn-tire
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -222,6 +222,7 @@ files:
222
222
  - examples/tire.rb
223
223
  - lib/tire.rb
224
224
  - lib/tire/click_log.rb
225
+ - lib/tire/cluster.rb
225
226
  - lib/tire/configuration.rb
226
227
  - lib/tire/count.rb
227
228
  - lib/tire/dsl.rb
@@ -233,13 +234,12 @@ files:
233
234
  - lib/tire/rubyext/ruby_1_8.rb
234
235
  - lib/tire/rubyext/to_json.rb
235
236
  - lib/tire/search.rb
236
- - lib/tire/state.rb
237
237
  - lib/tire/utils.rb
238
238
  - lib/tire/version.rb
239
+ - test/integration/cluster_test.rb
239
240
  - test/integration/count_test.rb
240
241
  - test/integration/index_test.rb
241
242
  - test/integration/search_test.rb
242
- - test/integration/state_test.rb
243
243
  - test/test_helper.rb
244
244
  - test/unit/configuration_test.rb
245
245
  - test/unit/http_client_test.rb
data/lib/tire/state.rb DELETED
@@ -1,27 +0,0 @@
1
- # -*- encoding : utf-8 -*-
2
- module Tire
3
- class State
4
- include Utils
5
-
6
- def initialize
7
-
8
- end
9
-
10
- def url
11
- "#{Configuration.url}/cluster/_state"
12
- end
13
-
14
- def info(more = false)
15
- desc_url = more ? "#{url}?more=true" : "#{url}?more=false"
16
- @response = Configuration.client.get(desc_url)
17
- if @response.success?
18
- MultiJson.decode(@response.body)
19
- else
20
- []
21
- end
22
- ensure
23
- curl = %Q|curl -X GET #{desc_url}|
24
- logged('CLUSTER_STATE', curl)
25
- end
26
- end
27
- end
@@ -1,22 +0,0 @@
1
- # -*- encoding : utf-8 -*-
2
- require 'test_helper'
3
-
4
- module Tire
5
-
6
- class StateIntegrationTest < Test::Unit::TestCase
7
- include Test::Integration
8
-
9
- context "state" do
10
- should "get short state" do
11
- assert_kind_of Array, Tire.state.info
12
- assert_kind_of Hash, Tire.state.info.first
13
- end
14
-
15
- should "get more state" do
16
- assert_kind_of Array, Tire.state.info(true)
17
- assert_kind_of Hash, Tire.state.info(true).first
18
- assert_not_nil Tire.state.info(true).first["blog"]
19
- end
20
- end
21
- end
22
- end