elasticsearch-rails2 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +201 -0
- data/Rakefile +11 -0
- data/elasticsearch-rails2.gemspec +31 -0
- data/lib/elasticsearch/rails2.rb +63 -0
- data/lib/elasticsearch/rails2/client.rb +58 -0
- data/lib/elasticsearch/rails2/configuration.rb +39 -0
- data/lib/elasticsearch/rails2/naming.rb +125 -0
- data/lib/elasticsearch/rails2/response.rb +77 -0
- data/lib/elasticsearch/rails2/response/result.rb +62 -0
- data/lib/elasticsearch/rails2/response/results.rb +52 -0
- data/lib/elasticsearch/rails2/searching.rb +120 -0
- data/lib/elasticsearch/rails2/version.rb +5 -0
- data/spec/client_spec.rb +38 -0
- data/spec/naming_spec.rb +88 -0
- data/spec/rails2_spec.rb +53 -0
- data/spec/response/result_spec.rb +89 -0
- data/spec/response/results_spec.rb +33 -0
- data/spec/response_spec.rb +13 -0
- data/spec/searching_search_request_spec.rb +62 -0
- data/spec/searching_spec.rb +36 -0
- data/spec/spec_helper.rb +27 -0
- data/spec/support/active_record.rb +3 -0
- data/tasks/rspec.rake +3 -0
- metadata +198 -0
data/spec/client_spec.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Elasticsearch::Rails2::Client do
|
4
|
+
|
5
|
+
context "Client module" do
|
6
|
+
class ::FooClientModule
|
7
|
+
include Elasticsearch::Rails2
|
8
|
+
# extend Elasticsearch::Rails2::Client::ClassMethods
|
9
|
+
# include Elasticsearch::Rails2::Client::InstanceMethods
|
10
|
+
end
|
11
|
+
|
12
|
+
context "default client method" do
|
13
|
+
it "should be an Elasticsearch::Transport::Client" do
|
14
|
+
expect(FooClientModule.client).to be_an Elasticsearch::Transport::Client
|
15
|
+
expect(FooClientModule.new.client).to be_an Elasticsearch::Transport::Client
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context "set the client for the model" do
|
20
|
+
it "should set the client for the class and for new instances" do
|
21
|
+
obj = 'foo'
|
22
|
+
FooClientModule.client = obj
|
23
|
+
expect(FooClientModule.client).to be obj
|
24
|
+
expect(FooClientModule.new.client).to be obj
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "set the client for an instance" do
|
29
|
+
it "should set the client for the instance" do
|
30
|
+
obj = 'foo'
|
31
|
+
instance = FooClientModule.new
|
32
|
+
instance.client = obj
|
33
|
+
expect(instance.client).to be obj
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
data/spec/naming_spec.rb
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Elasticsearch::Rails2::Naming do
|
4
|
+
context "Naming module" do
|
5
|
+
class ::NamingDummyModel < ActiveRecord::Base
|
6
|
+
include Elasticsearch::Rails2
|
7
|
+
end
|
8
|
+
|
9
|
+
before(:each){create_dummy_table :naming_dummy_models}
|
10
|
+
|
11
|
+
describe "index_name" do
|
12
|
+
context "Elasticsearch::Rails2.index_name is not defined" do
|
13
|
+
it "should return default index_name" do
|
14
|
+
expect(NamingDummyModel.index_name).to eq("naming_dummy_models_index")
|
15
|
+
expect(NamingDummyModel.new.index_name).to eq("naming_dummy_models_index")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context "Elasticsearch::Rails2.index_name is defined" do
|
20
|
+
|
21
|
+
before(:each) {Elasticsearch::Rails2.index_name = 'foo'}
|
22
|
+
after(:each) {Elasticsearch::Rails2.reset}
|
23
|
+
|
24
|
+
it "should return the Elasticsearch::Rails2.index_name" do
|
25
|
+
expect(NamingDummyModel.index_name).to eq("foo")
|
26
|
+
expect(NamingDummyModel.new.index_name).to eq("foo")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context "when the index_name is defined in the class" do
|
31
|
+
before(:each){ NamingDummyModel.index_name = 'foobar'}
|
32
|
+
after(:each){ NamingDummyModel.index_name = nil}
|
33
|
+
|
34
|
+
it "should return the index_name defined in the class" do
|
35
|
+
expect(NamingDummyModel.index_name).to eq("foobar")
|
36
|
+
expect(NamingDummyModel.new.index_name).to eq("foobar")
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
context "when the index_name is defined for an instance" do
|
43
|
+
before(:each){
|
44
|
+
@foo = NamingDummyModel.new
|
45
|
+
@foo.index_name = 'foobar_instance'
|
46
|
+
}
|
47
|
+
|
48
|
+
it "should return the index_name defined in the instance" do
|
49
|
+
expect(NamingDummyModel.index_name).to eq("naming_dummy_models_index")
|
50
|
+
expect(@foo.index_name).to eq("foobar_instance")
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "document_type" do
|
58
|
+
|
59
|
+
it "should return the default calculated document_type" do
|
60
|
+
expect(NamingDummyModel.document_type).to eq("naming_dummy_models")
|
61
|
+
expect(NamingDummyModel.new.document_type).to eq("naming_dummy_models")
|
62
|
+
end
|
63
|
+
|
64
|
+
context "when document_type is defined in the class" do
|
65
|
+
before(:each){ NamingDummyModel.document_type = 'foobar'}
|
66
|
+
after(:each){ NamingDummyModel.document_type = nil}
|
67
|
+
|
68
|
+
it "should return the new document_type" do
|
69
|
+
expect(NamingDummyModel.document_type).to eq("foobar")
|
70
|
+
expect(NamingDummyModel.new.document_type).to eq("foobar")
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context "when document_type is defined in the instance" do
|
75
|
+
before(:each){
|
76
|
+
@foo = NamingDummyModel.new
|
77
|
+
@foo.document_type = 'foobar_instance'
|
78
|
+
}
|
79
|
+
|
80
|
+
it "should return the new document_type" do
|
81
|
+
expect(NamingDummyModel.document_type).to eq("naming_dummy_models")
|
82
|
+
expect(@foo.document_type).to eq("foobar_instance")
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
data/spec/rails2_spec.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Elasticsearch::Rails2 do
|
4
|
+
|
5
|
+
after do
|
6
|
+
Elasticsearch::Rails2.reset
|
7
|
+
end
|
8
|
+
|
9
|
+
describe ".client" do
|
10
|
+
it "should return a default Elasticsearch::Transport::Client" do
|
11
|
+
expect(Elasticsearch::Rails2.client).to be_an Elasticsearch::Transport::Client
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe ".client=" do
|
16
|
+
it "should set the client" do
|
17
|
+
obj = 'foo'
|
18
|
+
Elasticsearch::Rails2.client = obj
|
19
|
+
expect(Elasticsearch::Rails2.client).to be obj
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '.index_name' do
|
24
|
+
it "should return the default index_name" do
|
25
|
+
expect(Elasticsearch::Rails2.index_name).to eq(Elasticsearch::Rails2::Configuration::DEFAULT_INDEX_NAME)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '.index_name=' do
|
30
|
+
it "should set the index_name" do
|
31
|
+
Elasticsearch::Rails2.index_name = 'production'
|
32
|
+
expect(Elasticsearch::Rails2.index_name).to eq('production')
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '.options=' do
|
37
|
+
before do
|
38
|
+
@keys = Elasticsearch::Rails2::Configuration::VALID_OPTIONS_KEYS
|
39
|
+
|
40
|
+
@options = {
|
41
|
+
:index_name => 'production'
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should override default configuration" do
|
46
|
+
Elasticsearch::Rails2.options = @options
|
47
|
+
@keys.each do |key|
|
48
|
+
expect(Elasticsearch::Rails2.send(key)).to eq(@options[key])
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Elasticsearch::Rails2::Response::Result do
|
4
|
+
|
5
|
+
it "should have method access to properties" do
|
6
|
+
result = Elasticsearch::Rails2::Response::Result.new foo: 'bar', bar: { bam: 'baz' }
|
7
|
+
|
8
|
+
expect(result).to respond_to :foo
|
9
|
+
expect(result).to respond_to :bar
|
10
|
+
|
11
|
+
expect(result.foo).to eq('bar')
|
12
|
+
expect(result.bar.bam).to eq('baz')
|
13
|
+
|
14
|
+
expect{result.xoxo}.to raise_error(NoMethodError)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should return _id as #id" do
|
18
|
+
result = Elasticsearch::Rails2::Response::Result.new foo: 'bar', _id: 42, _source: { id: 12 }
|
19
|
+
|
20
|
+
expect(result.id).to eq(42)
|
21
|
+
expect(result._source.id).to eq(12)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should return _type as #type" do
|
25
|
+
result = Elasticsearch::Rails2::Response::Result.new foo: 'bar', _type: 'baz', _source: { type: 'BAM' }
|
26
|
+
|
27
|
+
expect(result.type).to eq('baz')
|
28
|
+
expect(result._source.type).to eq('BAM')
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should delegate method calls to `_source` when available" do
|
32
|
+
result = Elasticsearch::Rails2::Response::Result.new foo: 'bar', _source: { bar: 'baz' }
|
33
|
+
|
34
|
+
expect(result).to respond_to :foo
|
35
|
+
expect(result).to respond_to :_source
|
36
|
+
expect(result).to respond_to :bar
|
37
|
+
|
38
|
+
expect(result.foo).to eq('bar')
|
39
|
+
expect(result._source.bar).to eq('baz')
|
40
|
+
expect(result.bar).to eq('baz')
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should delegate existence method calls to `_source`" do
|
44
|
+
result = Elasticsearch::Rails2::Response::Result.new foo: 'bar', _source: { bar: { bam: 'baz' } }
|
45
|
+
|
46
|
+
expect(result._source).to respond_to :bar?
|
47
|
+
expect(result).to respond_to :bar?
|
48
|
+
|
49
|
+
expect(result._source.bar?).to be true
|
50
|
+
expect(result.bar?).to be true
|
51
|
+
expect(result.boo?).to be false
|
52
|
+
|
53
|
+
expect(result.bar.bam?).to be true
|
54
|
+
expect(result.bar.boo?).to be false
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should delegate methods to @result" do
|
58
|
+
result = Elasticsearch::Rails2::Response::Result.new foo: 'bar'
|
59
|
+
|
60
|
+
|
61
|
+
expect(result.foo).to eq('bar')
|
62
|
+
expect(result.fetch('foo')).to eq('bar')
|
63
|
+
expect(result.fetch('NOT_EXIST', 'moo')).to eq('moo')
|
64
|
+
expect(result.keys).to eq(['foo'])
|
65
|
+
|
66
|
+
expect(result).to respond_to :to_hash
|
67
|
+
expect(result.to_hash).to eq({'foo' => 'bar'})
|
68
|
+
|
69
|
+
expect{result.does_not_exist}.to raise_error(NoMethodError)
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should delegate existence method calls to @result" do
|
73
|
+
result = Elasticsearch::Rails2::Response::Result.new foo: 'bar', _source: { bar: 'bam' }
|
74
|
+
expect(result).to respond_to :foo?
|
75
|
+
|
76
|
+
expect(result.foo?).to be true
|
77
|
+
expect(result.boo?).to be false
|
78
|
+
expect(result._source.foo?).to be false
|
79
|
+
expect(result._source.boo?).to be false
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should delegate as_json to @result even when ActiveSupport changed half of Ruby" do
|
83
|
+
require 'active_support/json/encoding'
|
84
|
+
result = Elasticsearch::Rails2::Response::Result.new foo: 'bar'
|
85
|
+
|
86
|
+
expect(result.instance_variable_get(:@result)).to receive(:as_json).with(:except => 'foo')
|
87
|
+
result.as_json(:except => 'foo')
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Elasticsearch::Rails2::Response::Results do
|
4
|
+
context "Response results" do
|
5
|
+
class ::ResultsDummyModel < ActiveRecord::Base
|
6
|
+
include Elasticsearch::Rails2
|
7
|
+
end
|
8
|
+
|
9
|
+
before do
|
10
|
+
create_dummy_table :results_dummy_models
|
11
|
+
|
12
|
+
@els_response = { 'hits' => { 'total' => 123, 'max_score' => 456, 'hits' => [{'foo' => 'bar'}] } }
|
13
|
+
|
14
|
+
@search = Elasticsearch::Rails2::Searching::SearchRequest.new ResultsDummyModel, '*'
|
15
|
+
allow(@search).to receive(:execute!){@els_response}
|
16
|
+
@response = Elasticsearch::Rails2::Response::Response.new ResultsDummyModel, @search
|
17
|
+
@results = Elasticsearch::Rails2::Response::Results.new ResultsDummyModel, @response
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should access the results" do
|
21
|
+
expect(@results).to respond_to :results
|
22
|
+
results_results = @results.results
|
23
|
+
expect(results_results.size).to eq 1
|
24
|
+
expect(results_results.first.foo).to eq 'bar'
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should delegate Enumerable methods to results" do
|
28
|
+
expect(@results).to_not be_empty
|
29
|
+
expect(@results.first.foo).to eq 'bar'
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Elasticsearch::Rails2::Response do
|
4
|
+
|
5
|
+
class ::ResponseFooModel < ActiveRecord::Base
|
6
|
+
include Elasticsearch::Rails2
|
7
|
+
index_name :test_index
|
8
|
+
end
|
9
|
+
|
10
|
+
RESPONSE = { 'took' => '5', 'timed_out' => false, '_shards' => {'one' => 'OK'}, 'hits' => { 'hits' => [] } }
|
11
|
+
|
12
|
+
|
13
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Elasticsearch::Rails2::Searching::SearchRequest do
|
4
|
+
class ::SearchingDummyModel < ActiveRecord::Base
|
5
|
+
include Elasticsearch::Rails2
|
6
|
+
end
|
7
|
+
|
8
|
+
before do
|
9
|
+
create_dummy_table :searching_dummy_models
|
10
|
+
|
11
|
+
@client = double('client')
|
12
|
+
expect(SearchingDummyModel).to receive(:client).and_return(@client)
|
13
|
+
end
|
14
|
+
|
15
|
+
context "when the query is an string" do
|
16
|
+
it "should pass the search definition as a simple query" do
|
17
|
+
expect(@client).to receive(:search).with(hash_including(q: 'foo'))
|
18
|
+
|
19
|
+
search_request = Elasticsearch::Rails2::Searching::SearchRequest.new SearchingDummyModel, 'foo'
|
20
|
+
search_request.execute!
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "when the query is a hash" do
|
25
|
+
it "should pass the search definition as a hash" do
|
26
|
+
expect(@client).to receive(:search).with(hash_including(body: {foo: 'bar'}))
|
27
|
+
|
28
|
+
search_request = Elasticsearch::Rails2::Searching::SearchRequest.new SearchingDummyModel, {foo: 'bar'}
|
29
|
+
search_request.execute!
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context "when the query is a JSON string" do
|
34
|
+
it "should pass the search definition as a JSON string" do
|
35
|
+
expect(@client).to receive(:search).with(hash_including(body: "{'foo':'bar'}"))
|
36
|
+
|
37
|
+
search_request = Elasticsearch::Rails2::Searching::SearchRequest.new SearchingDummyModel, "{'foo':'bar'}"
|
38
|
+
search_request.execute!
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "when the query responds to_hash" do
|
43
|
+
class CustomQuery
|
44
|
+
def to_hash; {foo: 'bar'}; end
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should pass the search definition as a hash" do
|
48
|
+
expect(@client).to receive(:search).with(hash_including(body: {foo: 'bar'}))
|
49
|
+
|
50
|
+
search_request = Elasticsearch::Rails2::Searching::SearchRequest.new SearchingDummyModel, CustomQuery.new
|
51
|
+
search_request.execute!
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should pass the options to the client" do
|
56
|
+
expect(@client).to receive(:search).with(hash_including(q: 'foo', size: 15))
|
57
|
+
|
58
|
+
search_request = Elasticsearch::Rails2::Searching::SearchRequest.new SearchingDummyModel, 'foo', size: 15
|
59
|
+
search_request.execute!
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Elasticsearch::Rails2::Searching do
|
4
|
+
class ::SearchingDummyModel < ActiveRecord::Base
|
5
|
+
include Elasticsearch::Rails2
|
6
|
+
end
|
7
|
+
|
8
|
+
before do
|
9
|
+
create_dummy_table :searching_dummy_models
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should have the search method" do
|
13
|
+
expect(SearchingDummyModel).to respond_to :search
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should have the scan_all_ids method" do
|
17
|
+
expect(SearchingDummyModel).to respond_to :scan_all_ids
|
18
|
+
end
|
19
|
+
|
20
|
+
describe ".search" do
|
21
|
+
it "should initialize the search request object" do
|
22
|
+
expect(Elasticsearch::Rails2::Searching::SearchRequest).to receive(:new)
|
23
|
+
.with(SearchingDummyModel, 'foo', {default_operator: 'AND'})
|
24
|
+
|
25
|
+
SearchingDummyModel.search 'foo', default_operator: 'AND'
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should not execute the actual search" do
|
29
|
+
# the search is executed when the response is accessed
|
30
|
+
search_request = double('search_request')
|
31
|
+
allow(Elasticsearch::Rails2::Searching::SearchRequest).to receive(:new).and_return(search_request)
|
32
|
+
expect(search_request).to_not receive(:execute!)
|
33
|
+
SearchingDummyModel.search 'foo'
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
RUBY_1_8 = defined?(RUBY_VERSION) && RUBY_VERSION < '1.9'
|
2
|
+
|
3
|
+
exit(0) if RUBY_1_8
|
4
|
+
|
5
|
+
$:.unshift File.dirname(__FILE__) + '/../lib'
|
6
|
+
|
7
|
+
require 'elasticsearch/rails2'
|
8
|
+
require 'rspec'
|
9
|
+
require 'support/active_record'
|
10
|
+
|
11
|
+
RSpec.configure do |config|
|
12
|
+
config.before(:each) {@dummy_tables = []}
|
13
|
+
config.after(:each) {drop_dummy_tables}
|
14
|
+
end
|
15
|
+
|
16
|
+
def create_dummy_table(table_name)
|
17
|
+
ActiveRecord::Migration.verbose = false
|
18
|
+
ActiveRecord::Migration.create_table table_name do |t|
|
19
|
+
t.string :name
|
20
|
+
end
|
21
|
+
@dummy_tables << table_name
|
22
|
+
end
|
23
|
+
|
24
|
+
def drop_dummy_tables
|
25
|
+
ActiveRecord::Migration.verbose = false
|
26
|
+
@dummy_tables.each {|table_name| ActiveRecord::Migration.drop_table table_name}
|
27
|
+
end
|