elastico 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/Gemfile.lock +5 -1
- data/README.md +95 -62
- data/elastico.gemspec +3 -2
- data/lib/elastico/callbacks.rb +13 -2
- data/lib/elastico/client.rb +21 -11
- data/lib/elastico/index.rb +1 -1
- data/lib/elastico/version.rb +1 -1
- metadata +29 -7
- checksums.yaml +0 -15
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,12 +1,15 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
elastico (0.0.
|
4
|
+
elastico (0.0.4)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
|
+
mime-types (1.25)
|
9
10
|
rake (10.1.0)
|
11
|
+
rest-client (1.6.7)
|
12
|
+
mime-types (>= 1.16)
|
10
13
|
spawnling (2.1.1)
|
11
14
|
|
12
15
|
PLATFORMS
|
@@ -15,4 +18,5 @@ PLATFORMS
|
|
15
18
|
DEPENDENCIES
|
16
19
|
elastico!
|
17
20
|
rake
|
21
|
+
rest-client
|
18
22
|
spawnling (~> 2.1.1)
|
data/README.md
CHANGED
@@ -1,6 +1,23 @@
|
|
1
1
|
# Elastico
|
2
2
|
|
3
|
-
|
3
|
+
Elastico is a __simple__ layer that enables you to use elasticsearch __full__ API. The elasticsearch team created this [great API](http://www.elasticsearch.org/guide/) which enables you to do almost anything todays search engines allow you to do.
|
4
|
+
|
5
|
+
__Simple__. Elastico is built in a way so your configuration and usage is as simple and streat forward as possible.
|
6
|
+
|
7
|
+
__Full__. Elastico is built to be transparent, non-blocking, non-opinionated layer between your code to elasticsearch API. Meaning Elastico won't force anything about how to index your active records models, neither how to search it.
|
8
|
+
|
9
|
+
There is one reason for building it Simple and Full:
|
10
|
+
Elasticsearch community is large. Its supportive, and very helpful. Though most of it does not use ruby. Its most likely that you want to spend your time on feaguring out how to use elasticsearch rather than how to use the gem that works with elasticsearch. If you use Elastico, its most likely that your time will go to the former, and will be able to take advantage of the elasticsearch community.
|
11
|
+
|
12
|
+
You should give Elastico a try if you want to use elasticsearch full api.
|
13
|
+
|
14
|
+
|
15
|
+
## Elasticsearch mini Background
|
16
|
+
|
17
|
+
In general, in order to be able to use elasticsearch search capabilities you should:
|
18
|
+
|
19
|
+
1. Define what and how you want to index (settings and mappings)
|
20
|
+
2. Define what and how you want to search (query the index)
|
4
21
|
|
5
22
|
## Installation
|
6
23
|
|
@@ -10,7 +27,7 @@ Add this line to your application's Gemfile:
|
|
10
27
|
|
11
28
|
And then execute:
|
12
29
|
|
13
|
-
$ bundle
|
30
|
+
$ bundle install
|
14
31
|
|
15
32
|
Or install it yourself as:
|
16
33
|
|
@@ -18,7 +35,80 @@ Or install it yourself as:
|
|
18
35
|
|
19
36
|
## Usage
|
20
37
|
|
21
|
-
|
38
|
+
In your model:
|
39
|
+
|
40
|
+
class Apple < ActiveRecord::Base
|
41
|
+
|
42
|
+
def self.prepare_elastico_settings_and_mappings_json
|
43
|
+
json = { "apple" => {
|
44
|
+
"properties" => {
|
45
|
+
"name" => {"type" => "string"},
|
46
|
+
"color" => {"type" => "string"}
|
47
|
+
}
|
48
|
+
}}.to_json
|
49
|
+
end
|
50
|
+
include Elastico
|
51
|
+
|
52
|
+
attr_accessible :color, :name
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
In your controller:
|
58
|
+
|
59
|
+
class ApplesController < ApplicationController
|
60
|
+
def index
|
61
|
+
if params[:query]
|
62
|
+
Apple.search_query = prepare_search_json_for params[:query]
|
63
|
+
@apples = Apple.elastico_search
|
64
|
+
else
|
65
|
+
@apples = Apple.all
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
def prepare_search_json_for query
|
71
|
+
json = { "query" =>
|
72
|
+
{
|
73
|
+
"term" => { "color" => query.to_s }
|
74
|
+
}
|
75
|
+
}.to_json
|
76
|
+
end
|
77
|
+
# now do something with your apples
|
78
|
+
end
|
79
|
+
|
80
|
+
|
81
|
+
Learn by Example
|
82
|
+
================
|
83
|
+
Fork the example app that shows how to use Elastico [here](https://github.com/gneyal/ElasticoExample).
|
84
|
+
|
85
|
+
|
86
|
+
Configure it
|
87
|
+
------------
|
88
|
+
|
89
|
+
In order to get a model work with elasticsearch + Elastico you have to:
|
90
|
+
|
91
|
+
__Mandatory__
|
92
|
+
|
93
|
+
1. In your controller, Create a class method __'prepare\_search\_json\_for'__ to be your standard search query for that model (defaults to nil). This is the same search query you would send if you were using the curl command.
|
94
|
+
|
95
|
+
2. In your model, Create a class method __'prepare\_elastico\_settings\_and\_mappings\_json'__ to be your settings and mapping json (defaults to nil). This is the same search query you would send if you were using the curl command.
|
96
|
+
|
97
|
+
__Optional__
|
98
|
+
|
99
|
+
1. Set Apple.elastico\_url= to be the ip of your elasticsearch server (defaults to "localhost:9200").
|
100
|
+
|
101
|
+
2. Set up Apple.elastico\_index\_name (optional - defaults to your class name followed by Rail.env; here it will be apples_development).
|
102
|
+
|
103
|
+
3. Set up Apple.elastico\_type\_name (optional - defaults to your class name; here it will be Apple).
|
104
|
+
|
105
|
+
Use it.
|
106
|
+
-------
|
107
|
+
1. After every save Elastico will automatically save you instance in elasticsearch.
|
108
|
+
|
109
|
+
2. Import current database instances by creating a rake task to "save" them all.
|
110
|
+
|
111
|
+
3. Search it: call Apple.elastico_search to get your results, or override it to better suit your needs.
|
22
112
|
|
23
113
|
## Contributing
|
24
114
|
|
@@ -28,63 +118,6 @@ TODO: Write usage instructions here
|
|
28
118
|
4. Push to the branch (`git push origin my-new-feature`)
|
29
119
|
5. Create new Pull Request
|
30
120
|
|
31
|
-
|
32
|
-
|
33
|
-
In order to get a model work with elastic search you have to:
|
34
|
-
1. Set Elastico::Configuration.url= to be the ip of your elasticsearch server (defaults to "localhost").
|
35
|
-
2. Set Elastico::Index.settings_and_mappings_json= to be your settings and mapping json.
|
36
|
-
3. Set up Elastico::Search.search_json= to be your standard search query for that model.
|
37
|
-
4. Elastico will automatically save your active record models after every "save" call on that instance.
|
38
|
-
5. In later versions I will add an auto rake task to import all of your Elastico models to elasticsearch server.
|
39
|
-
|
40
|
-
|
41
|
-
In order to search this model, all you need to do is to do Model.elastic_search(term). This will return the elasticsearch results in an Elastico results object. You should override this method (elastic_search) if you want to get a better tailored solution.
|
42
|
-
=======
|
43
|
-
|
44
|
-
Usage:
|
45
|
-
======
|
46
|
-
|
47
|
-
1. Setup (define the mapping and settings)
|
48
|
-
2. Index current data (Rake task)
|
49
|
-
3. Search (define how to search, what to search)
|
50
|
-
4. Callbacks (automatically added)
|
51
|
-
|
52
|
-
|
53
|
-
class Someclass < ActiveRecord::Base
|
54
|
-
Elastico::Setup = { } (How to index)
|
55
|
-
Elastico::Search = {} (What to search)
|
56
|
-
Elastico:Index = {} (What to index)
|
57
|
-
end
|
58
|
-
|
59
|
-
|
60
|
-
Setup
|
61
|
-
=====
|
62
|
-
You should give a hash that looks something like that:
|
63
|
-
settings_json[:settings] = {
|
64
|
-
:settings => {
|
65
|
-
:number_of_shards => 2,
|
66
|
-
:number_of_replicas => 0,
|
67
|
-
:analysis => {
|
68
|
-
:filter => {
|
69
|
-
:my_ngram => {
|
70
|
-
"type" => "nGram",
|
71
|
-
"max_gram" => 15,
|
72
|
-
"min_gram" => 1
|
73
|
-
},
|
74
|
-
:my_stemmer => {
|
75
|
-
"type" => "stemmer",
|
76
|
-
"name" => "english"
|
77
|
-
}
|
78
|
-
},
|
79
|
-
:analyzer => {
|
80
|
-
:ngram_analyzer => {
|
81
|
-
"tokenizer" => "whitespace",
|
82
|
-
"filter" => ["stop", "my_ngram", "lowercase"],
|
83
|
-
"type" => "custom"
|
84
|
-
}
|
85
|
-
}
|
86
|
-
}
|
87
|
-
}
|
88
|
-
}
|
121
|
+
## Feedback
|
89
122
|
|
90
|
-
|
123
|
+
Please send your feedback to gneyal+elastico@gmail.com
|
data/elastico.gemspec
CHANGED
@@ -7,10 +7,10 @@ Gem::Specification.new do |gem|
|
|
7
7
|
gem.name = "elastico"
|
8
8
|
gem.version = Elastico::VERSION
|
9
9
|
gem.authors = ["Eyal Goren"]
|
10
|
-
gem.email = ["gneyal@gmail.com"]
|
10
|
+
gem.email = ["gneyal+elastico@gmail.com"]
|
11
11
|
gem.description = %q{A general way to use elasticsearch}
|
12
12
|
gem.summary = %q{Elastico presents a simple way to work with elasticsearch. 1. Define your setup. 2. Define your search query. 3. Search.}
|
13
|
-
gem.homepage = ""
|
13
|
+
gem.homepage = "https://github.com/gneyal/Elastico"
|
14
14
|
|
15
15
|
gem.files = `git ls-files`.split($/)
|
16
16
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
@@ -19,4 +19,5 @@ Gem::Specification.new do |gem|
|
|
19
19
|
|
20
20
|
gem.add_development_dependency 'rake'
|
21
21
|
gem.add_development_dependency "spawnling", "~> 2.1.1"
|
22
|
+
gem.add_development_dependency "rest-client"
|
22
23
|
end
|
data/lib/elastico/callbacks.rb
CHANGED
@@ -2,10 +2,13 @@ module Elastico
|
|
2
2
|
module Callbacks
|
3
3
|
def self.included(base)
|
4
4
|
update_conditions = lambda { update_index_with_instance_elastico }
|
5
|
-
|
5
|
+
destroy_instance = lambda { destroy_instance_in_elastic_search }
|
6
6
|
base.send :after_save, update_conditions
|
7
|
-
base.send :after_destroy,
|
7
|
+
base.send :after_destroy, destroy_instance
|
8
8
|
base.send :after_touch, update_conditions
|
9
|
+
|
10
|
+
base.send :settings_and_mappings_json=, base.prepare_elastico_settings_and_mappings_json
|
11
|
+
base.send :send_settings_mappings_to_elasticsearch_server
|
9
12
|
end
|
10
13
|
|
11
14
|
def update_index_with_instance_elastico
|
@@ -16,5 +19,13 @@ module Elastico
|
|
16
19
|
instance = self.to_json
|
17
20
|
self.class.general_request(instance, url)
|
18
21
|
end
|
22
|
+
|
23
|
+
def destroy_instance_in_elastic_search
|
24
|
+
type = self.class.elastico_type_name
|
25
|
+
index = self.class.elastico_index_name
|
26
|
+
id = self.id
|
27
|
+
url = self.class.elastico_url + index + "/" + type + "/" + id.to_s
|
28
|
+
self.class.delete_instance(url)
|
29
|
+
end
|
19
30
|
end
|
20
31
|
end
|
data/lib/elastico/client.rb
CHANGED
@@ -8,20 +8,30 @@ module Elastico
|
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
def
|
12
|
-
RestClient.
|
13
|
-
|
14
|
-
|
15
|
-
|
11
|
+
def delete_instance url
|
12
|
+
RestClient.delete url
|
13
|
+
end
|
14
|
+
|
15
|
+
def general_request(content=nil, url=self.elastico_url, options = { :hash_respond_wanted => :true })
|
16
|
+
|
17
|
+
logger.info("========== in post ")
|
18
|
+
RestClient.post(url, content, :content_type => :json, :accept => :json) do |response, request, result|
|
19
|
+
ret = JSON.pretty_generate(JSON.parse(response.to_str))
|
20
|
+
options[:hash_respond_wanted] ? JSON.parse(ret) : ret
|
21
|
+
end
|
16
22
|
end
|
17
23
|
|
18
24
|
def elastico_search(hash_respond_wanted = true)
|
19
25
|
to_url = self.elastico_url + self.elastico_index_name + "/_search"
|
20
|
-
|
21
|
-
RestClient.
|
22
|
-
|
23
|
-
|
24
|
-
end
|
26
|
+
|
27
|
+
RestClient.post(to_url, self.search_query, :content_type => :json, :accept => :json) do |response, request, result|
|
28
|
+
ret = JSON.pretty_generate(JSON.parse(response.to_str))
|
29
|
+
hash_respond_wanted ? JSON.parse(ret) : ret
|
30
|
+
end
|
31
|
+
|
25
32
|
end
|
26
33
|
end
|
27
|
-
end
|
34
|
+
end
|
35
|
+
# curl -XGET "http://localhost:9200/apples_development/_search" -d '{"query":{"term":{"color":"test"}}}'
|
36
|
+
|
37
|
+
# RestClient.post "http://localhost:9200/apples_development/_search", '{"query":{"term":{"color":"test"}}}', :content_type => :json
|
data/lib/elastico/index.rb
CHANGED
data/lib/elastico/version.rb
CHANGED
metadata
CHANGED
@@ -1,18 +1,20 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: elastico
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
7
|
authors:
|
7
8
|
- Eyal Goren
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date: 2013-09-
|
12
|
+
date: 2013-09-26 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: rake
|
15
16
|
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
16
18
|
requirements:
|
17
19
|
- - ! '>='
|
18
20
|
- !ruby/object:Gem::Version
|
@@ -20,6 +22,7 @@ dependencies:
|
|
20
22
|
type: :development
|
21
23
|
prerelease: false
|
22
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
23
26
|
requirements:
|
24
27
|
- - ! '>='
|
25
28
|
- !ruby/object:Gem::Version
|
@@ -27,6 +30,7 @@ dependencies:
|
|
27
30
|
- !ruby/object:Gem::Dependency
|
28
31
|
name: spawnling
|
29
32
|
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
30
34
|
requirements:
|
31
35
|
- - ~>
|
32
36
|
- !ruby/object:Gem::Version
|
@@ -34,13 +38,30 @@ dependencies:
|
|
34
38
|
type: :development
|
35
39
|
prerelease: false
|
36
40
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
37
42
|
requirements:
|
38
43
|
- - ~>
|
39
44
|
- !ruby/object:Gem::Version
|
40
45
|
version: 2.1.1
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rest-client
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
41
62
|
description: A general way to use elasticsearch
|
42
63
|
email:
|
43
|
-
- gneyal@gmail.com
|
64
|
+
- gneyal+elastico@gmail.com
|
44
65
|
executables: []
|
45
66
|
extensions: []
|
46
67
|
extra_rdoc_files: []
|
@@ -65,28 +86,29 @@ files:
|
|
65
86
|
- test/lib/elastico/search_test.rb
|
66
87
|
- test/lib/elastico/version_test.rb
|
67
88
|
- test/test_helper.rb
|
68
|
-
homepage:
|
89
|
+
homepage: https://github.com/gneyal/Elastico
|
69
90
|
licenses: []
|
70
|
-
metadata: {}
|
71
91
|
post_install_message:
|
72
92
|
rdoc_options: []
|
73
93
|
require_paths:
|
74
94
|
- lib
|
75
95
|
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
76
97
|
requirements:
|
77
98
|
- - ! '>='
|
78
99
|
- !ruby/object:Gem::Version
|
79
100
|
version: '0'
|
80
101
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
none: false
|
81
103
|
requirements:
|
82
104
|
- - ! '>='
|
83
105
|
- !ruby/object:Gem::Version
|
84
106
|
version: '0'
|
85
107
|
requirements: []
|
86
108
|
rubyforge_project:
|
87
|
-
rubygems_version:
|
109
|
+
rubygems_version: 1.8.24
|
88
110
|
signing_key:
|
89
|
-
specification_version:
|
111
|
+
specification_version: 3
|
90
112
|
summary: Elastico presents a simple way to work with elasticsearch. 1. Define your
|
91
113
|
setup. 2. Define your search query. 3. Search.
|
92
114
|
test_files:
|
checksums.yaml
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
---
|
2
|
-
!binary "U0hBMQ==":
|
3
|
-
metadata.gz: !binary |-
|
4
|
-
M2JkZTg4NmYzZjZkM2QwNjEyZjFjMjFiOGU2OGIzY2YyYjZmMzM1Nw==
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
OGEwOTQ3ZDcwZWUzMDhkOGU5NjM4YWM0NTczMTE0N2U3MWQ0NDZiOQ==
|
7
|
-
!binary "U0hBNTEy":
|
8
|
-
metadata.gz: !binary |-
|
9
|
-
OGRjYzhlZDUyNDhhOTZiYTg5MzYxNzVjMzhmMWE5NzgyNmQ4MTdjNTMwMWFl
|
10
|
-
MGZiMjBiMjY2ZDAwMGFlY2YyMDY0NWE4N2UxYmRkMmYyYTM2ZjNhNmZlMDZh
|
11
|
-
NTJjZjA1NGI5N2JkNWVhZTVjMWIxNWY1NjFlOGE1ZTM0YTM5NDA=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
NGZlZjQ1MzUyMTE2MGFlODIxMjQxZWVmYWZkYTkzMTcwNjE2YjZhZDZjYjIz
|
14
|
-
OTQ5ZGEwOGI3YmU2NjVmNDFhNmUxOWJkMjg2NmJlNGI2NTY2OTM1YTlmNjI5
|
15
|
-
ZDA2MTI2MTQzNmU2YmJkMjU3MmM1NzYyNmY3YzJmMjhhMWQ2ODI=
|