diff --git a/.circleci/config.yml b/.circleci/config.yml index 03f9a6a7f..b44179a2a 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -23,8 +23,6 @@ workflows: - rspec-ruby-27-activerecord52 - rspec-ruby-27-mongoid - rspec-ruby-30-activerecord61 - - rspec-ruby-27-activerecord61-es6 - - rspec-ruby-30-activerecord61-es6 commands: rspec-test: @@ -214,27 +212,3 @@ jobs: GEMFILE: gemfiles/rails.6.1.activerecord.gemfile steps: - rspec-test - - rspec-ruby-27-activerecord61-es6: - docker: - - image: circleci/ruby:2.7 - - image: docker.elastic.co/elasticsearch/elasticsearch:6.8.13 - environment: - <<: *es-env - working_directory: ~/repo - environment: - GEMFILE: gemfiles/rails.6.1.activerecord.gemfile - steps: - - rspec-test - - rspec-ruby-30-activerecord61-es6: - docker: - - image: circleci/ruby:3.0 - - image: docker.elastic.co/elasticsearch/elasticsearch:6.8.13 - environment: - <<: *es-env - working_directory: ~/repo - environment: - GEMFILE: gemfiles/rails.6.1.activerecord.gemfile - steps: - - rspec-test diff --git a/CHANGELOG.md b/CHANGELOG.md index 3bd25e130..d301d87a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,17 @@ ### Bugs Fixed +## 7.1.0 (2021-03-03) + +### Changes + + * [#766](https://github.com/toptal/chewy/pull/766): **(Breaking)** Drop support for Elasticsearch 6.x ([@rabotyaga][]) + * [#765](https://github.com/toptal/chewy/pull/765): Fix ruby 2.7 warnings in rake tasks ([@aglushkov][]) + +### Bugs Fixed + + * [#722](https://github.com/toptal/chewy/issues/722): Remove alias_method_chain, use Module#prepend instead ([@dalthon][]) + ## 7.0.1 (2021-05-03) ### Changes diff --git a/README.md b/README.md index 3a31309ac..727d987b4 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,6 @@ Chewy is an ODM (Object Document Mapper), built on top of the [the official Elas * [Index definition](#index-definition) * [Type default import options](#type-default-import-options) * [Multi (nested) and object field types](#multi-nested-and-object-field-types) - * [Parent and children types](#parent-and-children-types) * [Geo Point fields](#geo-point-fields) * [Crutches™ technology](#crutches-technology) * [Witchcraft™ technology](#witchcraft-technology) @@ -55,10 +54,6 @@ Chewy is an ODM (Object Document Mapper), built on top of the [the official Elas In this section we'll cover why you might want to use Chewy instead of the official `elasticsearch-ruby` client gem. -* Multi-model indices. - - Index classes are independent from ORM/ODM models. Now, implementing e.g. cross-model autocomplete is much easier. You can just define the index and work with it in an object-oriented style. You can define several types for index - one per indexed model. - * Every index is observable by all the related models. Most of the indexed models are related to other and sometimes it is necessary to denormalize this related data and put at the same object. For example, you need to index an array of tags together with an article. Chewy allows you to specify an updateable index for every model separately - so corresponding articles will be reindexed on any tag update. @@ -99,6 +94,7 @@ Chewy is compatible with MRI 2.5-3.0¹. | Chewy version | Elasticsearch version | | ------------- | ---------------------------------- | +| 7.1.x | 7.x | | 7.0.0 | 6.8, 7.x | | 6.0.0 | 5.x, 6.x | | 5.x | 5.x, limited support for 1.x & 2.x | @@ -365,18 +361,6 @@ end The `value:` option for internal fields will no longer be effective. -### Parent and children types - -To define [parent](https://www.elastic.co/guide/en/elasticsearch/guide/current/parent-child-mapping.html) type for a given index_type, you can include root options for the type where you can specify parent_type and parent_id - -```ruby -define_type User.includes(:account) do - root parent: 'account', parent_id: ->{ account_id } do - field :created_at, type: 'date' - field :task_id, type: 'integer' - end -end -``` ### Geo Point fields You can use [Elasticsearch's geo mapping](https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-point.html) with the `geo_point` field type, allowing you to query, filter and order by latitude and longitude. You can use the following hash format: @@ -602,7 +586,7 @@ Imagine that you reset your index in a zero-downtime manner (to separate index), ### Types access -You can access index-defined types with the following API: +You can access index-defined type with the following API: ```ruby UsersIndex::User # => UsersIndex::User @@ -630,11 +614,10 @@ UsersIndex::User.import # import with 0 arguments process all the data specified UsersIndex::User.import User.where('rating > 100') # or import specified users scope UsersIndex::User.import User.where('rating > 100').to_a # or import specified users array UsersIndex::User.import [1, 2, 42] # pass even ids for import, it will be handled in the most effective way -UsersIndex::User.import User.where('rating > 100'), update_fields: [:email] # if update fields are specified - it will update their values only with the `update` bulk action. +UsersIndex::User.import User.where('rating > 100'), update_fields: [:email] # if update fields are specified - it will update their values only with the `update` bulk action -UsersIndex.import # import every defined type -UsersIndex.import user: User.where('rating > 100') # import only active users to `user` type. - # Other index types, if exists, will be imported with default scope from the type definition. +UsersIndex.import # same as UsersIndex::User.import +UsersIndex.import user: User.where('rating > 100') # import only specified users UsersIndex.reset! # purges index and imports default data for all types ``` @@ -923,19 +906,25 @@ Quick introduction. The request DSL have the same chainable nature as AR or Mongoid ones. The main class is `Chewy::Search::Request`. It is possible to perform requests on behalf of indices or types: ```ruby -PlaceIndex.query(match: {name: 'London'}) # returns documents of any type -PlaceIndex::City.query(match: {name: 'London'}) # returns cities only. +CitiesIndex.query(match: {name: 'London'}) # or +CitiesIndex::City.query(match: {name: 'London'}) ``` Main methods of the request DSL are: `query`, `filter` and `post_filter`, it is possible to pass pure query hashes or use `elasticsearch-dsl`. Also, there is an additional ```ruby -PlaceIndex +CitiesIndex .filter(term: {name: 'Bangkok'}) .query { match name: 'London' } .query.not(range: {population: {gt: 1_000_000}}) ``` +You can query a set of indexes at once: + +```ruby +CitiesIndex.indices(CountriesIndex).query(match: {name: 'Some'}) +``` + See https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html and https://github.com/elastic/elasticsearch-ruby/tree/master/elasticsearch-dsl for more details. An important part of requests manipulation is merging. There are 4 methods to perform it: `merge`, `and`, `or`, `not`. See [Chewy::Search::QueryProxy](lib/chewy/search/query_proxy.rb) for details. Also, `only` and `except` methods help to remove unneeded parts of the request. @@ -943,7 +932,7 @@ An important part of requests manipulation is merging. There are 4 methods to pe Every other request part is covered by a bunch of additional methods, see [Chewy::Search::Request](lib/chewy/search/request.rb) for details: ```ruby -PlaceIndex.limit(10).offset(30).order(:name, {population: {order: :desc}}) +CitiesIndex.limit(10).offset(30).order(:name, {population: {order: :desc}}) ``` Request DSL also provides additional scope actions, like `delete_all`, `exists?`, `count`, `pluck`, etc. @@ -969,8 +958,8 @@ See [Chewy::Search::Scrolling](lib/chewy/search/scrolling.rb) for details. It is possible to load ORM/ODM source objects with the `objects` method. To provide additional loading options use `load` method: ```ruby -PlacesIndex.load(scope: -> { active }).to_a # to_a returns `Chewy::Type` wrappers. -PlacesIndex.load(scope: -> { active }).objects # An array of AR source objects. +CitiesIndex.load(scope: -> { active }).to_a # to_a returns `Chewy::Type` wrappers. +CitiesIndex.load(scope: -> { active }).objects # An array of AR source objects. ``` See [Chewy::Search::Loader](lib/chewy/search/loader.rb) for more details. @@ -978,7 +967,7 @@ See [Chewy::Search::Loader](lib/chewy/search/loader.rb) for more details. In case when it is necessary to iterate through both of the wrappers and objects simultaneously, `object_hash` method helps a lot: ```ruby -scope = PlacesIndex.load(scope: -> { active }) +scope = CitiesIndex.load(scope: -> { active }) scope.each do |wrapper| scope.object_hash[wrapper] end @@ -995,8 +984,8 @@ Performs zero-downtime reindexing as described [here](https://www.elastic.co/blo ```bash rake chewy:reset # resets all the existing indices rake chewy:reset[users] # resets UsersIndex only -rake chewy:reset[users,places] # resets UsersIndex and PlacesIndex -rake chewy:reset[-users,places] # resets every index in the application except specified ones +rake chewy:reset[users,cities] # resets UsersIndex and CitiesIndex +rake chewy:reset[-users,cities] # resets every index in the application except specified ones ``` #### `chewy:upgrade` @@ -1011,8 +1000,8 @@ See [Chewy::Stash::Specification](lib/chewy/stash.rb) and [Chewy::Index::Specifi ```bash rake chewy:upgrade # upgrades all the existing indices rake chewy:upgrade[users] # upgrades UsersIndex only -rake chewy:upgrade[users,places] # upgrades UsersIndex and PlacesIndex -rake chewy:upgrade[-users,places] # upgrades every index in the application except specified ones +rake chewy:upgrade[users,cities] # upgrades UsersIndex and CitiesIndex +rake chewy:upgrade[-users,cities] # upgrades every index in the application except specified ones ``` #### `chewy:update` @@ -1024,8 +1013,8 @@ Unlike `reset` or `upgrade` tasks, it is possible to pass type references to upd ```bash rake chewy:update # updates all the existing indices rake chewy:update[users] # updates UsersIndex only -rake chewy:update[users,places#city] # updates the whole UsersIndex and PlacesIndex::City type -rake chewy:update[-users,places#city] # updates every index in the application except every type defined in UsersIndex and the rest of the types defined in PlacesIndex +rake chewy:update[users,cities#city] # updates UsersIndex and CitiesIndex (if City type is defined on CitiesIndex) +rake chewy:update[-users,cities#city] # updates every index in the application except UsersIndex and CitiesIndex::City ``` #### `chewy:sync` @@ -1039,8 +1028,8 @@ See [Chewy::Type::Syncer](lib/chewy/type/syncer.rb) for more details. ```bash rake chewy:sync # synchronizes all the existing indices rake chewy:sync[users] # synchronizes UsersIndex only -rake chewy:sync[users,places#city] # synchronizes the whole UsersIndex and PlacesIndex::City type -rake chewy:sync[-users,places#city] # synchronizes every index in the application except every type defined in UsersIndex and the rest of the types defined in PlacesIndex +rake chewy:sync[users,cities#city] # synchronizes UsersIndex and CitiesIndex (if City type is defined on CitiesIndex) +rake chewy:sync[-users,cities#city] # synchronizes every index in the application except except UsersIndex and CitiesIndex::City ``` #### `chewy:deploy` @@ -1064,7 +1053,7 @@ If the number of processes is not specified explicitly - `parallel` gem tries to ```bash rake chewy:parallel:reset rake chewy:parallel:upgrade[4] -rake chewy:parallel:update[4,places#city] +rake chewy:parallel:update[4,cities#city] rake chewy:parallel:sync[4,-users] rake chewy:parallel:deploy[4] # performs parallel upgrade and parallel sync afterwards ``` diff --git a/lib/chewy/fields/root.rb b/lib/chewy/fields/root.rb index 3e25f8cdb..e97c171fa 100644 --- a/lib/chewy/fields/root.rb +++ b/lib/chewy/fields/root.rb @@ -29,8 +29,7 @@ def mappings_hash mappings[name][:dynamic_templates].concat dynamic_templates end - mappings[name][:_parent] = parent.is_a?(Hash) ? parent : {type: parent} if parent - mappings + mappings[name] end def dynamic_template(*args) diff --git a/lib/chewy/index.rb b/lib/chewy/index.rb index b05a9b60d..48b940aa8 100644 --- a/lib/chewy/index.rb +++ b/lib/chewy/index.rb @@ -152,23 +152,12 @@ def define_type(target, options = {}, &block) self.type_hash = type_hash.merge(type_class.type_name => type_class) end - # Types method has double usage. - # If no arguments are passed - it returns array of defined types: + # Returns defined type: # - # UsersIndex.types # => [UsersIndex::Admin, UsersIndex::Manager, UsersIndex::User] + # UsersIndex.types # => [UsersIndex::User] # - # If arguments are passed it treats like a part of chainable query DSL and - # adds types array for index to select. - # - # UsersIndex.filters { name =~ 'ro' }.types(:admin, :manager) - # UsersIndex.types(:admin, :manager).filters { name =~ 'ro' } # the same as the first example - # - def types(*args) - if args.present? - all.types(*args) - else - type_hash.values - end + def types + type_hash.values end # Returns defined types names: diff --git a/lib/chewy/index/actions.rb b/lib/chewy/index/actions.rb index 9183b8927..ed25d4d00 100644 --- a/lib/chewy/index/actions.rb +++ b/lib/chewy/index/actions.rb @@ -59,9 +59,7 @@ def create!(suffix = nil, **options) body = specification_hash body[:aliases] = {general_name => {}} if options[:alias] && suffixed_name != general_name - args = {index: suffixed_name, body: body} - args[:include_type_name] = true if Runtime.version >= '6.7.0' - result = client.indices.create(**args) + result = client.indices.create(index: suffixed_name, body: body) Chewy.wait_for_status if result result diff --git a/lib/chewy/index/aliases.rb b/lib/chewy/index/aliases.rb index 21feebf8d..cfcf74732 100644 --- a/lib/chewy/index/aliases.rb +++ b/lib/chewy/index/aliases.rb @@ -5,9 +5,7 @@ module Aliases module ClassMethods def indexes - get_args = {index: index_name} - get_args[:include_type_name] = true if Runtime.version >= '6.7.0' - indexes = empty_if_not_found { client.indices.get(**get_args).keys } + indexes = empty_if_not_found { client.indices.get(index: index_name).keys } indexes += empty_if_not_found { client.indices.get_alias(name: index_name).keys } indexes.compact.uniq end diff --git a/lib/chewy/railtie.rb b/lib/chewy/railtie.rb index eee33adee..5d1c48104 100644 --- a/lib/chewy/railtie.rb +++ b/lib/chewy/railtie.rb @@ -22,17 +22,6 @@ def call(env) end module MigrationStrategy - extend ActiveSupport::Concern - included do - alias_method_chain :migrate, :chewy - end - - def migrate_with_chewy(*args) - Chewy.strategy(:bypass) { migrate_without_chewy(*args) } - end - end - - module Rails5MigrationStrategy def migrate(*args) Chewy.strategy(:bypass) { super } end @@ -57,13 +46,8 @@ def migrate(*args) initializer 'chewy.migration_strategy' do ActiveSupport.on_load(:active_record) do - if Rails::VERSION::MAJOR >= 5 - ActiveRecord::Migration.prepend(Rails5MigrationStrategy) - ActiveRecord::Migrator.prepend(Rails5MigrationStrategy) if defined? ActiveRecord::Migrator - else - ActiveRecord::Migration.send(:include, MigrationStrategy) - ActiveRecord::Migrator.send(:include, MigrationStrategy) if defined? ActiveRecord::Migrator - end + ActiveRecord::Migration.prepend(MigrationStrategy) + ActiveRecord::Migrator.prepend(MigrationStrategy) if defined? ActiveRecord::Migrator end end diff --git a/lib/chewy/search/loader.rb b/lib/chewy/search/loader.rb index cab2798ff..1a86d07bb 100644 --- a/lib/chewy/search/loader.rb +++ b/lib/chewy/search/loader.rb @@ -9,14 +9,10 @@ module Search # @see Chewy::Search::Scrolling#scroll_objects class Loader # @param indexes [Array] list of indexes to lookup types - # @param only [Array] list of selected type names to load - # @param except [Array] list of type names which will not be loaded # @param options [Hash] adapter-specific load options # @see Chewy::Type::Adapter::Base#load - def initialize(indexes: [], only: [], except: [], **options) + def initialize(indexes: [], **options) @indexes = indexes - @only = Array.wrap(only).map(&:to_s) - @except = Array.wrap(except).map(&:to_s) @options = options end @@ -31,7 +27,7 @@ def derive_type(index, type) (@derive_type ||= {})[[index, type]] ||= begin index_class = derive_index(index) raise Chewy::UnderivableType, "Can not find index named `#{index}`" unless index_class - index_class.type_hash[type] or raise Chewy::UnderivableType, "Index `#{index}` doesn`t have type named `#{type}`" + index_class.type_hash.values.first end end @@ -48,8 +44,6 @@ def derive_type(index, type) def load(hits) hit_groups = hits.group_by { |hit| [hit['_index'], hit['_type']] } loaded_objects = hit_groups.each_with_object({}) do |((index_name, type_name), hit_group), result| - next if skip_type?(type_name) - type = derive_type(index_name, type_name) ids = hit_group.map { |hit| hit['_id'] } loaded = type.adapter.load(ids, **@options.merge(_type: type)) @@ -74,10 +68,6 @@ def derive_index(index_name) def indexes_hash @indexes_hash ||= @indexes.index_by(&:index_name) end - - def skip_type?(type_name) - @except.include?(type_name) || @only.present? && !@only.include?(type_name) - end end end end diff --git a/lib/chewy/search/parameters/indices.rb b/lib/chewy/search/parameters/indices.rb index 8d0fd1d7f..dc3dc28ca 100644 --- a/lib/chewy/search/parameters/indices.rb +++ b/lib/chewy/search/parameters/indices.rb @@ -3,70 +3,48 @@ module Chewy module Search class Parameters - # Stores indices and/or types to query. + # Stores indices to query. # Renders it to lists of string accepted by ElasticSearch # API. # - # The semantics behind it can be described in the - # following statements: - # 1. If index is added to the storage, no matter, a class + # If index is added to the storage, no matter, a class # or a string/symbol, it gets appended to the list. - # 2. If type is added to the storage, it filters out types - # assigned via indices. - # 3. But when a type class with non-existing index is added, - # this index got also added to the list if indices. - # 4. In cases when of an index identifier added, type - # indetifiers also got appended instead of filtering. class Indices < Storage # Two index storages are equal if they produce the # same output on render. # # @see Chewy::Search::Parameters::Storage#== # @param other [Chewy::Search::Parameters::Storage] any storage instance - # @return [true, false] the result of comparision + # @return [true, false] the result of comparison def ==(other) super || other.class == self.class && other.render == render end - # Just adds types to types and indices to indices. + # Just adds indices to indices. # # @see Chewy::Search::Parameters::Storage#update! - # @param other_value [{Symbol => Array}] any acceptable storage value - # @return [{Symbol => Array}] updated value + # @param other_value [{Symbol => Array}] any acceptable storage value + # @return [{Symbol => Array}] updated value def update!(other_value) new_value = normalize(other_value) - @value = { - indices: value[:indices] | new_value[:indices], - types: value[:types] | new_value[:types] - } + @value = {indices: value[:indices] | new_value[:indices]} end - # Returns desired index and type names. + # Returns desired index names. # # @see Chewy::Search::Parameters::Storage#render # @return [{Symbol => Array}] rendered value with the parameter name def render - { - index: index_names.uniq.sort, - type: type_names.uniq.sort - }.reject { |_, v| v.blank? } + {index: index_names.uniq.sort}.reject { |_, v| v.blank? } end # Returns index classes used for the request. - # No strings/symbos included. + # No strings/symbols included. # # @return [Array] a list of index classes def indices - index_classes | type_classes.map(&:index) - end - - # Returns type classes used for the request. - # No strings/symbos included. - # - # @return [Array] a list of types classes - def types - type_classes | (index_classes - type_classes.map(&:index)).flat_map(&:types) + index_classes end private @@ -78,10 +56,7 @@ def initialize_clone(origin) def normalize(value) value ||= {} - { - indices: Array.wrap(value[:indices]).flatten.compact, - types: Array.wrap(value[:types]).flatten.compact - } + {indices: Array.wrap(value[:indices]).flatten.compact} end def index_classes @@ -97,26 +72,6 @@ def index_identifiers def index_names indices.map(&:index_name) | index_identifiers.map(&:to_s) end - - def type_classes - value[:types].select do |klass| - klass.is_a?(Class) && klass < Chewy::Type - end - end - - def type_identifiers - value[:types] - type_classes - end - - def type_names - type_names = types.map(&:type_name) - - if index_identifiers.blank? && type_identifiers.present? - (type_names & type_identifiers.map(&:to_s)).presence || type_names - else - type_names | type_identifiers.map(&:to_s) - end - end end end end diff --git a/lib/chewy/search/parameters/types.rb b/lib/chewy/search/parameters/types.rb deleted file mode 100644 index 3e9465096..000000000 --- a/lib/chewy/search/parameters/types.rb +++ /dev/null @@ -1,20 +0,0 @@ -require 'chewy/search/parameters/storage' - -module Chewy - module Search - class Parameters - # A standard string array storage with one exception: rendering is empty. - # - # @see Chewy::Search::Parameters::StringArrayStorage - class Types < Storage - include StringArrayStorage - - # Doesn't render anything, has specialized rendering logic in - # {Chewy::Search::Request} - # - # @return [nil] - def render; end - end - end - end -end diff --git a/lib/chewy/search/request.rb b/lib/chewy/search/request.rb index 482e6e25b..e151a8936 100644 --- a/lib/chewy/search/request.rb +++ b/lib/chewy/search/request.rb @@ -41,7 +41,7 @@ class Request EXTRA_STORAGES = %i[aggs suggest].freeze # An array of storage names that are changing the returned hist collection in any way. WHERE_STORAGES = %i[ - query filter post_filter none types min_score rescore indices_boost + query filter post_filter none min_score rescore indices_boost ].freeze delegate :hits, :wrappers, :objects, :records, :documents, @@ -60,11 +60,11 @@ class Request # Chewy::Search::Request.new(:places) # # => ["places"]}> # Chewy::Search::Request.new(PlacesIndex) - # # => ["places"], :type=>["city", "country"]}> + # # => ["places"]}> # Chewy::Search::Request.new(PlacesIndex::City) - # # => ["places"], :type=>["city"]}> + # # => ["places"]}> # Chewy::Search::Request.new(UsersIndex, PlacesIndex::City) - # # => ["users", "places"], :type=>["city", "user"]}> + # # => ["users", "places"]}> # @param indexes_or_types [Array] indices and types in any combinations def initialize(*indices_or_types) indices = indices_or_types.reject do |klass| @@ -75,8 +75,10 @@ def initialize(*indices_or_types) klass.is_a?(Class) && klass < Chewy::Type end + indices += types.map(&:index) + parameters.modify!(:indices) do - replace!(indices: indices, types: types) + replace!(indices: indices) end end @@ -305,34 +307,18 @@ def inspect end end - # @!method indices(*values) - # Modifies `index` request parameter. Updates the storage on every call. - # Added passed indexes to the parameter list. - # - # @example - # UsersIndex.indices(CitiesIndex).indices(:another) - # # => ["another", "cities", "users"], :type=>["city", "user"]}> - # @see Chewy::Search::Parameters::Indices - # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html - # @param values [Array] index names - # @return [Chewy::Search::Request] + # Modifies `index` request parameter. Updates the storage on every call. + # Added passed indexes to the parameter list. # - # @!method types(*values) - # Modifies `type` request parameter. Updates the storage on every call. - # Constrains types passed on the request initialization or adds them - # to the list depending on circumstances. - # - # @example - # UsersIndex.types(CitiesIndex::City).types(:unexistent) - # # => ["cities", "users"], :type=>["city", "user"]}> - # @see Chewy::Search::Parameters::Indices - # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html - # @param values [Array] type names - # @return [Chewy::Search::Request] - %i[indices types].each do |name| - define_method name do |value, *values| - modify(:indices) { update!(name => [value, *values]) } - end + # @example + # UsersIndex.indices(CitiesIndex).indices(:another) + # # => ["another", "cities", "users"]}> + # @see Chewy::Search::Parameters::Indices + # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html + # @param values [Array] index names + # @return [Chewy::Search::Request] + def indices(value, *values) + modify(:indices) { update!(indices: [value, *values]) } end # @overload reorder(*values) @@ -990,7 +976,6 @@ def reset def perform(additional = {}) request_body = render.merge(additional) - request_body[:rest_total_hits_as_int] = true if Chewy::Runtime::Version.new(Chewy.client.info['version']['number']) >= '7.0.0' ActiveSupport::Notifications.instrument 'search_query.chewy', notification_payload(request: request_body) do begin @@ -1003,9 +988,8 @@ def perform(additional = {}) def notification_payload(additional) { - indexes: _indices, types: _types, - index: _indices.one? ? _indices.first : _indices, - type: _types.one? ? _types.first : _types + indexes: _indices, + index: _indices.one? ? _indices.first : _indices }.merge(additional) end @@ -1013,10 +997,6 @@ def _indices parameters[:indices].indices end - def _types - parameters[:indices].types - end - def raw_limit_value parameters[:limit].value end diff --git a/lib/chewy/search/response.rb b/lib/chewy/search/response.rb index 6ee733f04..9169d0367 100644 --- a/lib/chewy/search/response.rb +++ b/lib/chewy/search/response.rb @@ -23,7 +23,7 @@ def hits # # @return [Integer] def total - @total ||= hits_root['total'] || 0 + @total ||= hits_root.fetch('total', {}).fetch('value', 0) end # Response `max_score` field. diff --git a/lib/chewy/search/scrolling.rb b/lib/chewy/search/scrolling.rb index f3baf4534..b71f21d75 100644 --- a/lib/chewy/search/scrolling.rb +++ b/lib/chewy/search/scrolling.rb @@ -28,7 +28,7 @@ def scroll_batches(batch_size: Request::DEFAULT_BATCH_SIZE, scroll: Request::DEF return enum_for(:scroll_batches, batch_size: batch_size, scroll: scroll) unless block_given? result = perform(size: batch_size, scroll: scroll) - total = [raw_limit_value, result.fetch('hits', {}).fetch('total', 0)].compact.min + total = [raw_limit_value, result.fetch('hits', {}).fetch('total', {}).fetch('value', 0)].compact.min last_batch_size = total % batch_size fetched = 0 scroll_id = nil diff --git a/lib/chewy/stash.rb b/lib/chewy/stash.rb index fc3b5fc24..6d461d116 100644 --- a/lib/chewy/stash.rb +++ b/lib/chewy/stash.rb @@ -46,11 +46,8 @@ def self.for(*something) types = something.flat_map { |s| Chewy.derive_types(s) } return none if something.present? && types.blank? scope = all - types.group_by(&:index).each do |index, index_types| - scope = scope.or( - filter(term: {index_name: index.derivable_name}) - .filter(terms: {type_name: index_types.map(&:type_name)}) - ) + types.map(&:index).uniq.each do |index| + scope = scope.or(filter(term: {index_name: index.derivable_name})) end scope end diff --git a/lib/chewy/type/import/bulk_request.rb b/lib/chewy/type/import/bulk_request.rb index b87423b78..fb94e4aa5 100644 --- a/lib/chewy/type/import/bulk_request.rb +++ b/lib/chewy/type/import/bulk_request.rb @@ -47,8 +47,7 @@ def perform(body) def request_base @request_base ||= { - index: @type.index_name(suffix: @suffix), - type: @type.type_name + index: @type.index_name(suffix: @suffix) }.merge!(@bulk_options) end diff --git a/lib/chewy/type/import/journal_builder.rb b/lib/chewy/type/import/journal_builder.rb index 8402aa7f4..510c96a93 100644 --- a/lib/chewy/type/import/journal_builder.rb +++ b/lib/chewy/type/import/journal_builder.rb @@ -17,8 +17,7 @@ def bulk_body ].compact ).bulk_body.each do |item| item.values.first.merge!( - _index: Chewy::Stash::Journal.index_name, - _type: Chewy::Stash::Journal::Journal.type_name + _index: Chewy::Stash::Journal.index_name ) end end diff --git a/lib/chewy/type/import/routine.rb b/lib/chewy/type/import/routine.rb index f24ece155..777838356 100644 --- a/lib/chewy/type/import/routine.rb +++ b/lib/chewy/type/import/routine.rb @@ -69,8 +69,8 @@ def create_indexes! @type.index.create!(**@bulk_options.slice(:suffix)) unless @type.index.exists? end - # The main process method. Converts passed objects to thr bulk request body, - # appends journal entires, performs this request and handles errors performing + # The main process method. Converts passed objects to the bulk request body, + # appends journal entries, performs this request and handles errors performing # failover procedures if applicable. # # @param index [Array] any acceptable objects for indexing diff --git a/lib/chewy/type/mapping.rb b/lib/chewy/type/mapping.rb index f02355510..5efd5ffec 100644 --- a/lib/chewy/type/mapping.rb +++ b/lib/chewy/type/mapping.rb @@ -183,7 +183,7 @@ def template(*args) # Returns compiled mappings hash for current type # def mappings_hash - root.mappings_hash[type_name.to_sym].present? ? root.mappings_hash : {} + root.mappings_hash end # Check whether the type has outdated_sync_field defined with a simple value. diff --git a/lib/chewy/type/syncer.rb b/lib/chewy/type/syncer.rb index db4e7794a..01121b50b 100644 --- a/lib/chewy/type/syncer.rb +++ b/lib/chewy/type/syncer.rb @@ -205,12 +205,9 @@ def outdated_sync_field_type return @outdated_sync_field_type if instance_variable_defined?(:@outdated_sync_field_type) return unless @type.outdated_sync_field - args = {index: @type.index_name, type: @type.type_name} - args[:include_type_name] = true if Runtime.version >= '6.7.0' - mappings = @type.client.indices.get_mapping(**args).values.first.fetch('mappings', {}) + mappings = @type.client.indices.get_mapping(index: @type.index_name).values.first.fetch('mappings', {}) @outdated_sync_field_type = mappings - .fetch(@type.type_name, {}) .fetch('properties', {}) .fetch(@type.outdated_sync_field.to_s, {})['type'] rescue Elasticsearch::Transport::Transport::Errors::NotFound diff --git a/lib/chewy/version.rb b/lib/chewy/version.rb index 9b5a23866..1fd9c9304 100644 --- a/lib/chewy/version.rb +++ b/lib/chewy/version.rb @@ -1,3 +1,3 @@ module Chewy - VERSION = '7.0.1'.freeze + VERSION = '7.1.0'.freeze end diff --git a/spec/chewy/fields/base_spec.rb b/spec/chewy/fields/base_spec.rb index d62e9e840..8240c0e33 100644 --- a/spec/chewy/fields/base_spec.rb +++ b/spec/chewy/fields/base_spec.rb @@ -143,7 +143,7 @@ end specify do - expect(EventsIndex::Event.mappings_hash).to eq(event: { + expect(EventsIndex::Event.mappings_hash).to eq( properties: { id: {type: 'integer'}, category: { @@ -160,7 +160,7 @@ } } } - }) + ) end end @@ -296,7 +296,7 @@ end specify do - expect(EventsIndex::Event.mappings_hash).to eq(event: { + expect(EventsIndex::Event.mappings_hash).to eq( properties: { id: {type: 'integer'}, name: { @@ -307,7 +307,7 @@ }, category: {type: 'object'} } - }) + ) end specify do diff --git a/spec/chewy/fields/root_spec.rb b/spec/chewy/fields/root_spec.rb index 63e456a60..6bf1dbeca 100644 --- a/spec/chewy/fields/root_spec.rb +++ b/spec/chewy/fields/root_spec.rb @@ -13,7 +13,7 @@ field.dynamic_template template_42: {mapping: {}, match: ''} field.dynamic_template(/hello\..*/) - expect(field.mappings_hash).to eq(product: {dynamic_templates: [ + expect(field.mappings_hash).to eq(dynamic_templates: [ {template_1: {mapping: {type: 'keyword'}, match: 'hello'}}, {template_2: {mapping: {}, match_mapping_type: 'integer', match: 'hello*'}}, {template_3: {mapping: {}, path_match: 'hello.*'}}, @@ -21,7 +21,7 @@ {template_5: {mapping: {}, match: 'hello.*', match_pattern: 'regexp'}}, {template_42: {mapping: {}, match: ''}}, {template_7: {mapping: {}, path_match: 'hello\..*', match_pattern: 'regexp'}} - ]}) + ]) end context do @@ -33,10 +33,10 @@ specify do field.dynamic_template 'hello', type: 'keyword' - expect(field.mappings_hash).to eq(product: {dynamic_templates: [ + expect(field.mappings_hash).to eq(dynamic_templates: [ {template_42: {mapping: {}, match: ''}}, {template_1: {mapping: {type: 'keyword'}, match: 'hello'}} - ]}) + ]) end end end diff --git a/spec/chewy/index/specification_spec.rb b/spec/chewy/index/specification_spec.rb index 744a1c172..07f997edf 100644 --- a/spec/chewy/index/specification_spec.rb +++ b/spec/chewy/index/specification_spec.rb @@ -56,12 +56,12 @@ specify do expect { specification1.lock! }.to change { Chewy::Stash::Specification.all.hits }.from([]).to([{ '_index' => 'chewy_specifications', - '_type' => 'specification', + '_type' => '_doc', '_id' => 'places', '_score' => 1.0, '_source' => {'specification' => Base64.encode64({ 'settings' => {'index' => {'number_of_shards' => 1, 'number_of_replicas' => 0}}, - 'mappings' => {'city' => {'properties' => {'founded_on' => {'type' => 'date'}}}} + 'mappings' => {'properties' => {'founded_on' => {'type' => 'date'}}} }.to_json)} }]) end @@ -72,21 +72,21 @@ specify do expect { specification5.lock! }.to change { Chewy::Stash::Specification.all.hits }.to([{ '_index' => 'chewy_specifications', - '_type' => 'specification', + '_type' => '_doc', '_id' => 'places', '_score' => 1.0, '_source' => {'specification' => Base64.encode64({ 'settings' => {'index' => {'number_of_shards' => 1, 'number_of_replicas' => 0}}, - 'mappings' => {'city' => {'properties' => {'founded_on' => {'type' => 'date'}}}} + 'mappings' => {'properties' => {'founded_on' => {'type' => 'date'}}} }.to_json)} }, { '_index' => 'chewy_specifications', - '_type' => 'specification', + '_type' => '_doc', '_id' => 'namespace/cities', '_score' => 1.0, '_source' => {'specification' => Base64.encode64({ 'settings' => {'index' => {'number_of_shards' => 1, 'number_of_replicas' => 0}}, - 'mappings' => {'city' => {'properties' => {'population' => {'type' => 'integer'}}}} + 'mappings' => {'properties' => {'population' => {'type' => 'integer'}}} }.to_json)} }]) end @@ -97,14 +97,14 @@ specify do expect { specification1.lock! }.to change { specification1.locked }.from({}).to( 'settings' => {'index' => {'number_of_shards' => 1, 'number_of_replicas' => 0}}, - 'mappings' => {'city' => {'properties' => {'founded_on' => {'type' => 'date'}}}} + 'mappings' => {'properties' => {'founded_on' => {'type' => 'date'}}} ) end specify do expect { specification5.lock! }.to change { specification5.locked }.from({}).to( 'settings' => {'index' => {'number_of_shards' => 1, 'number_of_replicas' => 0}}, - 'mappings' => {'city' => {'properties' => {'population' => {'type' => 'integer'}}}} + 'mappings' => {'properties' => {'population' => {'type' => 'integer'}}} ) end @@ -114,20 +114,20 @@ specify do expect { specification2.lock! }.to change { specification2.locked }.from( 'settings' => {'index' => {'number_of_shards' => 1, 'number_of_replicas' => 0}}, - 'mappings' => {'city' => {'properties' => {'founded_on' => {'type' => 'date'}}}} + 'mappings' => {'properties' => {'founded_on' => {'type' => 'date'}}} ).to( 'settings' => {'analyzer' => {}, 'index' => {'number_of_shards' => 1, 'number_of_replicas' => 0}}, - 'mappings' => {'city' => {'properties' => {'founded_on' => {'type' => 'date'}}}} + 'mappings' => {'properties' => {'founded_on' => {'type' => 'date'}}} ) end specify do expect { specification3.lock! }.to change { specification3.locked }.from( 'settings' => {'index' => {'number_of_shards' => 1, 'number_of_replicas' => 0}}, - 'mappings' => {'city' => {'properties' => {'founded_on' => {'type' => 'date'}}}} + 'mappings' => {'properties' => {'founded_on' => {'type' => 'date'}}} ).to( 'settings' => {'index' => {'number_of_shards' => 1, 'number_of_replicas' => 0}}, - 'mappings' => {'city' => {'properties' => {'founded_on' => {'type' => 'date'}, 'population' => {'type' => 'integer'}}}} + 'mappings' => {'properties' => {'founded_on' => {'type' => 'date'}, 'population' => {'type' => 'integer'}}} ) end end @@ -136,7 +136,7 @@ describe '#current' do specify do expect(specification2.current).to eq( - 'mappings' => {'city' => {'properties' => {'founded_on' => {'type' => 'date'}}}}, + 'mappings' => {'properties' => {'founded_on' => {'type' => 'date'}}}, 'settings' => {'analyzer' => {}, 'index' => {'number_of_shards' => 1, 'number_of_replicas' => 0}} ) end diff --git a/spec/chewy/index_spec.rb b/spec/chewy/index_spec.rb index b1ddc5e9e..4bcb57b93 100644 --- a/spec/chewy/index_spec.rb +++ b/spec/chewy/index_spec.rb @@ -143,8 +143,6 @@ class DummyCityIndex2 < Chewy::Index describe '.types' do specify { expect(DummiesIndex.types).to eq(DummiesIndex.type_hash.values) } - specify { expect(DummiesIndex.types(:dummy)).to be_a Chewy::Search::Request } - specify { expect(DummiesIndex.types(:user)).to be_a Chewy::Search::Request } end describe '.settings' do @@ -233,7 +231,7 @@ def self.by_id; end define_type :document do field :date, type: 'date' end - end.mappings_hash).to eq(mappings: {document: {properties: {date: {type: 'date'}}}}) + end.mappings_hash).to eq(mappings: {properties: {date: {type: 'date'}}}) end end diff --git a/spec/chewy/journal_spec.rb b/spec/chewy/journal_spec.rb index 491574878..abae9fa6b 100644 --- a/spec/chewy/journal_spec.rb +++ b/spec/chewy/journal_spec.rb @@ -137,7 +137,7 @@ def timestamp(time) expect(journal_entries.size).to eq 4 # simulate lost data - Chewy.client.delete(index: "#{Chewy.settings[:prefix]}_cities", type: 'city', id: 1, refresh: true) + Chewy.client.delete(index: "#{Chewy.settings[:prefix]}_cities", id: 1, refresh: true) expect(cities_index.count).to eq 1 described_class.new.apply(time) @@ -192,8 +192,8 @@ def timestamp(time) Array.new(2) { |i| Country.create!(id: i + 1) } # simulate lost data - Chewy.client.delete(index: 'cities', type: 'city', id: 1, refresh: true) - Chewy.client.delete(index: 'countries', type: 'country', id: 1, refresh: true) + Chewy.client.delete(index: 'cities', id: 1, refresh: true) + Chewy.client.delete(index: 'countries', id: 1, refresh: true) expect(CitiesIndex.all.to_a.length).to eq 1 expect(CountriesIndex.all.to_a.length).to eq 1 @@ -203,7 +203,7 @@ def timestamp(time) expect(CountriesIndex.all.to_a.length).to eq 1 # Replay on both - Chewy.client.delete(index: 'cities', type: 'city', id: 1, refresh: true) + Chewy.client.delete(index: 'cities', id: 1, refresh: true) expect(CitiesIndex.all.to_a.length).to eq 1 expect(described_class.new(CitiesIndex, CountriesIndex).apply(time)).to eq(4) expect(CitiesIndex.all.to_a.length).to eq 2 diff --git a/spec/chewy/runtime_spec.rb b/spec/chewy/runtime_spec.rb index bc21bcacc..edc85231b 100644 --- a/spec/chewy/runtime_spec.rb +++ b/spec/chewy/runtime_spec.rb @@ -3,7 +3,7 @@ describe Chewy::Runtime do describe '.version' do specify { expect(described_class.version).to be_a(described_class::Version) } - specify { expect(described_class.version).to be >= '6.8' } + specify { expect(described_class.version).to be >= '7.0' } specify { expect(described_class.version).to be < '8.0' } end end diff --git a/spec/chewy/search/loader_spec.rb b/spec/chewy/search/loader_spec.rb index c054c79aa..deb618285 100644 --- a/spec/chewy/search/loader_spec.rb +++ b/spec/chewy/search/loader_spec.rb @@ -37,7 +37,6 @@ specify { expect(subject.derive_type('cities', 'city')).to eq(CitiesIndex::City) } specify { expect(subject.derive_type('cities_suffix', 'city')).to eq(CitiesIndex::City) } - specify { expect { subject.derive_type('cities', 'place') }.to raise_error(Chewy::UnderivableType) } specify { expect { subject.derive_type('whatever', 'city') }.to raise_error(Chewy::UnderivableType) } specify { expect { subject.derive_type('citiessuffix', 'city') }.to raise_error(Chewy::UnderivableType) } @@ -54,21 +53,6 @@ specify { expect(subject.load(hits)).to eq([*cities, *countries]) } - context do - let(:options) { {only: 'city'} } - specify { expect(subject.load(hits)).to eq([*cities, nil, nil]) } - end - - context do - let(:options) { {except: 'city'} } - specify { expect(subject.load(hits)).to eq([nil, nil, *countries]) } - end - - context do - let(:options) { {except: %w[city country]} } - specify { expect(subject.load(hits)).to eq([nil, nil, nil, nil]) } - end - context 'scopes', :active_record do context do let(:options) { {scope: -> { where('rating > 2') }} } diff --git a/spec/chewy/search/parameters/indices_spec.rb b/spec/chewy/search/parameters/indices_spec.rb index 97b54cd43..649e7d3a5 100644 --- a/spec/chewy/search/parameters/indices_spec.rb +++ b/spec/chewy/search/parameters/indices_spec.rb @@ -9,30 +9,32 @@ stub_index(:second) do define_type :three end + + stub_index(:third) end - subject { described_class.new(indices: FirstIndex, types: SecondIndex::Three) } + subject { described_class.new(indices: [FirstIndex, SecondIndex]) } describe '#initialize' do - specify { expect(described_class.new.value).to eq(indices: [], types: []) } - specify { expect(described_class.new(nil).value).to eq(indices: [], types: []) } - specify { expect(described_class.new(foo: :whatever).value).to eq(indices: [], types: []) } - specify { expect(subject.value).to eq(indices: [FirstIndex], types: [SecondIndex::Three]) } + specify { expect(described_class.new.value).to eq(indices: []) } + specify { expect(described_class.new(nil).value).to eq(indices: []) } + specify { expect(described_class.new(foo: :whatever).value).to eq(indices: []) } + specify { expect(subject.value).to eq(indices: [FirstIndex, SecondIndex]) } end describe '#replace!' do specify do expect { subject.replace!(nil) } .to change { subject.value } - .from(indices: [FirstIndex], types: [SecondIndex::Three]) - .to(indices: [], types: []) + .from(indices: [FirstIndex, SecondIndex]) + .to(indices: []) end specify do - expect { subject.replace!(indices: SecondIndex, types: FirstIndex::One) } + expect { subject.replace!(indices: SecondIndex) } .to change { subject.value } - .from(indices: [FirstIndex], types: [SecondIndex::Three]) - .to(indices: [SecondIndex], types: [FirstIndex::One]) + .from(indices: [FirstIndex, SecondIndex]) + .to(indices: [SecondIndex]) end end @@ -43,10 +45,10 @@ end specify do - expect { subject.update!(indices: SecondIndex, types: [FirstIndex::One, SecondIndex::Three]) } + expect { subject.update!(indices: ThirdIndex) } .to change { subject.value } - .from(indices: [FirstIndex], types: [SecondIndex::Three]) - .to(indices: [FirstIndex, SecondIndex], types: [SecondIndex::Three, FirstIndex::One]) + .from(indices: [FirstIndex, SecondIndex]) + .to(indices: [FirstIndex, SecondIndex, ThirdIndex]) end end @@ -57,10 +59,8 @@ end specify do - expect { subject.merge!(described_class.new(indices: SecondIndex, types: [FirstIndex::One, SecondIndex::Three])) } - .to change { subject.value } - .from(indices: [FirstIndex], types: [SecondIndex::Three]) - .to(indices: [FirstIndex, SecondIndex], types: [SecondIndex::Three, FirstIndex::One]) + expect { subject.merge!(described_class.new(indices: SecondIndex)) } + .not_to change { subject.value } end end @@ -69,7 +69,7 @@ specify do expect(described_class.new( indices: FirstIndex - ).render).to eq(index: %w[first], type: %w[one]) + ).render).to eq(index: %w[first]) end specify do expect(described_class.new( @@ -78,113 +78,28 @@ end specify do expect(described_class.new( - types: FirstIndex::One - ).render).to eq(index: %w[first], type: %w[one]) - end - specify do - expect(described_class.new( - types: :whatever - ).render).to eq({}) - end - specify do - expect(described_class.new( - indices: FirstIndex, types: SecondIndex::Three - ).render).to eq(index: %w[first second], type: %w[one three]) - end - specify do - expect(described_class.new( - indices: FirstIndex, types: :one - ).render).to eq(index: %w[first], type: %w[one]) - end - specify do - expect(described_class.new( - indices: FirstIndex, types: :whatever - ).render).to eq(index: %w[first], type: %w[one]) - end - specify do - expect(described_class.new( - indices: FirstIndex, types: %i[one whatever] - ).render).to eq(index: %w[first], type: %w[one]) - end - specify do - expect(described_class.new( - indices: :whatever, types: SecondIndex::Three - ).render).to eq(index: %w[second whatever], type: %w[three]) - end - specify do - expect(described_class.new( - indices: :whatever, types: [SecondIndex::Three, :whatever] - ).render).to eq(index: %w[second whatever], type: %w[three whatever]) - end - specify do - expect(described_class.new( - indices: [FirstIndex, :whatever], types: FirstIndex::One - ).render).to eq(index: %w[first whatever], type: %w[one]) - end - specify do - expect(described_class.new( - indices: FirstIndex, types: [FirstIndex::One, :whatever] - ).render).to eq(index: %w[first], type: %w[one]) - end - specify do - expect(described_class.new( - indices: FirstIndex, types: [SecondIndex::Three, :whatever] - ).render).to eq(index: %w[first second], type: %w[one three]) - end - specify do - expect(described_class.new( - indices: [FirstIndex, :whatever], types: [FirstIndex::One, :whatever] - ).render).to eq(index: %w[first whatever], type: %w[one whatever]) - end - specify do - expect(described_class.new( - indices: [FirstIndex, :whatever], types: [SecondIndex::Three, FirstIndex::One] - ).render).to eq(index: %w[first second whatever], type: %w[one three]) + indices: [FirstIndex, :whatever] + ).render).to eq(index: %w[first whatever]) end end describe '#==' do specify { expect(described_class.new).to eq(described_class.new) } specify do - expect(described_class.new(indices: SecondIndex, types: [SecondIndex::Three, :whatever])) - .to eq(described_class.new(indices: SecondIndex, types: :whatever)) - end - specify do - expect(described_class.new(indices: :first, types: %w[one])) + expect(described_class.new(indices: :first)) .to eq(described_class.new(indices: FirstIndex)) end specify do - expect(described_class.new(indices: FirstIndex, types: SecondIndex::Three)) - .not_to eq(described_class.new(indices: FirstIndex)) + expect(described_class.new(indices: FirstIndex)) + .to eq(described_class.new(indices: FirstIndex)) end end describe '#indices' do specify do expect(described_class.new( - indices: [FirstIndex, :whatever], - types: [SecondIndex::Three, :whatever] - ).indices).to contain_exactly(FirstIndex, SecondIndex) - end - end - - describe '#types' do - specify do - expect(described_class.new( - indices: [FirstIndex, :whatever], - types: [SecondIndex::Three, :whatever] - ).types).to contain_exactly( - FirstIndex::One, SecondIndex::Three - ) - end - - specify do - expect(described_class.new( - indices: [FirstIndex, :whatever], - types: [FirstIndex::One, SecondIndex::Three, :whatever] - ).types).to contain_exactly( - FirstIndex::One, SecondIndex::Three - ) + indices: [FirstIndex, :whatever] + ).indices).to contain_exactly(FirstIndex) end end end diff --git a/spec/chewy/search/parameters/types_spec.rb b/spec/chewy/search/parameters/types_spec.rb deleted file mode 100644 index 3bf197169..000000000 --- a/spec/chewy/search/parameters/types_spec.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'chewy/search/parameters/string_array_storage_examples' - -describe Chewy::Search::Parameters::Types do - it_behaves_like :string_array_storage, nil -end diff --git a/spec/chewy/search/request_spec.rb b/spec/chewy/search/request_spec.rb index e75b4b941..d84a6239d 100644 --- a/spec/chewy/search/request_spec.rb +++ b/spec/chewy/search/request_spec.rb @@ -46,7 +46,6 @@ expect(subject.render) .to match( index: %w[products], - type: array_including(%w[product]), body: {} ) end @@ -55,11 +54,11 @@ describe '#inspect' do specify do expect(described_class.new(ProductsIndex).inspect) - .to eq('["products"], :type=>["product"], :body=>{}}>') + .to eq('["products"], :body=>{}}>') end specify do expect(ProductsIndex.limit(10).inspect) - .to eq('["products"], :type=>["product"], :body=>{:size=>10}}>') + .to eq('["products"], :body=>{:size=>10}}>') end end @@ -231,14 +230,6 @@ specify { expect { subject.indices(:cities) }.not_to change { subject.render } } end - describe '#types' do - specify { expect(subject.types(:product).render[:type]).to contain_exactly('product') } - specify { expect(subject.types(%i[product city]).types(nil).render[:type]).to match_array(%w[product]) } - specify { expect(subject.types(:product).types(:product, :city, :something).render[:type]).to match_array(%w[product]) } - specify { expect(subject.types(nil).render[:body]).to be_blank } - specify { expect { subject.types(:product) }.not_to change { subject.render } } - end - describe '#indices_boost' do specify { expect(subject.indices_boost(foo: 1.2).render[:body]).to include(indices_boost: [{'foo' => 1.2}]) } specify { expect(subject.indices_boost(foo: 1.2).indices_boost(moo: 1.3).render[:body]).to include(indices_boost: [{'foo' => 1.2}, {'moo' => 1.3}]) } @@ -420,14 +411,10 @@ outer_payload = payload end subject.query(match: {name: 'name3'}).to_a - request = {index: ['products'], type: %w[product], body: {query: {match: {name: 'name3'}}}} - request[:rest_total_hits_as_int] = true if Chewy::Runtime.version >= '7.0.0' expect(outer_payload).to eq( index: ProductsIndex, indexes: [ProductsIndex], - request: request, - type: ProductsIndex::Product, - types: [ProductsIndex::Product] + request: {index: ['products'], body: {query: {match: {name: 'name3'}}}} ) end end @@ -592,12 +579,12 @@ specify { expect(subject.limit(5).pluck(:_id, :age)).to eq([['1', 10], ['2', 20], ['3', 30], ['4', 40], ['5', 50]]) } specify { expect(subject.limit(5).source(:name).pluck(:id, :age)).to eq([[1, 10], [2, 20], [3, 30], [4, 40], [5, 50]]) } specify do - expect(subject.limit(5).pluck(:_index, :_type, :name)).to eq([ - %w[products product Name1], - %w[products product Name2], - %w[products product Name3], - %w[products product Name4], - %w[products product Name5] + expect(subject.limit(5).pluck(:_index, :name)).to eq([ + %w[products Name1], + %w[products Name2], + %w[products Name3], + %w[products Name4], + %w[products Name5] ]) end @@ -644,9 +631,7 @@ expect(outer_payload).to eq( index: ProductsIndex, indexes: [ProductsIndex], - request: {index: ['products'], type: %w[product], body: {query: {match: {name: 'name3'}}}, refresh: true}, - type: ProductsIndex::Product, - types: [ProductsIndex::Product] + request: {index: ['products'], body: {query: {match: {name: 'name3'}}}, refresh: true} ) end @@ -659,9 +644,7 @@ expect(outer_payload).to eq( index: ProductsIndex, indexes: [ProductsIndex], - request: {index: ['products'], type: %w[product], body: {query: {match: {name: 'name3'}}}, refresh: false}, - type: ProductsIndex::Product, - types: [ProductsIndex::Product] + request: {index: ['products'], body: {query: {match: {name: 'name3'}}}, refresh: false} ) end end diff --git a/spec/chewy/search/scrolling_spec.rb b/spec/chewy/search/scrolling_spec.rb index 8e21f4e6c..aa0b64174 100644 --- a/spec/chewy/search/scrolling_spec.rb +++ b/spec/chewy/search/scrolling_spec.rb @@ -119,22 +119,16 @@ outer_payload << payload end request.scroll_batches(batch_size: 3).to_a - request = {index: %w[cities countries], type: %w[city country], body: {sort: ['rating']}, size: 3, scroll: '1m'} - request[:rest_total_hits_as_int] = true if Chewy::Runtime.version >= '7.0.0' expect(outer_payload).to match_array([ hash_including( index: [CitiesIndex, CountriesIndex], indexes: [CitiesIndex, CountriesIndex], - request: request, - type: [CitiesIndex::City, CountriesIndex::Country], - types: [CitiesIndex::City, CountriesIndex::Country] + request: {index: %w[cities countries], body: {sort: ['rating']}, size: 3, scroll: '1m'} ), hash_including( index: [CitiesIndex, CountriesIndex], indexes: [CitiesIndex, CountriesIndex], - request: {scroll: '1m', scroll_id: an_instance_of(String)}, - type: [CitiesIndex::City, CountriesIndex::Country], - types: [CitiesIndex::City, CountriesIndex::Country] + request: {scroll: '1m', scroll_id: an_instance_of(String)} ) ]) end diff --git a/spec/chewy/search_spec.rb b/spec/chewy/search_spec.rb index 0b7468230..f8e52faf6 100644 --- a/spec/chewy/search_spec.rb +++ b/spec/chewy/search_spec.rb @@ -37,12 +37,12 @@ end specify do - expect(ProductsIndex.client).to receive(:search).with(hash_including(index: ['products'], type: %w[product])) + expect(ProductsIndex.client).to receive(:search).with(hash_including(index: ['products'])) ProductsIndex.search_string('hello') end specify do - expect(ProductsIndex.client).to receive(:search).with(hash_including(index: ['products'], type: %w[product])) + expect(ProductsIndex.client).to receive(:search).with(hash_including(index: ['products'])) product.search_string('hello') end end diff --git a/spec/chewy/type/import/bulk_request_spec.rb b/spec/chewy/type/import/bulk_request_spec.rb index febfb7839..3b62fd8b8 100644 --- a/spec/chewy/type/import/bulk_request_spec.rb +++ b/spec/chewy/type/import/bulk_request_spec.rb @@ -32,7 +32,6 @@ specify do expect(Chewy.client).to receive(:bulk).with( index: 'places', - type: 'city', body: [{index: {id: 42, data: {name: 'Name'}}}] ) subject.perform([{index: {id: 42, data: {name: 'Name'}}}]) @@ -44,7 +43,6 @@ specify do expect(Chewy.client).to receive(:bulk).with( index: 'places_suffix', - type: 'city', body: [{index: {id: 42, data: {name: 'Name'}}}] ) subject.perform([{index: {id: 42, data: {name: 'Name'}}}]) @@ -57,7 +55,6 @@ specify do expect(Chewy.client).to receive(:bulk).with( index: 'places', - type: 'city', body: "{\"index\":{\"id\":42}}\n{\"name\":\"#{'Name' * 10}\"}\n{\"index\":{\"id\":43}}\n{\"name\":\"#{'Shame' * 10}\"}\n" ) subject.perform([ @@ -69,17 +66,14 @@ specify do expect(Chewy.client).to receive(:bulk).with( index: 'places', - type: 'city', body: "{\"index\":{\"id\":42}}\n{\"name\":\"#{'Name' * 30}\"}\n" ) expect(Chewy.client).to receive(:bulk).with( index: 'places', - type: 'city', body: "{\"index\":{\"id\":43}}\n{\"name\":\"#{'Shame' * 100}\"}\n" ) expect(Chewy.client).to receive(:bulk).with( index: 'places', - type: 'city', body: "{\"index\":{\"id\":44}}\n{\"name\":\"#{'Blame' * 30}\"}\n" ) subject.perform([ diff --git a/spec/chewy/type/import/journal_builder_spec.rb b/spec/chewy/type/import/journal_builder_spec.rb index dcc7ea54f..25b36f48c 100644 --- a/spec/chewy/type/import/journal_builder_spec.rb +++ b/spec/chewy/type/import/journal_builder_spec.rb @@ -29,7 +29,6 @@ expect(subject.bulk_body).to eq([{ index: { _index: 'chewy_journal', - _type: 'journal', data: { 'index_name' => 'namespace/cities', 'type_name' => 'city', @@ -48,7 +47,6 @@ expect(subject.bulk_body).to eq([{ index: { _index: 'chewy_journal', - _type: 'journal', data: { 'index_name' => 'namespace/cities', 'type_name' => 'city', @@ -69,7 +67,6 @@ expect(subject.bulk_body).to eq([{ index: { _index: 'chewy_journal', - _type: 'journal', data: { 'index_name' => 'namespace/countries', 'type_name' => 'country', @@ -81,7 +78,6 @@ }, { index: { _index: 'chewy_journal', - _type: 'journal', data: { 'index_name' => 'namespace/countries', 'type_name' => 'country', diff --git a/spec/chewy/type/import_spec.rb b/spec/chewy/type/import_spec.rb index 833e671a8..ad3f77f47 100644 --- a/spec/chewy/type/import_spec.rb +++ b/spec/chewy/type/import_spec.rb @@ -366,8 +366,8 @@ def subscribe_notification # Full match doesn't work here. expect(payload[:errors][:update].keys).to match([ - hash_including('type' => 'document_missing_exception', 'reason' => '[city][1]: document missing'), - hash_including('type' => 'document_missing_exception', 'reason' => '[city][3]: document missing') + hash_including('type' => 'document_missing_exception', 'reason' => '[_doc][1]: document missing'), + hash_including('type' => 'document_missing_exception', 'reason' => '[_doc][3]: document missing') ]) expect(payload[:errors][:update].values).to eq([['1'], ['3']]) expect(imported_cities).to match_array([ diff --git a/spec/chewy/type/mapping_spec.rb b/spec/chewy/type/mapping_spec.rb index f1cdb7d5b..a6ed5a633 100644 --- a/spec/chewy/type/mapping_spec.rb +++ b/spec/chewy/type/mapping_spec.rb @@ -59,7 +59,7 @@ Chewy.default_root_options = previous_options end - specify { expect(product.mappings_hash[:product]).to include(_all: {enabled: false}) } + specify { expect(product.mappings_hash).to include(_all: {enabled: false}) } end end @@ -90,12 +90,11 @@ before do stub_index(:products) do define_type :product do - root _parent: 'project', other_option: 'nothing' do + root other_option: 'nothing' do field :name do field :last_name # will be redefined in the following root flock end end - root _parent: 'something_else' root other_option: 'option_value' do field :identifier field :name, type: 'integer' @@ -105,47 +104,16 @@ end specify do - expect(product.mappings_hash).to eq(product: { + expect(product.mappings_hash).to eq( properties: { name: {type: 'integer'}, identifier: {type: Chewy.default_field_type} }, - other_option: 'option_value', - _parent: {type: 'something_else'} - }) + other_option: 'option_value' + ) end end end - - context 'parent-child relationship' do - context do - before do - stub_index(:products) do - define_type :product do - root _parent: 'project', parent_id: -> { project_id } do - field :name, 'surname' - end - end - end - end - - specify { expect(product.mappings_hash[:product][:_parent]).to eq(type: 'project') } - end - - context do - before do - stub_index(:products) do - define_type :product do - root parent: {'type' => 'project'}, parent_id: -> { project_id } do - field :name, 'surname' - end - end - end - end - - specify { expect(product.mappings_hash[:product][:_parent]).to eq('type' => 'project') } - end - end end describe '.supports_outdated_sync?' do