Searchkick: Options with multiple indices

Created on 21 Dec 2016  路  8Comments  路  Source: ankane/searchkick

Hi @ankane! One more question.

My usecase is needing me to search across multiple Models and come up with the ranked results. And following your multiple indices example, I have it set up as following:

  @search_results = FirstModel.search(
    query,
    index_name: [ FirstModel, SecondModel, ThirdModel ],
    indices_boost: {
              FirstModel => 2,
              SecondModel => 1.5,
              ThirdModel => 1
            },
    fields: [
              {"name^2"       =>  :word_start},
              {"description"    =>  :word_start},
              {"synonyms^1.5"   =>  :word_start}
            ],
    misspellings: {
              edit_distance:  2,
              transpositions: true
            },
    operator:  "or",
    limit:     10,
    load:      false,
    debug:     true
  )

Two questions at this point -

  1. How can I set up eager-loading when searching across multiple indices?
    There seems to be support for eager-loading with single index but not sure how to set up with multiple index that might have different associations that need to be eager-loaded. Is using search_import scope in each of the model, the best way to go?

  2. In general, what is the tradeoff of setting up an index across multiple models vs searching across multiple indices?

  3. What are the implications above of using Searchkick.search vs FirstModel.search?
    I was getting the following errors (debug=true option) with Searchkick.search, so I resorted to the previously suggested approach of FirstModel.search.

Model Search Data
NoMethodError: undefined method `first' for nil:NilClass

Elasticsearch Mapping
NoMethodError: undefined method `mapping' for nil:NilClass

I came across #344, #383, #744 but none seem to answer the questions above.

Thank you!

Most helpful comment

Despite (the seemingly) popular belief, open source features don't write themselves :)

All 8 comments

_(edited above question for clarity and details)_

  1. Not possible right now - see #701
  2. I've never had to search across multiple models in practice, so don't really know.
  3. Looks like the debug option doesn't take multiple models into account. Will fix.

Good to know, I'll follow that ticket. Quick follow up though - I ended up using search_import scope for eager-loading for each of the models (instead of doing it in the search method). It seems to work the best I can tell.. Does that sound right?

Not sure I follow. search_import only gets called on indexing, not searching.

ah, got it - that's what I was missing in my understanding.

oh well, consider this ticket as one more vote for eager-loading multiple indices :)

Will do.

It's been one of the top requested features for Searchkick for years now but what is a few more years gonna hurt

Despite (the seemingly) popular belief, open source features don't write themselves :)

Was this page helpful?
0 / 5 - 0 ratings