fts_lite 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NTAzYmM5Nzk5YTkyNmY5Mzk1Y2I0MzdhZWFhMTRmZTAzM2UyZDQxNA==
5
+ data.tar.gz: !binary |-
6
+ ODY0MzU1Y2Y1NmY3NDg3Zjk5MGYxYWVlN2JmY2Q3NTk4NjBlNTg3Nw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ODBmNzRiODM4MTFhYjE2NjNlNzQ0ZTFmYmYyZjg2YmIyMGZhMmQ3ODYwNDNm
10
+ ODRjMDA2OGM2M2M0MzE2NjQzOTIxMjRjYTU5ZmI5OWQ0MTBjMWNhZTgxMTY2
11
+ ZDBmZThlOGFkZGZjMTMzN2VjYWRmZjFmNTcwOGQwZWFmYmYyMGE=
12
+ data.tar.gz: !binary |-
13
+ YzNhMWMxNGY0YzUwOTQ2MzhmZDAxYzMwNzg3YjhkYTg4MDdiZjczZWU3OTlh
14
+ ZGNjMmIxZWY1MDMwMmQyOTZjYmIzNDcwYmY0MTgyZDNiN2FiZTgzNzI4YmU3
15
+ ZGE3ZTQ5MzQwMGExMjI5NzIxMjQ2MzMyMWNhMDAzZDEyMGMwNjM=
@@ -37,7 +37,21 @@ module FtsLite
37
37
  Index.new(path, options)
38
38
  end
39
39
  end
40
-
40
+
41
+ def sql_value(x)
42
+ if x.nil?
43
+ x
44
+ elsif x.is_a?(DateTime)
45
+ x.iso8601
46
+ elsif x.is_a?(Date)
47
+ x.iso8601
48
+ elsif x.is_a?(Time)
49
+ x.to_datetime.iso8601
50
+ else
51
+ x
52
+ end
53
+ end
54
+
41
55
  def close
42
56
  @db.close
43
57
  end
@@ -52,27 +66,32 @@ module FtsLite
52
66
  def set(docid, text, sort_value = nil)
53
67
  if (SQLITE_HAVE_FT4_REPLACE)
54
68
  @db.execute("INSERT OR REPLACE INTO #{@table_name} (docid, text, sort_value) VALUES(?, ?, ?);",
55
- [docid, @tokenizer.vector(text), sort_value])
69
+ [docid, @tokenizer.vector(text), sql_value(sort_value)])
56
70
  else
57
71
  begin
58
72
  @db.execute("INSERT INTO #{@table_name} (docid, text, sort_value) VALUES(?, ?, ?);",
59
- [docid, @tokenizer.vector(text), sort_value])
73
+ [docid, @tokenizer.vector(text), sql_value(sort_value)])
60
74
  rescue SQLite3::ConstraintException
61
75
  @db.execute("UPDATE #{@table_name} SET text = ?, sort_value = ? WHERE docid = ?;",
62
- [@tokenizer.vector(text), sort_value, docid])
76
+ [@tokenizer.vector(text), sql_value(sort_value), docid])
63
77
  end
64
78
  end
65
79
  end
66
80
  def update_sort_value(docid, sort_value)
67
81
  @db.execute("UPDATE #{@table_name} SET sort_value = ? WHERE docid = ?;",
68
- [sort_value, docid])
82
+ [sql_value(sort_value), docid])
69
83
  end
70
84
  def delete(docid)
71
85
  @db.execute("DELETE FROM #{@table_name} WHERE docid = ?;", [docid])
72
86
  end
73
87
  def search(text, options = {})
88
+ options ||= {}
74
89
  limit = options[:limit]
75
90
  order = nil
91
+ gt = nil
92
+ lt = nil
93
+ gte = nil
94
+ lte = nil
76
95
  if (options[:order])
77
96
  case options[:order].to_sym
78
97
  when :desc
@@ -81,7 +100,25 @@ module FtsLite
81
100
  order = :asc
82
101
  end
83
102
  end
103
+ if (options[:range])
104
+ gt = options[:range][:gt]
105
+ lt = options[:range][:lt]
106
+ gte = options[:range][:gte]
107
+ lte = options[:range][:lte]
108
+ end
84
109
  sql = "SELECT docid FROM #{@table_name} WHERE text MATCH ?"
110
+ if gt
111
+ sql += " AND sort_value > ? "
112
+ end
113
+ if lt
114
+ sql += " AND sort_value < ? "
115
+ end
116
+ if gte
117
+ sql += " AND sort_value >= ? "
118
+ end
119
+ if lte
120
+ sql += " AND sort_value <= ? "
121
+ end
85
122
  if (order)
86
123
  sql += sprintf(" ORDER BY sort_value %s", order == :desc ? "DESC" : "ASC")
87
124
  else
@@ -91,7 +128,8 @@ module FtsLite
91
128
  sql += sprintf(" LIMIT %d", limit)
92
129
  end
93
130
  sql += ";"
94
- @db.execute(sql, [@tokenizer.query(text, options)]).flatten
131
+ conditions = [gt, lt, gte, lte].reject{|v| v.nil?}.map{|v| sql_value(v)}
132
+ @db.execute(sql, [@tokenizer.query(text, options), conditions].flatten).flatten
95
133
  end
96
134
  def count
97
135
  @db.execute("SELECT COUNT(*) FROM #{@table_name} ;").first.first
@@ -1,3 +1,3 @@
1
1
  module FtsLite
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -195,6 +195,38 @@ class FtsLiteTest < Test::Unit::TestCase
195
195
  assert_equal db.search("あいう", :fuzzy => false).size, 0
196
196
  end
197
197
  end
198
+ def test_conditions
199
+ db = FtsLite::Index.open(DB_FILE, :tokenizer => :bigram)
200
+ db.transaction do
201
+ db.delete_all
202
+ db.set(1, "なぜナポリタンは赤いのだろうか ?", Date.new(2014, 1, 1))
203
+ db.set(2, "昼飯のスパゲティナポリタンを眺めながら、積年の疑問を考えていた。 ", Date.new(2014, 1, 2))
204
+ db.set(3, "それは「なぜナポリタンは赤いのだろうか」という問いである。", Date.new(2014, 1, 3))
205
+
206
+ assert_equal db.search("ナポリタン", :order => :asc).size, 3
207
+ assert_equal db.search("ナポリタン", :order => :asc)[0], 1
208
+ assert_equal db.search("ナポリタン", :order => :asc)[1], 2
209
+ assert_equal db.search("ナポリタン", :order => :asc)[2], 3
210
+ assert_equal db.search("ナポリタン", :order => :desc).size, 3
211
+ assert_equal db.search("ナポリタン", :order => :desc)[0], 3
212
+ assert_equal db.search("ナポリタン", :order => :desc)[1], 2
213
+ assert_equal db.search("ナポリタン", :order => :desc)[2], 1
214
+
215
+ assert_equal db.search("ナポリタン", :range => {:gt => Date.new(2014, 1, 2)}).size, 1
216
+ assert_equal db.search("ナポリタン", :range => {:gt => Date.new(2014, 1, 2)})[0], 3
217
+ assert_equal db.search("ナポリタン", :range => {:lt => Date.new(2014, 1, 2)}).size, 1
218
+ assert_equal db.search("ナポリタン", :range => {:lt => Date.new(2014, 1, 2)})[0], 1
219
+ assert_equal db.search("ナポリタン", :range => {:gte => Date.new(2014, 1, 2)}).size, 2
220
+ assert_equal db.search("ナポリタン", :order => :asc, :range => {:gte => Date.new(2014, 1, 2)})[0], 2
221
+ assert_equal db.search("ナポリタン", :order => :asc, :range => {:gte => Date.new(2014, 1, 2)})[1], 3
222
+ assert_equal db.search("ナポリタン", :range => {:lte => Date.new(2014, 1, 2)}).size, 2
223
+ assert_equal db.search("ナポリタン", :order => :asc, :range => {:lte => Date.new(2014, 1, 2)})[0], 1
224
+ assert_equal db.search("ナポリタン", :order => :asc, :range => {:lte => Date.new(2014, 1, 2)})[1], 2
225
+
226
+ assert_equal db.search("ナポリタン", :range => {:gte => Date.new(2014, 1, 2), :lt => Date.new(2014, 1, 3)}).size, 1
227
+ assert_equal db.search("ナポリタン", :range => {:gte => Date.new(2014, 1, 2), :lt => Date.new(2014, 1, 3)})[0], 2
228
+ end
229
+ end
198
230
  def test_create
199
231
  db = FtsLite::Index.open(DB_FILE)
200
232
  db.drop_table!
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fts_lite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
5
- prerelease:
4
+ version: 0.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - nagadomi
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-06-20 00:00:00.000000000 Z
11
+ date: 2014-04-14 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: bimyou_segmenter
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ! '>='
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ! '>='
28
25
  - !ruby/object:Gem::Version
@@ -30,7 +27,6 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: sqlite3-ruby
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ! '>='
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,6 @@ dependencies:
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ! '>='
44
39
  - !ruby/object:Gem::Version
@@ -64,27 +59,26 @@ files:
64
59
  - test/test_helper.rb
65
60
  homepage: https://github.com/nagadomi/fts_lite
66
61
  licenses: []
62
+ metadata: {}
67
63
  post_install_message:
68
64
  rdoc_options: []
69
65
  require_paths:
70
66
  - lib
71
67
  required_ruby_version: !ruby/object:Gem::Requirement
72
- none: false
73
68
  requirements:
74
69
  - - ! '>='
75
70
  - !ruby/object:Gem::Version
76
71
  version: '0'
77
72
  required_rubygems_version: !ruby/object:Gem::Requirement
78
- none: false
79
73
  requirements:
80
74
  - - ! '>='
81
75
  - !ruby/object:Gem::Version
82
76
  version: '0'
83
77
  requirements: []
84
78
  rubyforge_project:
85
- rubygems_version: 1.8.23
79
+ rubygems_version: 2.1.11
86
80
  signing_key:
87
- specification_version: 3
81
+ specification_version: 4
88
82
  summary: simple full text search engine
89
83
  test_files:
90
84
  - test/fts_lite_test.rb