Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amol/flatten nested filters #956

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
f940af1
Change client to conditionally use different hosts
maxwxzheng Nov 6, 2017
852faa9
Update all places necessary to use conditional host names.
maxwxzheng Nov 7, 2017
1072cc4
Fix a bug where we are not passing hosts name to the version call
maxwxzheng Nov 7, 2017
663b7ab
Merge remote-tracking branch 'upstream/master'
maxwxzheng Dec 24, 2019
e5c526a
Fix request.rb _indices
maxwxzheng Dec 25, 2019
8156b64
Add host_name to some query client calls
maxwxzheng Dec 25, 2019
cc251e7
Changes to support es 7
maxwxzheng Dec 31, 2019
8af16f5
Remove changes on delete_all
maxwxzheng Jan 4, 2020
d1523ab
Merge post_filter into query if counting total
maxwxzheng Mar 18, 2020
15de427
print
3mammoth Mar 23, 2020
d8f4818
fix
3mammoth Mar 23, 2020
bc68de0
add logging
3mammoth Mar 23, 2020
eeed671
add more logging
3mammoth Mar 23, 2020
ef9f577
mabe fix bug
3mammoth Mar 23, 2020
dd5e2c1
delete putss
3mammoth Mar 23, 2020
e1fc580
track_total_hits
3mammoth Apr 13, 2020
258c5f1
track_total_hits
3mammoth Apr 13, 2020
3cdc21e
Support x-opaque-id
maxwxzheng May 3, 2020
d817ed1
Prevent double query
mightymercado May 14, 2021
5d7d0e3
Skip those with indexable criteria for now
mightymercado May 14, 2021
a708693
allow passing of searchable proc
mightymercado May 14, 2021
3f8ff3c
Merge pull request #2 from apolloio/ken/prevent-query
mightymercado May 14, 2021
56c954f
fix searchable proc passing
mightymercado May 14, 2021
e19cfe4
Merge pull request #3 from apolloio/ken/prevent-query
mightymercado May 14, 2021
a26eeeb
Delete non indexable documents
mightymercado May 18, 2021
7c360f4
Merge pull request #5 from apolloio/ken/delete-non-indexable-people
mightymercado May 19, 2021
dce35eb
Fix ruby 2.7 deprecation warnings
jaywonder20 Jul 28, 2021
f60be63
more fixes
jaywonder20 Jul 28, 2021
7e1fe93
fix
jaywonder20 Aug 7, 2021
cb10905
Merge pull request #6 from apolloio/jerry/upgrade
3mammoth Aug 19, 2022
32fbd91
reproducing fix
prasad-apollo Jan 11, 2024
3d6be5d
supporting new signature for reduce
prasad-apollo Mar 25, 2024
209de33
Merge pull request #10 from apolloio/prasad/i18n-core-ext
prasad-apollo Apr 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/.rakeTasks

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions .idea/chewy.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 20 additions & 4 deletions lib/chewy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
require 'active_support/core_ext/object/inclusion'
require 'active_support/core_ext/string/inflections'

require 'i18n/core_ext/hash'
require 'active_support/core_ext/hash'
require 'chewy/backports/deep_dup' unless Object.respond_to?(:deep_dup)
require 'singleton'
require 'base64'
Expand Down Expand Up @@ -132,21 +132,35 @@ def derive_types(from)
def create_type(index, target, options = {}, &block)
type = Class.new(Chewy::Type)

adapter = adapters.find { |klass| klass.accepts?(target) }.new(target, options)
adapter = adapters.find { |klass| klass.accepts?(target) }.new(target, **options)

index.const_set(adapter.name, type)
type.send(:define_singleton_method, :index) { index }
type.send(:define_singleton_method, :adapter) { adapter }

type.class_eval(&block) if block

type.class_eval do
field :_index_type, value: -> () {adapter.name.underscore}, type: 'keyword'
end

type
end

# Main elasticsearch-ruby client instance
#
def client
Thread.current[:chewy_client] ||= begin
def client(hosts=nil)
# We are changing this to support multiple clusters in chewy.
if hosts
thread_cache_key = "chewy_client_#{hosts}"
else
thread_cache_key = "chewy_client"
end
Thread.current[thread_cache_key.to_sym] ||= begin
client_configuration = configuration.deep_dup
if hosts
client_configuration[:hosts] = client_configuration[hosts]
end
client_configuration.delete(:prefix) # used by Chewy, not relevant to Elasticsearch::Client
block = client_configuration[:transport_options].try(:delete, :proc)
::Elasticsearch::Client.new(client_configuration, &block)
Expand All @@ -159,13 +173,15 @@ def client
# Does nothing in case of config `wait_for_status` is undefined.
#
def wait_for_status
# TODO(Max): Add hosts to this client call
client.cluster.health wait_for_status: Chewy.configuration[:wait_for_status] if Chewy.configuration[:wait_for_status].present?
end

# Deletes all corresponding indexes with current prefix from ElasticSearch.
# Be careful, if current prefix is blank, this will destroy all the indexes.
#
def massacre
# TODO(Max): Add hosts to this client call
Chewy.client.indices.delete(index: [Chewy.configuration[:prefix], '*'].reject(&:blank?).join('_'))
Chewy.wait_for_status
end
Expand Down
2 changes: 1 addition & 1 deletion lib/chewy/fields/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Base
def initialize(name, value: nil, **options)
@name = name.to_sym
@options = {}
update_options!(options)
update_options!(**options)
@value = value
@children = []
end
Expand Down
4 changes: 2 additions & 2 deletions lib/chewy/fields/root.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class Root < Chewy::Fields::Base
attr_reader :parent
attr_reader :parent_id

def initialize(*)
super
def initialize(name, **options)
super(name, **options)

@value ||= -> { self }
@dynamic_templates = []
Expand Down
29 changes: 28 additions & 1 deletion lib/chewy/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class Index
self._settings = Chewy::Index::Settings.new

class << self
attr_reader :hosts_name

# @overload index_name(suggest)
# If suggested name is passed, it is set up as the new base name for
# the index. Used for the index base name redefinition.
Expand Down Expand Up @@ -62,6 +64,13 @@ def index_name(suggest = nil, prefix: nil, suffix: nil)
end
end

# Sets the hosts name of the index. If hosts_name is nil, use the default
# hosts in chewy.yml. Otherwise use the hosts with the specified name for
# indexing/queries.
def set_hosts_name(hosts_name)
@hosts_name = hosts_name
end

# Base name for the index. Uses the default value inferred from the
# class name unless redefined.
#
Expand Down Expand Up @@ -219,7 +228,25 @@ def settings_hash
end

def mappings_hash
mappings = types.map(&:mappings_hash).inject(:merge)
# Removes the type from type_mappings and merge fields from different types
type_mappings = types.map(&:mappings_hash)
mappings = {properties: {}}
type_mappings.each do |type_mapping|
if type_mapping.count != 1
raise "Type mapping must have 1 entry: #{type_mapping}"
end

fields = type_mapping.reduce(:merge)[1][:properties]
fields.each do |field_name, field_settings|
existing_field_settings = mappings[:properties][field_name]
if existing_field_settings && existing_field_settings != field_settings
raise "Same field name has different settings: #{field_name}. Setting 1: #{existing_field_settings}. Setting 2: #{field_settings}"
end

mappings[:properties][field_name] = field_settings
end
end

mappings.present? ? {mappings: mappings} : {}
end

Expand Down
12 changes: 6 additions & 6 deletions lib/chewy/index/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module ClassMethods
# UsersIndex.exists? #=> true
#
def exists?
client.indices.exists(index: index_name)
client(@hosts_name).indices.exists(index: index_name)
end

# Creates index and applies mappings and settings.
Expand Down Expand Up @@ -59,7 +59,7 @@ def create!(suffix = nil, **options)

body = specification_hash
body[:aliases] = {general_name => {}} if options[:alias] && suffixed_name != general_name
result = client.indices.create(index: suffixed_name, body: body)
result = client(@hosts_name).indices.create(index: suffixed_name, body: body)

Chewy.wait_for_status if result
result
Expand All @@ -74,7 +74,7 @@ def create!(suffix = nil, **options)
# UsersIndex.delete '01-2014' # deletes `users_01-2014` index
#
def delete(suffix = nil)
result = client.indices.delete index: index_name(suffix: suffix)
result = client(@hosts_name).indices.delete index: index_name(suffix: suffix)
Chewy.wait_for_status if result
result
# es-ruby >= 1.0.10 handles Elasticsearch::Transport::Transport::Errors::NotFound
Expand Down Expand Up @@ -183,13 +183,13 @@ def reset!(suffix = nil, apply_journal: true, journal: false, **import_options)
original_index_settings suffixed_name

delete if indexes.blank?
client.indices.update_aliases body: {actions: [
client(@hosts_name).indices.update_aliases body: {actions: [
*indexes.map do |index|
{remove: {index: index, alias: general_name}}
end,
{add: {index: suffixed_name, alias: general_name}}
]}
client.indices.delete index: indexes if indexes.present?
client(@hosts_name).indices.delete index: indexes if indexes.present?

self.journal.apply(start_time, **import_options) if apply_journal
result
Expand Down Expand Up @@ -229,7 +229,7 @@ def original_index_settings(index_name)
end

def update_settings(index_name, **options)
client.indices.put_settings index: index_name, body: {index: options[:settings]}
client(@hosts_name).indices.put_settings index: index_name, body: {index: options[:settings]}
end

def index_settings(setting_name)
Expand Down
4 changes: 2 additions & 2 deletions lib/chewy/index/aliases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ module Aliases

module ClassMethods
def indexes
client.indices.get_alias(name: index_name).keys
client(@hosts_name).indices.get_alias(name: index_name).keys
rescue Elasticsearch::Transport::Transport::Errors::NotFound
[]
end

def aliases
name = index_name
client.indices.get_alias(index: name, name: '*')[name].try(:[], 'aliases').try(:keys) || []
client(@hosts_name).indices.get_alias(index: name, name: '*')[name].try(:[], 'aliases').try(:keys) || []
rescue Elasticsearch::Transport::Transport::Errors::NotFound
[]
end
Expand Down
8 changes: 4 additions & 4 deletions lib/chewy/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ def merge(other)
#
def delete_all
if Runtime.version >= '2.0'
plugins = Chewy.client.nodes.info(plugins: true)['nodes'].values.map { |item| item['plugins'] }.flatten
plugins = Chewy.client(_indexes.first.hosts_name).nodes.info(plugins: true)['nodes'].values.map { |item| item['plugins'] }.flatten
raise PluginMissing, 'install delete-by-query plugin' unless plugins.find { |item| item['name'] == 'delete-by-query' }
end

Expand All @@ -995,9 +995,9 @@ def delete_all
Elasticsearch::API::Utils.__listify(request[:type]),
'/_query'
)
Chewy.client.perform_request(Elasticsearch::API::HTTP_DELETE, path, {}, request[:body]).body
Chewy.client(_indexes.first.hosts_name).perform_request(Elasticsearch::API::HTTP_DELETE, path, {}, request[:body]).body
else
Chewy.client.delete_by_query(request)
Chewy.client(_indexes.first.hosts_name).delete_by_query(request)
end
end
end
Expand Down Expand Up @@ -1101,7 +1101,7 @@ def _response
index: _indexes.one? ? _indexes.first : _indexes,
type: _types.one? ? _types.first : _types do
begin
Chewy.client.search(_request)
Chewy.client(_indexes.first.hosts_name).search(_request)
rescue Elasticsearch::Transport::Transport::Errors::NotFound => e
raise e if e.message !~ /IndexMissingException/ && e.message !~ /index_not_found_exception/
{}
Expand Down
2 changes: 1 addition & 1 deletion lib/chewy/query/loading.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def _load_objects!
loaded_objects = Hash[_results.group_by(&:class).map do |type, objects|
next if except.include?(type.type_name)
next if only.present? && !only.include?(type.type_name)
loaded = type.adapter.load(objects.map(&:id), options.merge(_type: type)) || objects
loaded = type.adapter.load(objects.map(&:id), **options.merge(_type: type)) || objects
[type, loaded.index_by.with_index do |loaded_object, i|
objects[i]._object = loaded_object
objects[i]
Expand Down
2 changes: 1 addition & 1 deletion lib/chewy/query/pagination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Pagination
# PlacesIndex.query(...).filter(...).total
#
def total
_response['hits'].try(:[], 'total') || 0
_response['hits']['total'] ? _response['hits']['total']['value'] || 0 : 0
end
alias_method :total_count, :total
alias_method :total_entries, :total
Expand Down
2 changes: 1 addition & 1 deletion lib/chewy/rspec/update_index.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'i18n/core_ext/hash'
require 'active_support/core_ext/hash'

# Rspec matcher `update_index`
# To use it - add `require 'chewy/rspec'` to the `spec_helper.rb`
Expand Down
4 changes: 2 additions & 2 deletions lib/chewy/runtime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

module Chewy
module Runtime
def self.version
Thread.current[:chewy_runtime_version] ||= Version.new(Chewy.client.info['version']['number'])
def self.version(hosts=nil)
Thread.current[:chewy_runtime_version] ||= Version.new(Chewy.client(hosts).info['version']['number'])
end
end
end
2 changes: 1 addition & 1 deletion lib/chewy/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def all
# @return [Hash] the request result
def search_string(query, options = {})
options = options.merge(all.render.slice(:index, :type).merge(q: query))
Chewy.client.search(options)
Chewy.client(@hosts_name).search(options)
end

# Delegates methods from the request class to the index or type class
Expand Down
5 changes: 2 additions & 3 deletions lib/chewy/search/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,14 @@ def derive_type(index, type)
# @param hits [Array<Hash>] ES hits array
# @return [Array<Object, nil>] the array of corresponding ORM/ODM objects
def load(hits)
hit_groups = hits.group_by { |hit| [hit['_index'], hit['_type']] }
hit_groups = hits.group_by { |hit| [hit['_index'], hit['_source']['_index_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))
loaded = type.adapter.load(ids, **@options.merge(_type: type))
loaded ||= hit_group.map { |hit| type.build(hit) }

result.merge!(hit_group.zip(loaded).to_h)
end

Expand Down
Loading