tire-contrib 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,12 +5,15 @@
5
5
  language: ruby
6
6
 
7
7
  rvm:
8
+ - 1.8.7
8
9
  - 1.9.3
10
+ - 2.0.0
11
+ - jruby-19mode
9
12
 
10
13
  script: "bundle exec rake test"
11
14
 
12
- before_install:
13
- - sudo service elasticsearch start
15
+ services:
16
+ - elasticsearch
14
17
 
15
18
  notifications:
16
19
  disable: true
@@ -23,6 +23,10 @@ Adds support for dynamic attributes when using Tire::Model::Persistence so that
23
23
 
24
24
  Adds support for [“more like this”](http://www.elasticsearch.org/guide/reference/query-dsl/mlt-query.html) queries.
25
25
 
26
+ ### Fuzzy Like This Queries ###
27
+
28
+ Adds support for [“fuzzy like this”](http://www.elasticsearch.org/guide/reference/query-dsl/flt-query.html) queries.
29
+
26
30
  ### Custom Filters Score ###
27
31
 
28
32
  Adds support for [“custom filters score”](http://www.elasticsearch.org/guide/reference/query-dsl/custom-filters-score-query.html) queries.
@@ -31,6 +35,10 @@ Adds support for [“custom filters score”](http://www.elasticsearch.org/guide
31
35
 
32
36
  Adds support for displaying Tire related statistics in the Rails' log.
33
37
 
38
+ ### Namespace ###
39
+
40
+ Adds support for namespace all Index's of an Application
41
+
34
42
  -----
35
43
 
36
44
  [Karel Minarik](http://karmi.cz) and [contributors](http://github.com/karmi/tire-contrib/contributors)
@@ -1,5 +1,5 @@
1
1
  module Tire
2
2
  module Contrib
3
- VERSION = "0.1.2"
3
+ VERSION = "0.1.3"
4
4
  end
5
5
  end
@@ -0,0 +1,68 @@
1
+ # Active Model Serializer
2
+ # ===================
3
+ #
4
+ # Author: Sergey Efremov <efremov.sergey@gmail.com>
5
+ #
6
+ #
7
+ # Adds support for serialization through ActiveModel::Serializers to Tire::Model::Collection and Tire::Results::Item.
8
+ #
9
+ # ActiveModel::Serializers provide simple way to encapsulate serialization of ActiveModel objects.
10
+ #
11
+ # More info: https://github.com/rails-api/active_model_serializers
12
+ # For Vacancy model from Tire::Result will use TireVacancySerializer, if not exist will fallback to VacancySerializer.
13
+ #
14
+ #
15
+ # Usage
16
+ # -----
17
+ #
18
+ # Require the component:
19
+ #
20
+ # require 'tire/model/active_model_serializer'
21
+ #
22
+ # Example
23
+ # -------
24
+ #
25
+ # class Vacancy
26
+ # include Tire::Model::Search
27
+ #
28
+ # # ...
29
+ # end
30
+ #
31
+ # class VacancySerializer < ActiveModel::Serializer
32
+ # attributes :position, :body, :location
33
+ #
34
+ # def location
35
+ # "#{city}, #{state}, #{country}"
36
+ # end
37
+ #
38
+ # end
39
+ #
40
+ # class VacanciesController < ApplicationController
41
+ #
42
+ # def index
43
+ # render json: Vacancy.search(params[:term]), root: false
44
+ # end
45
+ #
46
+ # end
47
+ #
48
+ # Response:
49
+ #
50
+ # {
51
+ # [
52
+ # {
53
+ # position: "Developer", body: "Some text", location: "Palo Alto, California, United States"
54
+ # },
55
+ # {
56
+ # position: "Designer", body: "Some text", location: "San Carlos, California, United States"
57
+ # }
58
+ # ]
59
+ # }
60
+ #
61
+ #
62
+
63
+ require 'active_model_serializers'
64
+ require 'tire/model/active_model_serializer/serializer_support'
65
+
66
+ Tire::Results::Collection.send(:include, ActiveModel::ArraySerializerSupport)
67
+ Tire::Results::Item.send(:include, ActiveModel::SerializerSupport)
68
+ Tire::Results::Item.send(:include, Tire::Model::SerializerSupport)
@@ -0,0 +1,29 @@
1
+ module Tire
2
+ module Model
3
+ module SerializerSupport
4
+
5
+ extend ActiveSupport::Concern
6
+
7
+ if "".respond_to?(:safe_constantize)
8
+ def tire_active_model_serializer
9
+ "Tire#{self.class}Serializer".safe_constantize
10
+ end
11
+ else
12
+ def tire_active_model_serializer
13
+ begin
14
+ "Tire#{self.class}Serializer".constantize
15
+ rescue NameError => e
16
+ raise unless e.message =~ /uninitialized constant/
17
+ end
18
+ end
19
+ end
20
+
21
+ # Returns a model serializer for this object considering its namespace.
22
+ #
23
+ def active_model_serializer
24
+ self.tire_active_model_serializer || self.class.active_model_serializer
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ # Index Namespace
2
+ # ===============
3
+ #
4
+ # Author: Timo Schilling <timo@schilling.io>
5
+ #
6
+ # This extension allows you to "namespace" index operations in Tire.
7
+ #
8
+ # Usage:
9
+ # ------
10
+ #
11
+ # require 'tire/namespace'
12
+ #
13
+ # Tire.configure do
14
+ # namespace("foo")
15
+ # end
16
+ #
17
+ # Tire.index("bar").name
18
+ # => "foo-bar"
19
+ #
20
+ # Tire.index("bar").exists?
21
+ # => curl -I "http://localhost:9200/foo-bar"
22
+ #
23
+ require 'tire/namespace/index'
24
+ require 'tire/namespace/configuration'
25
+
26
+ module Tire
27
+ Index.send :include, Tire::Namespace::Index
28
+ Configuration.send :extend, Tire::Namespace::Configuration
29
+ end
@@ -0,0 +1,9 @@
1
+ module Tire
2
+ module Namespace
3
+ module Configuration
4
+ def namespace(name = nil)
5
+ @namespace = name || @namespace
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,18 @@
1
+ module Tire
2
+ module Namespace
3
+ module Index
4
+ def self.included klass
5
+ klass.class_eval do
6
+
7
+ # Redefine the `initialize` method to prefix the index name with the namespace
8
+ #
9
+ def initialize(name, &block)
10
+ @name = Tire::Configuration.namespace ? "#{Tire::Configuration.namespace}-#{name}" : name
11
+ block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given?
12
+ end
13
+
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -64,6 +64,11 @@ module Tire
64
64
  @value
65
65
  end
66
66
 
67
+ def params(value)
68
+ @value[:params] = value
69
+ @value
70
+ end
71
+
67
72
  def to_hash
68
73
  @value[:filters] ?
69
74
  @value :
@@ -0,0 +1,44 @@
1
+ # Fuzzy Like This
2
+ # ==============
3
+
4
+ # Author: Tim van de Langkruis <tim@maximum.nl>
5
+ # Inspired by: Mereghost <marcello.rocha@gmail.com>
6
+
7
+
8
+ # Adds support for "fuzzy_like_this" and "fuzzt_like_this_field" queries in Tire DSL.
9
+
10
+ # It hooks into the Query class and inserts the fuzzy_like_this and fuzzy_like_this_field query types.
11
+
12
+
13
+ # Usage:
14
+ # ------
15
+
16
+ # Require the component:
17
+
18
+ # require 'tire/queries/fuzzy_like_this'
19
+
20
+ # From that point on you should have the fuzzy_like_this (aliased to flt) and
21
+ # the fuzzy_like_this_field (aliased to flt_field) queries available.
22
+
23
+
24
+ # Example:
25
+ # -------
26
+
27
+ # Tire.search 'articles' do
28
+ # query do
29
+ # fuzzy_like_this 'search for similar text'
30
+ # fuzzy_like_this_field :field_name, 'similar text'
31
+ # end
32
+ # end
33
+
34
+ # For available options for these queries see:
35
+
36
+ # * <http://www.elasticsearch.org/guide/reference/query-dsl/flt-query.html>
37
+ # * <http://www.elasticsearch.org/guide/reference/query-dsl/flt-field-query.html>
38
+
39
+ #
40
+ require 'tire/queries/fuzzy_like_this/fuzzy_like_this'
41
+
42
+ Tire::Search::Query.class_eval do
43
+ include Tire::Search::FuzzyLikeThis
44
+ end
@@ -0,0 +1,28 @@
1
+ module Tire
2
+ module Search
3
+ module FuzzyLikeThis
4
+ def fuzzy_like_this(like_text, options = {})
5
+ @value = {:flt => {:like_text => like_text}}
6
+ @value[:flt].update(validate_fuzzy_like_this_options(options))
7
+ @value
8
+ end
9
+
10
+ def fuzzy_like_this_field(field, like_text, options = {})
11
+ @value = {:flt_field => {field => {:like_text => like_text}}}
12
+ # :fields is invalid in this context. Better than doing some kind of meta-black magic.
13
+ options.delete(:fields)
14
+ @value[:flt_field][field].update(validate_fuzzy_like_this_options(options))
15
+ @value
16
+ end
17
+
18
+ alias_method :flt, :fuzzy_like_this
19
+ alias_method :flt_field, :fuzzy_like_this_field
20
+
21
+ private
22
+ def validate_fuzzy_like_this_options(options)
23
+ valid_options = [:fields, :ingore_tf, :max_query_terms, :min_similarity, :prefix_length, :boost, :analyzer]
24
+ options.delete_if { |key, value| !valid_options.member? key }
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,53 @@
1
+ require 'test_helper'
2
+
3
+ require 'tire/model/active_model_serializer'
4
+
5
+ class SomeClass
6
+ include ActiveModel::SerializerSupport
7
+ end
8
+
9
+ class SomeClassSerializer
10
+ end
11
+
12
+ class OtherClass
13
+ include ActiveModel::SerializerSupport
14
+ end
15
+
16
+ class TireOtherClassSerializer
17
+ end
18
+
19
+ class OtherClassSerializer
20
+ end
21
+
22
+ module Tire
23
+ module Model
24
+
25
+ class ActiveModelSerializerTest < Test::Unit::TestCase
26
+
27
+ context "Active model serializer integration in Tire models" do
28
+
29
+ should "return nil if no serializer exists" do
30
+ assert_equal nil, Tire::Results::Item.new({"_type" => "RandomClass"}).active_model_serializer
31
+ end
32
+
33
+ should "return a serializer" do
34
+ class ::Rails; end
35
+ assert_equal SomeClassSerializer, Tire::Results::Item.new({"_type" => "SomeClass"}).active_model_serializer
36
+ Object.class_eval{remove_const :Rails}
37
+ end
38
+
39
+ should "return a Tire prefixed serializer" do
40
+ class ::Rails; end
41
+ assert_equal TireOtherClassSerializer, Tire::Results::Item.new({"_type" => "OtherClass"}).active_model_serializer
42
+ Object.class_eval{remove_const :Rails}
43
+ end
44
+
45
+ should "be an ArraySerializer for Tire::Results::Collection" do
46
+ assert_equal ActiveModel::ArraySerializer, Tire::Results::Collection.new({}).active_model_serializer
47
+ end
48
+
49
+ end
50
+
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,31 @@
1
+ require 'tire'
2
+ require 'tire/namespace'
3
+ require 'shoulda'
4
+
5
+ module Tire
6
+ module Namespace
7
+ class ConfigurationTest < Test::Unit::TestCase
8
+ context "Namespace" do
9
+ context "Configuration" do
10
+ def teardown
11
+ Tire::Configuration.reset
12
+ end
13
+
14
+ should "extend Tire::Configuration with 'namespace'" do
15
+ assert_nothing_raised { Tire::Configuration.namespace("foo") }
16
+ end
17
+
18
+ should "set namespace" do
19
+ Tire::Configuration.namespace("foo")
20
+ assert_equal "foo", Tire::Configuration.instance_variable_get(:@namespace)
21
+ end
22
+
23
+ should "get namespace" do
24
+ Tire::Configuration.instance_variable_set(:@namespace, "bar")
25
+ assert_equal "bar", Tire::Configuration.namespace
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,34 @@
1
+ require 'test_helper'
2
+
3
+ require 'tire/namespace'
4
+
5
+ module Tire
6
+ module Namespace
7
+ class IndexTest < Test::Unit::TestCase
8
+ context "Namespaced index" do
9
+ teardown { Tire::Configuration.reset }
10
+
11
+ should "return the name with the namespace" do
12
+ Tire.configure { namespace "foo" }
13
+ assert_equal "foo-bar", Tire.index("bar").name
14
+ end
15
+
16
+ should "return the name without the namespace" do
17
+ assert_equal "bar", Tire.index("bar").name
18
+ end
19
+
20
+ should "work with the namespaced name across the API" do
21
+ Tire.configure { namespace "foo" }
22
+ index = Tire.index('bar')
23
+
24
+ Tire::Configuration.client.expects(:head).with do |url|
25
+ assert_equal 'http://localhost:9200/foo-bar', url
26
+ true
27
+ end.returns(mock_response('OK'))
28
+
29
+ index.exists?
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -89,8 +89,36 @@ module Tire
89
89
  end
90
90
  end
91
91
 
92
+ should "allow setting params" do
93
+ query = Query.new.custom_filters_score do
94
+ query { term :foo, 'bar' }
95
+ params :a => 'b'
96
+ end
97
+
98
+ f = query[:custom_filters_score]
99
+ assert_equal( { :term => { :foo => { :term => 'bar' } } }, f[:query].to_hash )
100
+ assert_equal( { :a => 'b' }, f[:params] )
101
+ end
102
+
103
+ should "allow using script parameters" do
104
+ score_script = "foo * 2"
105
+
106
+ query = Query.new.custom_filters_score do
107
+ query { string 'foo' }
108
+
109
+ params :foo => 42
110
+
111
+ filter do
112
+ filter :exists, :field => 'date'
113
+ script score_script
114
+ end
115
+ end
116
+
117
+ f = query[:custom_filters_score]
118
+ assert_equal 42, f[:params][:foo]
119
+ end
92
120
  end
121
+
93
122
  end
94
-
95
123
  end
96
124
  end
@@ -0,0 +1,42 @@
1
+ require 'tire'
2
+ require 'tire/queries/fuzzy_like_this'
3
+ require 'shoulda'
4
+
5
+ module Tire
6
+ module Search
7
+ class FuzzyLikeThisTest < Test::Unit::TestCase
8
+ context "Fuzzy Like This queries" do
9
+ should "search for similar documents" do
10
+ assert_equal({:flt => {:like_text => 'similar text'}}, Query.new.flt('similar text'))
11
+ end
12
+ end
13
+
14
+ should "allow to pass a list of fields to a flt query" do
15
+ assert_equal({:flt => {:like_text => 'similar text', :fields => ['foo', 'bar.baz']}},
16
+ Query.new.flt('similar text', :fields => ['foo', 'bar.baz']))
17
+
18
+ assert_equal({:flt => {:like_text => 'similar text', :fields => ['foo', 'bar.baz'], :min_similarity => 0.8}},
19
+ Query.new.flt('similar text', :fields => ['foo', 'bar.baz'], :min_similarity => 0.8))
20
+ end
21
+
22
+ should "search for similar text on a selected field (flt_field)" do
23
+ assert_equal({:flt_field => {:foo => {:like_text => 'similar text'}}},
24
+ Query.new.flt_field(:foo, 'similar text'))
25
+
26
+ assert_equal({:flt_field => {:foo => {:like_text => 'similar text', :min_similarity => 0.8}}},
27
+ Query.new.flt_field(:foo, 'similar text', :min_similarity => 0.8))
28
+ end
29
+
30
+
31
+ context "validate the options passed" do
32
+ should "drop all the invalid keys" do
33
+ assert_equal({:flt => {:like_text => 'similar text', :fields => ['foo', 'bar.baz']}},
34
+ Query.new.flt('similar text', :fields => ['foo', 'bar.baz'], :foo => :bar))
35
+
36
+ assert_equal({:flt_field => {:foo => {:like_text => 'similar text'}}},
37
+ Query.new.flt_field(:foo, 'similar text', :fields => [:bar]))
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -6,8 +6,8 @@ Gem::Specification.new do |s|
6
6
  s.name = "tire-contrib"
7
7
  s.version = Tire::Contrib::VERSION
8
8
  s.platform = Gem::Platform::RUBY
9
- s.authors = ["Karel Minarik", "Oliver Eilhard", "Dave Kinkead"]
10
- s.email = ["karmi@karmi.cz", "oliver.eilhard@gmail.com", "dave@kinkead.com.au"]
9
+ s.authors = ["Karel Minarik", "Oliver Eilhard", "Dave Kinkead", "Sergey Efremov"]
10
+ s.email = ["karmi@karmi.cz", "oliver.eilhard@gmail.com", "dave@kinkead.com.au", "efremov.sergey@gmail.com"]
11
11
  s.homepage = ""
12
12
  s.summary = %q{Contributions and additions for the Tire gem}
13
13
 
@@ -19,6 +19,8 @@ Gem::Specification.new do |s|
19
19
 
20
20
  s.add_development_dependency "newrelic_rpm"
21
21
 
22
+ s.add_development_dependency "active_model_serializers"
23
+
22
24
  s.add_development_dependency "turn"
23
25
  s.add_development_dependency "shoulda"
24
26
  s.add_development_dependency "mocha"
metadata CHANGED
@@ -1,17 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tire-contrib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Karel Minarik
9
9
  - Oliver Eilhard
10
10
  - Dave Kinkead
11
+ - Sergey Efremov
11
12
  autorequire:
12
13
  bindir: bin
13
14
  cert_chain: []
14
- date: 2013-05-21 00:00:00.000000000 Z
15
+ date: 2013-07-05 00:00:00.000000000 Z
15
16
  dependencies:
16
17
  - !ruby/object:Gem::Dependency
17
18
  name: tire
@@ -61,6 +62,22 @@ dependencies:
61
62
  - - ! '>='
62
63
  - !ruby/object:Gem::Version
63
64
  version: '0'
65
+ - !ruby/object:Gem::Dependency
66
+ name: active_model_serializers
67
+ requirement: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ type: :development
74
+ prerelease: false
75
+ version_requirements: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
64
81
  - !ruby/object:Gem::Dependency
65
82
  name: turn
66
83
  requirement: !ruby/object:Gem::Requirement
@@ -162,6 +179,7 @@ email:
162
179
  - karmi@karmi.cz
163
180
  - oliver.eilhard@gmail.com
164
181
  - dave@kinkead.com.au
182
+ - efremov.sergey@gmail.com
165
183
  executables: []
166
184
  extensions: []
167
185
  extra_rdoc_files:
@@ -179,13 +197,20 @@ files:
179
197
  - lib/tire-contrib/version.rb
180
198
  - lib/tire/README.markdown
181
199
  - lib/tire/http/clients/pooled_curb.rb
200
+ - lib/tire/model/active_model_serializer.rb
201
+ - lib/tire/model/active_model_serializer/serializer_support.rb
182
202
  - lib/tire/model/dynamic_persistence.rb
183
203
  - lib/tire/model/dynamic_persistence/README.markdown
184
204
  - lib/tire/model/dynamic_persistence/dynamic_persistence.rb
205
+ - lib/tire/namespace.rb
206
+ - lib/tire/namespace/configuration.rb
207
+ - lib/tire/namespace/index.rb
185
208
  - lib/tire/new_relic/instrumentation.rb
186
209
  - lib/tire/new_relic/model.rb
187
210
  - lib/tire/queries/custom_filters_score.rb
188
211
  - lib/tire/queries/custom_filters_score/custom_filters_score.rb
212
+ - lib/tire/queries/fuzzy_like_this.rb
213
+ - lib/tire/queries/fuzzy_like_this/fuzzy_like_this.rb
189
214
  - lib/tire/queries/more_like_this.rb
190
215
  - lib/tire/queries/more_like_this/more_like_this.rb
191
216
  - lib/tire/rails/logger.rb
@@ -193,11 +218,15 @@ files:
193
218
  - lib/tire/rails/logger/instrumentation.rb
194
219
  - lib/tire/rails/logger/log_subscriber.rb
195
220
  - lib/tire/rails/logger/railtie.rb
221
+ - test/active_model_serializer/active_model_serializer_test.rb
196
222
  - test/dynamic_persistence/dynamic_persistence_test.rb
223
+ - test/namespace/configuration_test.rb
224
+ - test/namespace/index_test.rb
197
225
  - test/new_relic/config/newrelic.yml
198
226
  - test/new_relic/instrumentation_test.rb
199
227
  - test/new_relic/model.rb
200
228
  - test/queries/custom_filters_score/custom_filters_score_test.rb
229
+ - test/queries/fuzzy_like_this/fuzzy_like_this_test.rb
201
230
  - test/queries/more_like_this/more_like_this_test.rb
202
231
  - test/rails/logger/active_record_article.rb
203
232
  - test/rails/logger/log_subscriber_test.rb
@@ -229,11 +258,15 @@ signing_key:
229
258
  specification_version: 3
230
259
  summary: Contributions and additions for the Tire gem
231
260
  test_files:
261
+ - test/active_model_serializer/active_model_serializer_test.rb
232
262
  - test/dynamic_persistence/dynamic_persistence_test.rb
263
+ - test/namespace/configuration_test.rb
264
+ - test/namespace/index_test.rb
233
265
  - test/new_relic/config/newrelic.yml
234
266
  - test/new_relic/instrumentation_test.rb
235
267
  - test/new_relic/model.rb
236
268
  - test/queries/custom_filters_score/custom_filters_score_test.rb
269
+ - test/queries/fuzzy_like_this/fuzzy_like_this_test.rb
237
270
  - test/queries/more_like_this/more_like_this_test.rb
238
271
  - test/rails/logger/active_record_article.rb
239
272
  - test/rails/logger/log_subscriber_test.rb