tantiny-in-memory 1.0.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 +7 -0
- data/CHANGELOG.md +8 -0
- data/Cargo.toml +20 -0
- data/LICENSE +21 -0
- data/README.md +339 -0
- data/bin/console +64 -0
- data/bin/setup +6 -0
- data/ext/Rakefile +10 -0
- data/lib/tantiny/errors.rb +38 -0
- data/lib/tantiny/helpers.rb +19 -0
- data/lib/tantiny/index.rb +165 -0
- data/lib/tantiny/query.rb +165 -0
- data/lib/tantiny/schema.rb +53 -0
- data/lib/tantiny/tokenizer.rb +28 -0
- data/lib/tantiny/version.rb +5 -0
- data/lib/tantiny.rb +27 -0
- data/lib/tantiny.so +0 -0
- data/sig/tantiny/errors.rbs +20 -0
- data/sig/tantiny/helpers.rbs +8 -0
- data/sig/tantiny/index.rbs +103 -0
- data/sig/tantiny/query.rbs +135 -0
- data/sig/tantiny/schema.rbs +26 -0
- data/sig/tantiny/tokenizer.rbs +25 -0
- data/sig/tantiny/version.rbs +3 -0
- data/sig/tantiny.rbs +5 -0
- data/src/helpers.rs +202 -0
- data/src/index.rs +286 -0
- data/src/lib.rs +14 -0
- data/src/query.rs +260 -0
- data/src/tokenizer.rs +94 -0
- metadata +148 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7c209016dc2484903b6d27851d551a6e1f71a4bbc52ed54bc5b46f0bd567842d
|
4
|
+
data.tar.gz: 4586683870e8036ae55c09c5971807e0b870fe9bd9712aa98172f4d5ea72a502
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 42d3d96336441ed0c4ef5fae15e7b52a1b26e8e241700c3079e7ebbe4063b4774b7071d924610b278a78e01b3c002abbfd6aa6e7beb8edc1b623a27bfb7f9a15
|
7
|
+
data.tar.gz: c1dbaf41a82cf7ddf9c5213def71bec96bdf9cf98231c2c6cf85e0ca78fc9164e5dcf88bc59bfbc2625df23ce92cf9aca4a5fa4fa877759ed4a6a5ade209fa17
|
data/CHANGELOG.md
ADDED
data/Cargo.toml
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
[package]
|
2
|
+
name = "tantiny"
|
3
|
+
version = "1.0.0" # {x-release-please-version}
|
4
|
+
edition = "2021"
|
5
|
+
authors = ["Alexander Baygeldin"]
|
6
|
+
repository = "https://github.com/baygeldin/tantiny"
|
7
|
+
|
8
|
+
[lib]
|
9
|
+
crate-type = ["cdylib"]
|
10
|
+
|
11
|
+
[dependencies]
|
12
|
+
rutie = "0.8"
|
13
|
+
tantivy = "0.16"
|
14
|
+
lazy_static = "1.4"
|
15
|
+
paste = "1.0"
|
16
|
+
|
17
|
+
[package.metadata.thermite]
|
18
|
+
github_releases = true
|
19
|
+
github_release_type = "latest"
|
20
|
+
git_tag_regex = "^v(\\d+\\.\\d+\\.\\d+)$"
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2022 Alexander Baygeldin
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,339 @@
|
|
1
|
+
[](https://github.com/baygeldin/tantiny/actions/workflows/build.yml)
|
2
|
+
[](https://rubygems.org/gems/tantiny)
|
3
|
+
[](https://codeclimate.com/github/baygeldin/tantiny/maintainability)
|
4
|
+
[](https://codeclimate.com/github/baygeldin/tantiny/test_coverage)
|
5
|
+
|
6
|
+
# Tantiny In Memory
|
7
|
+
|
8
|
+
Need a fast full-text search for your Ruby script, but Solr and Elasticsearch are an overkill? 😏
|
9
|
+
|
10
|
+
You're in the right place. **Tantiny** is a minimalistic full-text search library for Ruby based on [Tanti**v**y](https://github.com/quickwit-oss/tantivy) (an awesome alternative to Apache Lucene written in Rust). It's great for cases when your task at hand requires a full-text search, but configuring a full-blown distributed search engine would take more time than the task itself. And even if you already use such an engine in your project (which is highly likely, actually), it still might be easier to just use Tantiny instead because unlike Solr and Elasticsearch it doesn't need *anything* to work (no separate server or process or whatever), it's purely embeddable. So, when you find yourself in a situation when using your search engine of choice would be tricky/inconvinient or would require additional setup you can always revert back to a quick and dirty solution that is nontheless flexible and fast.
|
11
|
+
|
12
|
+
Tantiny is not exactly Ruby bindings to Tantivy, but it tries to be close. The main philosophy is to provide low-level access to Tantivy's inverted index, but with a nice Ruby-esque API, sensible defaults, and additional functionality sprinkled on top.
|
13
|
+
|
14
|
+
Take a look at the most basic example:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
index = Tantiny::Index.new("/path/to/index") { text :description }
|
18
|
+
|
19
|
+
index << { id: 1, description: "Hello World!" }
|
20
|
+
index << { id: 2, description: "What's up?" }
|
21
|
+
index << { id: 3, description: "Goodbye World!" }
|
22
|
+
|
23
|
+
index.reload
|
24
|
+
|
25
|
+
index.search("world") # 1, 3
|
26
|
+
```
|
27
|
+
|
28
|
+
## Installation
|
29
|
+
|
30
|
+
Add this line to your application's Gemfile:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
gem "tantiny"
|
34
|
+
```
|
35
|
+
|
36
|
+
And then execute:
|
37
|
+
|
38
|
+
$ bundle install
|
39
|
+
|
40
|
+
Or install it yourself as:
|
41
|
+
|
42
|
+
$ gem install tantiny
|
43
|
+
|
44
|
+
You don't **have to** have Rust installed on your system since Tantiny will try to download the pre-compiled binaries hosted on GitHub releases during the installation. However, if no pre-compiled binaries were found for your system (which is a combination of platform, architecture, and Ruby version) you will need to [install Rust](https://www.rust-lang.org/tools/install) first.
|
45
|
+
|
46
|
+
⚠️ **IMPORTANT** ⚠️
|
47
|
+
|
48
|
+
Please, make sure to specify the minor version when declaring dependency on `tantiny`. The API is a subject to change, and until it reaches `1.0.0` a bump in the minor version will most likely signify a breaking change.
|
49
|
+
|
50
|
+
## Defining the index
|
51
|
+
|
52
|
+
You have to specify a path to where the index would be stored and a block that defines the schema:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
Tantiny::Index.new "/tmp/index" do
|
56
|
+
id :imdb_id
|
57
|
+
facet :category
|
58
|
+
string :title
|
59
|
+
text :description
|
60
|
+
integer :duration
|
61
|
+
double :rating
|
62
|
+
date :release_date
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
66
|
+
Here are the descriptions for every field type:
|
67
|
+
|
68
|
+
| Type | Description |
|
69
|
+
| --- | --- |
|
70
|
+
| id | Specifies where documents' ids are stored (defaults to `:id`). |
|
71
|
+
| facet | Fields with values like `/animals/birds` (i.e. hierarchial categories). |
|
72
|
+
| string | Fields with text that are **not** tokenized. |
|
73
|
+
| text | Fields with text that are tokenized by the specified tokenizer. |
|
74
|
+
| integer | Fields with integer values. |
|
75
|
+
| double | Fields with float values. |
|
76
|
+
| date | Fields with either `DateTime` type or something that converts to it. |
|
77
|
+
|
78
|
+
## Managing documents
|
79
|
+
|
80
|
+
You can feed the index any kind of object that has methods specified in your schema, but plain hashes also work:
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
rio_bravo = OpenStruct.new(
|
84
|
+
imdb_id: "tt0053221",
|
85
|
+
type: '/western/US',
|
86
|
+
title: "Rio Bravo",
|
87
|
+
description: "A small-town sheriff enlists a drunk, a kid and an old man to help him fight off a ruthless cattle baron.",
|
88
|
+
duration: 141,
|
89
|
+
rating: 8.0,
|
90
|
+
release_date: Date.parse("March 18, 1959")
|
91
|
+
)
|
92
|
+
|
93
|
+
index << rio_bravo
|
94
|
+
|
95
|
+
hanabi = {
|
96
|
+
imdb_id: "tt0119250",
|
97
|
+
type: "/crime/Japan",
|
98
|
+
title: "Hana-bi",
|
99
|
+
description: "Nishi leaves the police in the face of harrowing personal and professional difficulties. Spiraling into depression, he makes questionable decisions.",
|
100
|
+
duration: 103,
|
101
|
+
rating: 7.7,
|
102
|
+
release_date: Date.parse("December 1, 1998")
|
103
|
+
}
|
104
|
+
|
105
|
+
index << hanabi
|
106
|
+
|
107
|
+
brother = {
|
108
|
+
imdb_id: "tt0118767",
|
109
|
+
type: "/crime/Russia",
|
110
|
+
title: "Brother",
|
111
|
+
description: "An ex-soldier with a personal honor code enters the family crime business in St. Petersburg, Russia.",
|
112
|
+
duration: 99,
|
113
|
+
rating: 7.9,
|
114
|
+
release_date: Date.parse("December 12, 1997")
|
115
|
+
}
|
116
|
+
|
117
|
+
index << brother
|
118
|
+
```
|
119
|
+
|
120
|
+
In order to update the document just add it again (as long as the id is the same):
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
rio_bravo.rating = 10.0
|
124
|
+
index << rio_bravo
|
125
|
+
```
|
126
|
+
|
127
|
+
You can also delete it if you want:
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
index.delete(rio_bravo.imdb_id)
|
131
|
+
```
|
132
|
+
|
133
|
+
### Transactions
|
134
|
+
|
135
|
+
If you need to perform multiple writing operations (i.e. more than one) you should always use `transaction`:
|
136
|
+
|
137
|
+
```ruby
|
138
|
+
index.transaction do
|
139
|
+
index << rio_bravo
|
140
|
+
index << hanabi
|
141
|
+
index << brother
|
142
|
+
end
|
143
|
+
```
|
144
|
+
|
145
|
+
Transactions group changes and [commit](https://docs.rs/tantivy/latest/tantivy/struct.IndexWriter.html#method.commit) them to the index in one go. This is *dramatically* more efficient than performing these changes one by one. In fact, all writing operations (i.e. `<<` and `delete`) are wrapped in a transaction implicitly when you call them outside of a transaction, so calling `<<` 10 times outside of a transaction is the same thing as performing 10 separate transactions.
|
146
|
+
|
147
|
+
### Concurrency and thread-safety
|
148
|
+
|
149
|
+
Tantiny is thread-safe meaning that you can safely share a single instance of the index between threads. You can also spawn separate processes that could write to and read from the same index. However, while reading from the index should be parallel, writing to it is **not**. Whenever you call `transaction` or any other operation that modify the index (i.e. `<<` and `delete`) it will lock the index for the duration of the operation or wait for another process or thread to release the lock. The only exception to this is when there is another process with an index with an exclusive writer running somewhere in which case the methods that modify the index will fail immediately.
|
150
|
+
|
151
|
+
Thus, it's best to have a single writer process and many reader processes if you want to avoid blocking calls. The proper way to do this is to set `exclusive_writer` to `true` when initializing the index:
|
152
|
+
|
153
|
+
```ruby
|
154
|
+
index = Tantiny::Index.new("/path/to/index", exclusive_writer: true) {}
|
155
|
+
```
|
156
|
+
|
157
|
+
This way the [index writer](https://docs.rs/tantivy/latest/tantivy/struct.IndexWriter.html) will only be acquired once which means the memory for it and indexing threads will only be allocated once as well. Otherwise a new index writer is acquired every time you perform a writing operation.
|
158
|
+
|
159
|
+
## Searching
|
160
|
+
|
161
|
+
Make sure that your index is up-to-date by reloading it first:
|
162
|
+
|
163
|
+
```ruby
|
164
|
+
index.reload
|
165
|
+
```
|
166
|
+
|
167
|
+
And search it (finally!):
|
168
|
+
|
169
|
+
```ruby
|
170
|
+
index.search("a drunk, a kid, and an old man")
|
171
|
+
```
|
172
|
+
|
173
|
+
By default it will return ids of 10 best matching documents, but you can customize it:
|
174
|
+
|
175
|
+
```ruby
|
176
|
+
index.search("a drunk, a kid, and an old man", limit: 100)
|
177
|
+
```
|
178
|
+
|
179
|
+
You may wonder, how exactly does it conduct the search? Well, the default behavior is to use `smart_query` search (see below for details) over all `text` fields defined in your schema. So, you can pass the parameters that the `smart_query` accepts right here:
|
180
|
+
|
181
|
+
```ruby
|
182
|
+
index.search("a dlunk, a kib, and an olt mab", fuzzy_distance: 1)
|
183
|
+
```
|
184
|
+
|
185
|
+
However, you can customize it by composing your own query out of basic building blocks:
|
186
|
+
|
187
|
+
```ruby
|
188
|
+
popular_movies = index.range_query(:rating, 8.0..10.0)
|
189
|
+
about_sheriffs = index.term_query(:description, "sheriff")
|
190
|
+
crime_movies = index.facet_query(:cetegory, "/crime")
|
191
|
+
long_ass_movies = index.range_query(:duration, 180..9999)
|
192
|
+
something_flashy = index.smart_query(:description, "bourgeoisie")
|
193
|
+
|
194
|
+
index.search((popular_movies & about_sheriffs) | (crime_movies & !long_ass_movies) | something_flashy)
|
195
|
+
```
|
196
|
+
|
197
|
+
I know, weird taste! But pretty cool, huh? Take a look at all the available queries below.
|
198
|
+
|
199
|
+
### Supported queries
|
200
|
+
|
201
|
+
| Query | Behavior |
|
202
|
+
| --- | --- |
|
203
|
+
| all_query | Returns all indexed documents. |
|
204
|
+
| empty_query | Returns exactly nothing (used internally). |
|
205
|
+
| term_query | Documents that contain the specified term. |
|
206
|
+
| fuzzy_term_query | Documents that contain the specified term within a Levenshtein distance. |
|
207
|
+
| phrase_query | Documents that contain the specified sequence of terms. |
|
208
|
+
| regex_query | Documents that contain a term that matches the specified regex. |
|
209
|
+
| prefix_query | Documents that contain a term with the specified prefix. |
|
210
|
+
| range_query | Documents that with an `integer`, `double` or `date` field within the specified range. |
|
211
|
+
| facet_query | Documents that belong to the specified category. |
|
212
|
+
| smart_query | A combination of `term_query`, `fuzzy_term_query` and `prefix_query`. |
|
213
|
+
|
214
|
+
Take a look at the [signatures file](https://github.com/baygeldin/tantiny/blob/main/sig/tantiny/query.rbs) to see what parameters do queries accept.
|
215
|
+
|
216
|
+
### Searching on multiple fields
|
217
|
+
|
218
|
+
All queries can search on multuple fields (except for `facet_query` because it doesn't make sense there).
|
219
|
+
|
220
|
+
So, the following query:
|
221
|
+
|
222
|
+
```ruby
|
223
|
+
index.term_query(%i[title description], "hello")
|
224
|
+
```
|
225
|
+
|
226
|
+
Is equivalent to:
|
227
|
+
|
228
|
+
```ruby
|
229
|
+
index.term_query(:title, "hello") | index.term_query(:description, "hello")
|
230
|
+
```
|
231
|
+
|
232
|
+
### Boosting queries
|
233
|
+
|
234
|
+
All queries support the `boost` parameter that allows to bump documents position in the search:
|
235
|
+
|
236
|
+
```ruby
|
237
|
+
about_cowboys = index.term_query(:description, "cowboy", boost: 2.0)
|
238
|
+
about_samurai = index.term_query(:description, "samurai") # sorry, Musashi...
|
239
|
+
|
240
|
+
index.search(about_cowboys | about_samurai)
|
241
|
+
```
|
242
|
+
|
243
|
+
### `smart_query` behavior
|
244
|
+
|
245
|
+
The `smart_query` search will extract terms from your query string using the respective field tokenizers and search the index for documents that contain those terms via the `term_query`. If the `fuzzy_distance` parameter is specified it will use the `fuzzy_term_query`. Also, it allows the last term to be unfinished by using the `prefix_query`.
|
246
|
+
|
247
|
+
So, the following query:
|
248
|
+
|
249
|
+
```ruby
|
250
|
+
index.smart_query(%i[en_text ru_text], "dollars рубли eur", fuzzy_distance: 1)
|
251
|
+
```
|
252
|
+
|
253
|
+
Is equivalent to:
|
254
|
+
|
255
|
+
```ruby
|
256
|
+
t1_en = index.fuzzy_term_query(:en_text, "dollar")
|
257
|
+
t2_en = index.fuzzy_term_query(:en_text, "рубли")
|
258
|
+
t3_en = index.fuzzy_term_query(:en_text, "eur")
|
259
|
+
t3_prefix_en = index.prefix_query(:en_text, "eur")
|
260
|
+
|
261
|
+
t1_ru = index.fuzzy_term_query(:ru_text, "dollars")
|
262
|
+
t2_ru = index.fuzzy_term_query(:ru_text, "рубл")
|
263
|
+
t3_ru = index.fuzzy_term_query(:ru_text, "eur")
|
264
|
+
t3_prefix_ru = index.prefix_query(:ru_text, "eur")
|
265
|
+
|
266
|
+
(t1_en & t2_en & (t3_en | t3_prefix_en)) | (t1_ru & t2_ru & (t3_ru | t3_prefix_ru))
|
267
|
+
```
|
268
|
+
|
269
|
+
Notice how words "dollars" and "рубли" are stemmed differently depending on the field we are searching. This is assuming we have `en_text` and `ru_text` fields in our schema that use English and Russian stemmer tokenizers respectively.
|
270
|
+
|
271
|
+
### About `regex_query`
|
272
|
+
|
273
|
+
The `regex_query` accepts the regex pattern, but it has to be a [Rust regex](https://docs.rs/regex/latest/regex/#syntax), not a Ruby `Regexp`. So, instead of `index.regex_query(:description, /hel[lp]/)` you need to use `index.regex_query(:description, "hel[lp]")`. As a side note, the `regex_query` is pretty fast because it uses the [fst crate](https://github.com/BurntSushi/fst) internally.
|
274
|
+
|
275
|
+
## Tokenizers
|
276
|
+
|
277
|
+
So, we've mentioned tokenizers more than once already. What are they?
|
278
|
+
|
279
|
+
Tokenizers is what Tantivy uses to chop your text onto terms to build an inverted index. Then you can search the index by these terms. It's an important concept to understand so that you don't get confused when `index.term_query(:description, "Hello")` returns nothing because `Hello` isn't a term, but `hello` is. You have to extract the terms from the query before searching the index. Currently, only `smart_query` does that for you. Also, the only field type that is tokenized is `text`, so for `string` fields you should use the exact match (i.e. `index.term_query(:title, "Hello")`).
|
280
|
+
|
281
|
+
### Specifying the tokenizer
|
282
|
+
|
283
|
+
By default the `simple` tokenizer is used, but you can specify the desired tokenizer globally via index options or locally via field specific options:
|
284
|
+
|
285
|
+
```ruby
|
286
|
+
en_stemmer = Tantiny::Tokenizer.new(:stemmer)
|
287
|
+
ru_stemmer = Tantiny::Tokenizer.new(:stemmer, language: :ru)
|
288
|
+
|
289
|
+
Tantiny::Index.new "/tmp/index", tokenizer: en_stemmer do
|
290
|
+
text :description_en
|
291
|
+
text :description_ru, tokenizer: ru_stemmer
|
292
|
+
end
|
293
|
+
```
|
294
|
+
|
295
|
+
### Simple tokenizer
|
296
|
+
|
297
|
+
Simple tokenizer chops the text on punctuation and whitespaces, removes long tokens, and lowercases the text.
|
298
|
+
|
299
|
+
```ruby
|
300
|
+
tokenizer = Tantiny::Tokenizer.new(:simple)
|
301
|
+
tokenizer.terms("Hello World!") # ["hello", "world"]
|
302
|
+
```
|
303
|
+
|
304
|
+
### Stemmer tokenizer
|
305
|
+
|
306
|
+
Stemmer tokenizers is exactly like simple tokenizer, but with additional stemming according to the specified language (defaults to English).
|
307
|
+
|
308
|
+
```ruby
|
309
|
+
tokenizer = Tantiny::Tokenizer.new(:stemmer, language: :ru)
|
310
|
+
tokenizer.terms("Привет миру сему!") # ["привет", "мир", "сем"]
|
311
|
+
```
|
312
|
+
|
313
|
+
Take a look at the [source](https://github.com/baygeldin/tantiny/blob/main/src/helpers.rs) to see what languages are supported.
|
314
|
+
|
315
|
+
### Ngram tokenizer
|
316
|
+
|
317
|
+
Ngram tokenizer chops your text onto ngrams of specified size.
|
318
|
+
|
319
|
+
```ruby
|
320
|
+
tokenizer = Tantiny::Tokenizer.new(:ngram, min: 5, max: 10, prefix_only: true)
|
321
|
+
tokenizer.terms("Morrowind") # ["Morro", "Morrow", "Morrowi", "Morrowin", "Morrowind"]
|
322
|
+
```
|
323
|
+
## Retrieving documents
|
324
|
+
|
325
|
+
You may have noticed that `search` method returns only documents ids. This is by design. The documents themselves are **not** stored in the index. Tantiny is a minimalistic library, so it tries to keep things simple. If you need to retrieve a full document, use a key-value store like Redis alongside.
|
326
|
+
|
327
|
+
## Development
|
328
|
+
|
329
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake build` to build native extensions, and then `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
330
|
+
|
331
|
+
We use [conventional commits](https://www.conventionalcommits.org) to automatically generate the CHANGELOG, bump the semantic version, and to publish and release the gem. All you need to do is stick to the convention and [CI will take care of everything else](https://github.com/baygeldin/tantiny/blob/main/.github/workflows/release.yml) for you.
|
332
|
+
|
333
|
+
## Contributing
|
334
|
+
|
335
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/baygeldin/tantiny.
|
336
|
+
|
337
|
+
## License
|
338
|
+
|
339
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/bin/console
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "pry"
|
6
|
+
|
7
|
+
require "tantiny"
|
8
|
+
|
9
|
+
path = File.join(__dir__, "../tmp")
|
10
|
+
|
11
|
+
options = {
|
12
|
+
tokenizer: Tantiny::Tokenizer.new(:stemmer, language: :en),
|
13
|
+
exclusive_writer: true,
|
14
|
+
}
|
15
|
+
|
16
|
+
index = Tantiny::Index.new(path, **options) do
|
17
|
+
id :imdb_id
|
18
|
+
facet :category
|
19
|
+
string :title
|
20
|
+
text :description
|
21
|
+
integer :duration
|
22
|
+
double :rating
|
23
|
+
date :release_date
|
24
|
+
end
|
25
|
+
|
26
|
+
rio_bravo = OpenStruct.new(
|
27
|
+
imdb_id: "tt0053221",
|
28
|
+
type: '/western/US',
|
29
|
+
title: "Rio Bravo",
|
30
|
+
description: "A small-town sheriff enlists a drunk, a kid and an old man to help him fight off a ruthless cattle baron.",
|
31
|
+
duration: 141,
|
32
|
+
rating: 8.0,
|
33
|
+
release_date: Date.parse("March 18, 1959")
|
34
|
+
)
|
35
|
+
|
36
|
+
hanabi = {
|
37
|
+
imdb_id: "tt0119250",
|
38
|
+
type: "/crime/Japan",
|
39
|
+
title: "Hana-bi",
|
40
|
+
description: "Nishi leaves the police in the face of harrowing personal and professional difficulties. Spiraling into depression, he makes questionable decisions.",
|
41
|
+
duration: 103,
|
42
|
+
rating: 7.7,
|
43
|
+
release_date: Date.parse("December 1, 1998")
|
44
|
+
}
|
45
|
+
|
46
|
+
brother = {
|
47
|
+
imdb_id: "tt0118767",
|
48
|
+
type: "/crime/Russia",
|
49
|
+
title: "Brother",
|
50
|
+
description: "An ex-soldier with a personal honor code enters the family crime business in St. Petersburg, Russia.",
|
51
|
+
duration: 99,
|
52
|
+
rating: 7.9,
|
53
|
+
release_date: Date.parse("December 12, 1997")
|
54
|
+
}
|
55
|
+
|
56
|
+
index.transaction do
|
57
|
+
index << rio_bravo
|
58
|
+
index << hanabi
|
59
|
+
index << brother
|
60
|
+
end
|
61
|
+
|
62
|
+
index.reload
|
63
|
+
|
64
|
+
binding.pry
|
data/bin/setup
ADDED
data/ext/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Tantiny
|
4
|
+
class TantivyError < StandardError; end
|
5
|
+
|
6
|
+
class IndexWriterBusyError < StandardError
|
7
|
+
def initialize
|
8
|
+
msg = "Failed to acquire an index writer. " \
|
9
|
+
"Is there an active index with an exclusive writer already?"
|
10
|
+
|
11
|
+
super(msg)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class UnexpectedNone < StandardError
|
16
|
+
def initialize(type)
|
17
|
+
super("Didn't expect Option<#{type}> to be empty.")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class UnknownTokenizer < StandardError
|
22
|
+
def initialize(tokenizer_type)
|
23
|
+
super("Can't find \"#{tokenizer_type}\" tokenizer.")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class UnsupportedRange < StandardError
|
28
|
+
def initialize(range_type)
|
29
|
+
super("#{range_type} range is not supported by range_query.")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class UnsupportedField < StandardError
|
34
|
+
def initialize(field)
|
35
|
+
super("Can't search the \"#{field}\" field with this query.")
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Tantiny
|
4
|
+
module Helpers
|
5
|
+
def self.timestamp(date)
|
6
|
+
date.to_datetime.iso8601
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.with_lock(lockfile)
|
10
|
+
File.open(lockfile, File::CREAT) do |file|
|
11
|
+
file.flock(File::LOCK_EX)
|
12
|
+
|
13
|
+
yield
|
14
|
+
|
15
|
+
file.flock(File::LOCK_UN)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|