sander6-enygma 0.0.5 → 0.0.6
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.
- data/README.markdown +58 -19
- metadata +1 -1
data/README.markdown
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
*NOTE: Enygma is currently in a state of disarray, since I hacked it together just enough to work with ActiveRecord. The specs shouldn't run and coverage is inexcusable, which I feel bad about. I'll clean things up incrementally. Until then, consider it in alpha.*
|
4
4
|
|
5
|
+
*ANOTHER NOTE: This documentation is unfinished and a little wanky. I'll improve it when time allows. The best way right now to figure out how to use Enygma is to look through the code.*
|
6
|
+
|
5
7
|
Sphinx is awesome, but it's sometimes kind of unwieldly to use, requiring a bunch of moving parts just to see what a certain Sphinx query would yank out of your database. Some solutions for working with Sphinx exist, but it's hard to justify spinning up an entire new Rails project just to search through some HTML documents you've got lying on your hard drive.
|
6
8
|
|
7
9
|
For this reason, Enygma exists to be an awesome little Sphinx toolset usable just about anywhere.
|
@@ -27,22 +29,21 @@ That being said, Enygma plans to eventually support guided Sphinx configuration.
|
|
27
29
|
|
28
30
|
#### Configuration ####
|
29
31
|
|
30
|
-
Take your favorite class and `include Enygma`.
|
32
|
+
Take your favorite class and `include Enygma`, then give it class-level configuration by calling `configure_enygma`. This will save a constant in the class called `<class_name>_ENYGMA_CONFIGURATION` holding the configuration
|
33
|
+
|
31
34
|
|
32
35
|
Enygma::Configuration.global do
|
33
|
-
adapter
|
34
|
-
|
36
|
+
adapter :sequel
|
37
|
+
datastore "postgres://user@localhost/db"
|
38
|
+
sphinx.host 'localhost'
|
39
|
+
sphinx.port 3312
|
35
40
|
end
|
36
41
|
|
37
42
|
class SearchyThing
|
38
43
|
include Enygma
|
39
44
|
|
40
|
-
configure_enygma do
|
41
|
-
|
42
|
-
sphinx[:port] = 3312
|
43
|
-
|
44
|
-
table :posts, :indexes => [ :posts_idx, :comments_idx ]
|
45
|
-
table :comments, :indexes => [ :comments_idx ]
|
45
|
+
configure_enygma do
|
46
|
+
table :posts, :indexes => [ :posts, :comments ]
|
46
47
|
end
|
47
48
|
end
|
48
49
|
|
@@ -50,21 +51,33 @@ This appends the `search` method to the included class.
|
|
50
51
|
|
51
52
|
#### Searching ####
|
52
53
|
|
53
|
-
To search
|
54
|
-
|
55
|
-
SearchyThing.search.for("funtimes")
|
54
|
+
To search, call the `search` method on a class that included Enygma. The way searching works depends a lot on both the kind of class you've included Enygma in and the type of datastore adapter you're using. The fundamentals are the same, though.
|
56
55
|
|
57
|
-
|
56
|
+
For a plain ol' Ruby class, like a controller, call `search` and tell it where to go find the actual objects after it's retrieved the pointers from Sphinx. An example using the `active_record` adapter:
|
58
57
|
|
59
|
-
|
60
|
-
|
61
|
-
|
58
|
+
class PostsController < ApplicationController
|
59
|
+
include Enygma
|
60
|
+
|
61
|
+
configure_enygma do
|
62
|
+
adapter :active_record
|
63
|
+
end
|
64
|
+
|
65
|
+
def index
|
66
|
+
@posts = search(Post).for(params[:term]).using_index(:posts)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
An example using the `sequel` adapter:
|
62
71
|
|
63
|
-
|
72
|
+
include Enygma
|
64
73
|
|
65
|
-
|
74
|
+
configure_enygma do
|
75
|
+
adapter :sequel
|
76
|
+
end
|
66
77
|
|
67
|
-
|
78
|
+
get '/posts' do
|
79
|
+
@posts = search(:posts).for(params[:term]).using_index(:posts)
|
80
|
+
end
|
68
81
|
|
69
82
|
Adding filters:
|
70
83
|
|
@@ -183,4 +196,30 @@ What follows is an example of using Enygma in an ActionController::Base subclass
|
|
183
196
|
def index
|
184
197
|
@posts = search(:posts).for(params[:search]).all(:include => :comments)
|
185
198
|
end
|
199
|
+
end
|
200
|
+
|
201
|
+
|
202
|
+
## Non-relational Database Stores ##
|
203
|
+
|
204
|
+
Enygma is so awesome that you can use it to hook Sphinx up to non-relational database stores and other data-storing structures, such as Memcache, Tokyo Cabinet, and BerkeleyDB (not currently implemented).
|
205
|
+
|
206
|
+
Of course, Sphinx can't index content from one of these database types, so it's assumed that the data has been prepopulated and that you have already set up a system to keep the original data source (the one Sphinx indexed from) and the data store in sync.
|
207
|
+
|
208
|
+
For example, assume you've taken a large chunk of mostly-static data from your database and put it as marshalled hashes into a Tokyo Cabinet. You can tell Enygma to to query Sphinx for a term, then get the records from the Tokyo Cabinet.
|
209
|
+
|
210
|
+
Let's assume that, nightly, you reindex your users table and stuff a bunch of hashes structured like `{ :id => <id>, :username => <username>, :email => <email> }` into a Tokyo Cabinet file called 'usernames.tch', each under the key `user:<id>`. You want to set up a controller to autocomplete the users' names, and you want it to be fast. You can tell Enygma to look for these hashes in the (lightning-fast) Tokyo Cabinet instead of your (glacially-slow) database like so:
|
211
|
+
|
212
|
+
class UserNamesAutocompletionsController < ApplicationController
|
213
|
+
include Enygma
|
214
|
+
|
215
|
+
configure_enygma do
|
216
|
+
adapter :tokyo_cabinet
|
217
|
+
database 'usernames.tch'
|
218
|
+
key_prefix 'user:'
|
219
|
+
index :users
|
220
|
+
end
|
221
|
+
|
222
|
+
def index
|
223
|
+
@usernames = search.for(params[:search]).run
|
224
|
+
end
|
186
225
|
end
|