meilisearch / meilisearch-rails

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

Perform multiple searches in one single HTTP request #254

Closed brunoocasali closed 8 months ago

brunoocasali commented 1 year ago

⚠️ This issue is generated, it means the examples and the namings do not necessarily correspond to the language of this repository. Also, if you are a maintainer, feel free to add any clarification and instruction about this issue.

Sorry if this is already partially/completely implemented, feel free to let me know about the state of this issue in the repo.

Related to https://github.com/meilisearch/integration-guides/issues/251


Related to:

Create a new client method called multiSearch/multi_search, which will request POST /multi-search with a body payload containing a structure similar to this:

{
   "queries": [ 
      { "indexUid": "movie", "q": "wonder", "limit": 10 }, 
      { "indexUid": "books", "q": "king", "page": 2 } 
    ]
}

Each object is a simple search object sent in the POST /indexes/:indexUid/search request.

Pay attention to the response, which will follow the order of the requests object and will look like this (note the new indexUid key in the response):

{
   "results": [ 
      {
         "indexUid": "movie",
         "hits": [ { "title": "wonderwoman" } ],
         // other search results fields: processingTimeMs, limit, ...
      },
      {
         "indexUid": "books",
         "hits": [ { "title": "king kong theory" } ],
         // other search results fields: processingTimeMs, limit, ...
      },
   ]
}

TODO:

ellnix commented 1 year ago

Should this not first be added to meilisearch-ruby? I checked, but there was no issue for it, past or present.

brunoocasali commented 1 year ago

The multi_search works on ruby already :)

https://github.com/meilisearch/meilisearch-ruby/issues/489#issuecomment-1750645773

jonathanfikunayomi commented 9 months ago

No problem

doutatsu commented 9 months ago

Any updates on this? I am now adding multi-search to my app, so this would be really helpful with integration

ashwin47 commented 9 months ago

Same here, Would really like to add this to my app.

curquiza commented 9 months ago

Meili team will not have the time to work on it, sorry. We are a really small team so we have to prioritize our tasks. However any contributions are welcome and we will be ready to review them 😊

botbotbotbotboot commented 9 months ago

I tried to do this but didnt reach far.

@curquiza Would you be open to bounties?.

Created bounty on algora but i think you will have to add app and accept or something : https://console.algora.io/bounties/clsd5ih5e0006jy0fja0imiu0

ellnix commented 9 months ago

I can probably do this, but it will take some reengineering with the current code base, and we should probably discuss API since there is nothing like it currently in the gem.

ellnix commented 9 months ago

The primary challenge is matching index names to models, which we reasonably cannot do and therefore will be passed on as a responsibility to the user.

Currently I can think of:

  1. Lazy-evaluating search with an in-between array-like class so we can add method chaining like ActiveRecord Relation. Think Product.search('thing').and(Book.search('paper')).
    • Difficult to use if the searches are not predetermined
    • Complex to implement, certainly qualifies as a breaking change
  2. A separate multi-search method

    1. Model-centric

      MeiliSearch::Rails.multi_search(
      Book => {q: 'paper', **book_options}, # Model with implied index
      Product => {q: 'thing', **product_options}, 
      **other_searches)
    2. Index-centric

      MeiliSearch::Rails.multi_search(
      'book_production' => {q: 'paper', class_name: 'Book', **book_options}, # Index with a model
      'stuff_production' => {q: 'thing', class_name: 'Product', **product_options},
      'blurbs' => { q: 'happy' }, # Index not backed by a model, results will be simple hashes
      **other_searches)
    3. Polymorphic (both model-centric and index-centric)

    MeiliSearch::Rails.multi_search(
      'book_production' => {q: 'paper', class_name: 'Book', **book_options}, # Index with a model
      Product => {q: 'thing', **product_options}, # Model with implied index
      'blurbs' => { q: 'happy' }, # Index not backed by a model, results will be simple hashes
      **other_searches)

Personally I prefer 2. iii. the polymorphic dedicated method, despite how messy it looks in the example above, it would allow for the most options and flexibility. I am open to other suggestions and will also add any others I can think of.

curquiza commented 9 months ago

@curquiza Would you be open to bounties?.

No, we don't have a bounty for opensource contributions

curquiza commented 9 months ago

Thank you @ellnix let know now if you need review on anything. I talked with Bruno and we will try to be reactive on this

zcesur commented 9 months ago

Created bounty on algora but i think you will have to add app and accept or something : https://console.algora.io/bounties/clsd5ih5e0006jy0fja0imiu0

hey algora cofounder here :wave: meilisearch team can accept the bounty here if they'd like! https://algora.io/invitation/meilisearch

botbotbotbotboot commented 8 months ago

Just checking to make sure,

If I have two models, Question and Reply, then to search both of them at once, do I need this feature? Is there any other way of doing it that I don't know about?

I'm curious because it seems like a common use case for a search product.

ellnix commented 8 months ago

If I have two models, Question and Reply, then to search both of them at once, do I need this feature? Is there any other way of doing it that I don't know about?

Well not necessarily:

botbotbotbotboot commented 8 months ago

Making "Questions and Replies share an index and search that index" could works for me.

Let me try, Thanks.

botbotbotbotboot commented 8 months ago

Thank you for merging this, can you share some pointers on using it?

MeiliSearch::Rails.multi_search is giving undefined method.

ellnix commented 8 months ago

Thank you for merging this, can you share some pointers on using it?

MeiliSearch::Rails.multi_search is giving undefined method.

It has just been implemented, it will be made available in the next release. I will also make a PR to add usage to the README.