rawscsi 1.0.5 → 1.1.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: 760003135e883d26c8cd64bdd89b9dfbfd4157d9
4
- data.tar.gz: a8b6715c316df621c23815f5ee39bd40e925af8e
3
+ metadata.gz: b4c20697b5268a66d2391bdf053b7ff063044234
4
+ data.tar.gz: c0e2452a4b3e3ff54f6150183be70ffee6237da9
5
5
  SHA512:
6
- metadata.gz: e3e2bae8a4208571263e67638554b9d05a0009592c2c6a3f87f3d73e065cc0f87876913a6442ce40d0c3bfbfcf5827a942618ebee75a56304441f5243b2de5cb
7
- data.tar.gz: 6ddc30ae9f1e9e2afcfa696680d05b4312a39f3e08b8c2b3552f4a55759fe847c5eb117055b5ed6918a7a3d3591877df64af0055fc0bbc77d7138e38d1b172af
6
+ metadata.gz: b0314657d7ed42473b848fa86f531c07e685191193253d2d684262a4b4411a204751a95dcea3fa54e6baea46de589caab0e12062d90cbb51500323331adc5553
7
+ data.tar.gz: e1f72c03b3aea4a3b9b8d8f67d0d86503d333ea78ea7a2e2d4417a33a53e85d81f521d3a581f86984ad8c55706fc162fab7cc61ebd2d89be0c79d1fd7bf8e432
data/README.md CHANGED
@@ -261,6 +261,23 @@ search_songs.search(q: {and: [ {artist: "Cold Play"} ],
261
261
 
262
262
  ```
263
263
 
264
+ ### Prefix Matching
265
+
266
+ Rawscsi supports prefix matching using the `prefix` key.
267
+
268
+ ```ruby
269
+ search_songs.search(q: {prefix: "To"})
270
+ => [{song_id: 54967, title: "Aenima", artist: "Tool"},
271
+ {song_id: 96566, title: "Lateralus", artist: "Tool"},
272
+ {song_id: 32356, title: "Today is the Day", artist: "Yo La Tengo"}]
273
+
274
+ # you can specify the prefix for a certain field
275
+ search_songs.search(q: {and: [{genres: "80s"},
276
+ {prefix: "Every"})
277
+ => [{song_id: 91485, title: "Everybody Wants to Rule the World", artist: "Tears for Fears"},
278
+ {song_id: 96566, title: "Everything Counts", artist: "Depeche Mode"}]
279
+ ```
280
+
264
281
  ### Pagination
265
282
 
266
283
  ```ruby
@@ -1,6 +1,10 @@
1
+ require "pry"
2
+
1
3
  module Rawscsi
2
4
  module Query
3
5
  class Compound
6
+ include Rawscsi::Stringifier::Encode
7
+
4
8
  attr_reader :query_hash
5
9
  def initialize(query_hash)
6
10
  @query_hash = query_hash
@@ -20,7 +24,7 @@ module Rawscsi
20
24
 
21
25
  private
22
26
  def query
23
- "q=" + compound_bool(query_hash[:q])
27
+ "q=" + Rawscsi::Query::Stringifier.new(query_hash[:q]).build
24
28
  end
25
29
 
26
30
  def date
@@ -62,59 +66,7 @@ module Rawscsi
62
66
  end
63
67
  "return=" + output.join(",")
64
68
  end
65
-
66
- def compound_bool(hash)
67
- if compound?(hash)
68
- stringify_compound(hash)
69
- else
70
- stringify_noncompound(hash)
71
- end
72
- end
73
-
74
- def compound?(hash)
75
- ar = hash.keys
76
- ar.include?(:and) || ar.include?(:or)
77
- end
78
-
79
- def stringify_compound(hash)
80
- bool_op = hash.keys.first
81
- ar = hash[bool_op]
82
- "(#{bool_op}" + encode(" #{bool_map(ar)}") + ")"
83
- end
84
-
85
- def stringify_noncompound(hash)
86
- if not_hash = hash[:not]
87
- "(not" + encode(" #{stringify(not_hash)}") + ")"
88
- elsif range = hash[:range]
89
- range
90
- else
91
- encode(stringify(hash))
92
- end
93
- end
94
-
95
- def bool_map(array)
96
- output = ""
97
- array.each do |pred|
98
- output << compound_bool(pred)
99
- end
100
- output
101
- end
102
-
103
- def stringify(hash)
104
- output_str = ""
105
- hash.each do |k,v|
106
- output_str << "#{k}:'#{v}'"
107
- end
108
- output_str
109
- end
110
-
111
- def encode(str)
112
- # URI and CGI.escape don't quite work here
113
- # For example, I need blank space as %20, but they encode it as +
114
- # So I have to write my own
115
- str.gsub(' ', '%20').gsub("'", '%27').gsub("[", '%5B').gsub("]",'%5D').gsub("{", '%7B').gsub("}", '%7D')
116
- end
117
- end
69
+ end
118
70
  end
119
71
  end
120
72
 
@@ -0,0 +1,31 @@
1
+ module Rawscsi
2
+ module Query
3
+ class Stringifier
4
+ attr_reader :bool_hash
5
+
6
+ def initialize(bool_hash)
7
+ @bool_hash = bool_hash
8
+ end
9
+
10
+ def build
11
+ if compound?(bool_hash)
12
+ Rawscsi::Stringifier::Compound.new(bool_hash).build
13
+ else
14
+ Rawscsi::Stringifier::Simple.new(bool_hash).build
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def compound?(value)
21
+ if value.kind_of?(Hash)
22
+ ar = value.keys
23
+ ar.include?(:and) || ar.include?(:or)
24
+ else
25
+ false
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+
@@ -0,0 +1,34 @@
1
+ module Rawscsi
2
+ module Stringifier
3
+ class Compound
4
+ include Rawscsi::Stringifier::Encode
5
+
6
+ attr_reader :bool_hash
7
+
8
+ def initialize(bool_hash)
9
+ @bool_hash = bool_hash
10
+ end
11
+
12
+ def build
13
+ bool_op = bool_hash.keys.first
14
+ ar = bool_hash[bool_op]
15
+ "(#{bool_op}" + encode(" #{bool_map(ar)}") + ")"
16
+ end
17
+
18
+ private
19
+
20
+ def bool_map(value)
21
+ output = ""
22
+ if value.kind_of?(Enumerable)
23
+ value.each do |v|
24
+ output << Rawscsi::Query::Stringifier.new(v).build
25
+ end
26
+ else
27
+ output = Rawscsi::Query::Stringifier.new(v).build
28
+ end
29
+ output
30
+ end
31
+ end
32
+ end
33
+ end
34
+
@@ -0,0 +1,13 @@
1
+ module Rawscsi
2
+ module Stringifier
3
+ module Encode
4
+ def encode(str)
5
+ # URI and CGI.escape don't quite work here
6
+ # For example, I need blank space as %20, but they encode it as +
7
+ # So I have to write my own
8
+ str.gsub(' ', '%20').gsub("'", '%27').gsub("[", '%5B').gsub("]",'%5D').gsub("{", '%7B').gsub("}", '%7D')
9
+ end
10
+ end
11
+ end
12
+ end
13
+
@@ -0,0 +1,55 @@
1
+ module Rawscsi
2
+ module Stringifier
3
+ class Simple
4
+ include Rawscsi::Stringifier::Encode
5
+
6
+ attr_reader :value
7
+
8
+ def initialize(value)
9
+ @value = value
10
+ end
11
+
12
+ def build
13
+ if value.kind_of?(Hash)
14
+ build_from_hash
15
+ else
16
+ encode(stringify(value))
17
+ end
18
+ end
19
+
20
+ private
21
+ def build_from_hash
22
+ if not_hash = value[:not]
23
+ "(not" + encode(" #{stringify(not_hash)}") + ")"
24
+ elsif pre_hash = value[:prefix]
25
+ "(prefix" + encode(" #{stringify(pre_hash, true)}") + ")"
26
+ elsif range = value[:range]
27
+ range
28
+ else
29
+ encode(stringify(value))
30
+ end
31
+ end
32
+
33
+ def stringify(value, prefix=false)
34
+ output_str = ""
35
+ if value.kind_of?(Hash)
36
+ value.each do |k,v|
37
+ output_str << kv_stringify(k, v, prefix)
38
+ end
39
+ else
40
+ output_str << "'#{value.to_s}'"
41
+ end
42
+ output_str
43
+ end
44
+
45
+ def kv_stringify(k, v, prefix=false)
46
+ if prefix
47
+ "field=#{k} '#{v}'"
48
+ else
49
+ "#{k}:'#{v}'"
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+
@@ -1,3 +1,3 @@
1
1
  module Rawscsi
2
- VERSION = "1.0.5"
2
+ VERSION = "1.1.0"
3
3
  end
data/lib/rawscsi.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  $:.unshift(File.expand_path("../", __FILE__))
2
2
 
3
+ require "pry"
4
+
3
5
  module Rawscsi
4
6
  autoload :Version, 'rawscsi/version'
5
7
  autoload :Base, 'rawscsi/base'
@@ -9,6 +11,13 @@ module Rawscsi
9
11
  module Query
10
12
  autoload :Simple, "rawscsi/query/simple"
11
13
  autoload :Compound, "rawscsi/query/compound"
14
+ autoload :Stringifier, "rawscsi/query/stringifier"
15
+ end
16
+
17
+ module Stringifier
18
+ autoload :Simple, "rawscsi/stringifier/simple"
19
+ autoload :Compound, "rawscsi/stringifier/compound"
20
+ autoload :Encode, "rawscsi/stringifier/encode"
12
21
  end
13
22
 
14
23
  module SearchHelpers
@@ -86,5 +86,19 @@ describe Rawscsi::Query::Compound do
86
86
  str = Rawscsi::Query::Compound.new(arg).build
87
87
  expect(str).to eq("q=(and%20title:%27star%20wars%27)&start=2&size=3&q.parser=structured")
88
88
  end
89
+
90
+ it "constructs a prefix query" do
91
+ arg = {:q => {:prefix => "star wars"}}
92
+ str = Rawscsi::Query::Compound.new(arg).build
93
+ expect(str).to eq("q=(prefix%20%27star%20wars%27)&q.parser=structured")
94
+ end
95
+
96
+ it "constructs a combination of conjunction and prefix query" do
97
+ arg = {:q => {:and => [{:genres => "Action"},
98
+ {:prefix => {:actor => "Stallone"}}]}}
99
+ str = Rawscsi::Query::Compound.new(arg).build
100
+
101
+ expect(str).to eq("q=(and%20genres:%27Action%27(prefix%20field=actor%20%27Stallone%27))&q.parser=structured")
102
+ end
89
103
  end
90
104
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rawscsi
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steven Li
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-13 00:00:00.000000000 Z
11
+ date: 2015-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -171,9 +171,13 @@ files:
171
171
  - lib/rawscsi/index_helpers/sdf_delete.rb
172
172
  - lib/rawscsi/query/compound.rb
173
173
  - lib/rawscsi/query/simple.rb
174
+ - lib/rawscsi/query/stringifier.rb
174
175
  - lib/rawscsi/search.rb
175
176
  - lib/rawscsi/search_helpers/results_active_record.rb
176
177
  - lib/rawscsi/search_helpers/results_hash.rb
178
+ - lib/rawscsi/stringifier/compound.rb
179
+ - lib/rawscsi/stringifier/encode.rb
180
+ - lib/rawscsi/stringifier/simple.rb
177
181
  - lib/rawscsi/version.rb
178
182
  - rawscsi.gemspec
179
183
  - spec/fixtures/vcr/index_spec/delete.yml
@@ -219,7 +223,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
219
223
  version: '0'
220
224
  requirements: []
221
225
  rubyforge_project:
222
- rubygems_version: 2.2.2
226
+ rubygems_version: 2.4.2
223
227
  signing_key:
224
228
  specification_version: 4
225
229
  summary: Adds service objects to upload and search active record models with AWS Cloud