ankane/searchkick

Feature Request: Implement none Method

satyakampandya opened this issue · 3 comments

Is your feature request related to a problem? Please describe.

At present, Searchkick lacks the none method which is a powerful tool available in ActiveRecord. The absence of this feature poses certain limitations, particularly when developers need to create queries that are empty by default and conditionally build upon them.

Describe the solution you'd like

Implementation of the none method in the Searchkick gem. This feature is already available in ActiveRecord, and it would be incredibly valuable to have a similar capability in Searchkick when working with Elasticsearch.

Benefits

  • Improved query building: The none method simplifies the process of conditionally building queries.
  • Consistency with ActiveRecord: Given that ActiveRecord offers this feature, implementing it in Searchkick would make it more consistent and familiar for Rails developers who are already using ActiveRecord.

I'm willing to submit PR, @ankane I'll need your guidance to proceed in right direction.

ankane commented

Hi @satyakampandya, thanks for the feature request. However, I don't understand the problem you're try to solve. Can you share more about the specific problem, and possibly a code example of what you're envisioning?

Scenario:

In my codebase, I am handling Elasticsearch connectivity issues by rescuing Faraday::TimeoutError. However, this approach causes issues with my existing code that relies on @products for fetching suggestions.

def retrieve_products
  @products = base_elasticsearch
rescue Faraday::TimeoutError
  @products = []
end

Proposed Solution:

To address this issue, I propose the creation of a new method called none that sets @products, allowing to gracefully handle Elasticsearch downtime without breaking your existing codebase.

def retrieve_products
  @products = base_elasticsearch
rescue Faraday::TimeoutError
  @products = Searchkick::Results.none
  # Alternatively, you can use a custom `none` method:
  # @products = Product.search(none: true)
end
searcher = build_searcher(search_params)
products = searcher.retrieve_products
products_suggestions = products.suggestions             
# products.suggestions will work, it'll return [] if Elasticsearch is down
required_hits = (10 - products_suggestions.count)      
# products_suggestions.count will work, it'll return 0 if Elasticsearch is down

This approach ensures that your existing code can continue to use @products without disruption, and it provides a more structured way to handle Elasticsearch downtime using the none method.

In summary, this proposal introduces a new method to gracefully handle exceptions while maintaining the stability of your codebase in situations where Elasticsearch is unavailable.

ankane commented

Thanks for the additional explanation. However, I don't see a strong reason for including it in the gem (users can do nil checks or implement their own null objects as needed).