meilisearch / meilisearch-rails

Meilisearch integration for Ruby on Rails
https://www.meilisearch.com
MIT License
308 stars 47 forks source link

override index_uid in per_environment setup #229

Closed 0xMostafa closed 1 year ago

0xMostafa commented 1 year ago

Hi,

I've successfully setup meilisearch to work with my Rails backend.

I set per_environment: true in the configuration as i want an index per model per env, except for two models, I want them for production only.

For those 2 models, i set index name manually like:

class User < ApplicationRecord
   meilisearch index_uid: "User" do
   end
end

Problem is, when i use User.ms_index it queries User_prod/staging/etc

how can i override User.ms_index to set the index name manually to just User?

brunoocasali commented 1 year ago

Hi @0xMostafa, thanks for using Meilisearch!!

So, this configuration only works globally. At that moment, we didn't know any use case that made us implement it differently, and maybe your case could be the one...

Can you explain it to me? Why do you need this special handling for only a few indexes?

CC: @oluademola just to be in the loop :)

0xMostafa commented 1 year ago

Sure,

I consume meilisearch in two ways:

1- from the frontend using algolia's instantsearch.js 2- from the backend to read from & search some indexes that are not front-facing

so, for 2, i need per_environment: true in order to test on multiple envs

and for 1, i still need multi env for testing, but I gave it up as it changes index name in url, for example, on production the url is roughly: company.xyz/pagename/index_production?page[size]=1&page[number]=1

hope it helps, and thank you for meilisearch,

brunoocasali commented 1 year ago

Sorry for the delay here @0xMostafa,

I've read your message in the past, but I'm still unconvinced about the issue or got the problem correctly.

Of course, I don't know 100% how your environment is made, but I can't see why you want to call in the front end a different index name with the same data as the one on the back end.

For me, if you have books_production, you should call it from the production environment no matter where you call it.

It was hard for me to get the whole point, but I think the issue relies on the problem of setting the environment on the instantsearch side, right? (like this)

const search = instantsearch({
  indexName: 'books_production',
  searchClient: instantMeiliSearch(
    'https://ms-adf78ae33284-106.lon.meilisearch.io',
    'a63da4928426f12639e19d62886f621130f3fa9ff3c7534c5d179f0f51c4f303'
  ),
})

If that's the thing, you may find a way of introducing an environment variable that is applied during build time to replace the _production of your indexName.

I'm sorry again for the delay and for not understanding your issue completely. Let me know if I can help you...

brunoocasali commented 1 year ago

I'm closing this since this refactoring would require too many human resources, which we currently need. Let me know if someone in the future would like to do it, I can consider accepting the PRs :)