algoliasearch-rails icon indicating copy to clipboard operation
algoliasearch-rails copied to clipboard

allow add_index, inherit: true

Open paneq opened this issue 7 years ago • 7 comments

add_replica already supports inherit: true do. It would be nice if add_index also supported it.

How it would work

algoliasearch if: :published do
    attribute :title, :description

    attribute :starts_at do
      starts_at.to_i
    end

    searchableAttributes %w[title unordered(description)]
    attributesToSnippet %w[description]
    attributesForFaceting %w[filterOnly(starts_at)]

    add_replica STARTS_AT_ASC_INDEX, inherit: true do
      customRanking ['asc(starts_at)']
    end

    add_index ADMIN_INDEX_NAME, inherit: true do
      attributesForFaceting %w[state]
    end
  end

The idea is that ADMIN_INDEX_NAME would not require repeated definition of:

    attribute :title, :description

    attribute :starts_at do
      starts_at.to_i
    end

    searchableAttributes %w[title unordered(description)]
    attributesToSnippet %w[description]

and it would not get the STARTS_AT_ASC_INDEX replica.

I tried to replicate this behavior with:

add_index ADMIN_INDEX_NAME, inherit: true, primary_settings: self

similarly to how add_replica works under the hood but there were certain problems with this approach. Namely starts_at was not using the Proc with to_i conversion.

Also there is an API documentation of Ruby client https://www.algolia.com/doc/api-client/ruby/getting-started/ and there is tutorial for Rails https://www.algolia.com/doc/api-client/rails/indices/#target-multiple-indices but I could not find a full API documentation for algoliasearch-rails to know which exact options are supported on which methods.

Or maybe you can recommend other ways to handle this scenario? Defining a method on the model and using it as attribute name, maybe?

paneq avatar Feb 22 '18 16:02 paneq

According to the documentation, this should work but unfortunately, it doesn't. I added inherit: true but it doesn't use the attributes I defined before. In addition I get the error NoMethodError: undefined method 'to_settings' for nil:NilClass which happens here https://github.com/algolia/algoliasearch-rails/blob/master/lib/algoliasearch-rails.rb#L755

joker-777 avatar May 11 '18 02:05 joker-777

Ok, adding :primary_settings => self to the options removed the error but when reading the code I realized that it really only inherits OPTIONS but not attributes.

joker-777 avatar May 11 '18 02:05 joker-777

+1 for this - did anyone find an alternate solution to avoid repetition?

MatthewRuddy avatar Jun 18 '18 19:06 MatthewRuddy

It would be very useful, and would help refactor this https://github.com/algolia/algoliasearch-rails/issues/307#issuecomment-463529441

Spone avatar Feb 14 '19 08:02 Spone

I'm working on it 👍 see PR #335

julienbourdeau avatar Feb 14 '19 15:02 julienbourdeau

I think #335 is getting close. I introduce 2 new options: inherit_settings: true and inherit_attributes: true. Both need inherit: true to work. I know it's a lot of option but it's the best way I found to keep backward compatibility.

I'm currently writing tests but if anyone can try this PR this will be very useful 🙏 Everything I tested by hand worked as expected but I could have forgotten something.

Original example

With this PR, the original snippet would be

algoliasearch if: :published do
    attribute :title, :description

    attribute :starts_at do
      starts_at.to_i
    end

    searchableAttributes %w[title unordered(description)]
    attributesToSnippet %w[description]
    attributesForFaceting %w[filterOnly(starts_at)]

    add_replica STARTS_AT_ASC_INDEX, inherit: true, inherit_settings: true, inherit_attributes: true do
      customRanking ['asc(starts_at)']
    end

    add_index ADMIN_INDEX_NAME, inherit: true, inherit_settings: true, inherit_attributes: true do
      attributesForFaceting %w[state]
    end
  end

julienbourdeau avatar Mar 25 '19 15:03 julienbourdeau

The PR looks great @julienbourdeau! It's letting me refrain from duplicating thousands of lines of attribution declarations. Thank you!

This should be merged into the gem. I tried to ping a few current Algolia devs on the linked thread. If there's someone else at Algolia who would be better to ping, I'd be grateful if you could say the word...

duhaime avatar Feb 19 '22 12:02 duhaime