Giter VIP home page Giter VIP logo

Comments (26)

ankane avatar ankane commented on August 15, 2024 2

@MrHubble You'll need use multi search instead (basically, two separate queries) if you want different orders and limits.

https://github.com/ankane/searchkick#multi-search

from searchkick.

ankane avatar ankane commented on August 15, 2024 2

Closing this issue as multi-index search is now supported. Please create a new issue if I missed anything or there are future questions.

from searchkick.

ankane avatar ankane commented on August 15, 2024 2

You'll need to do this in Ruby. Also, as mentioned in the comment above, please create a new issue for new questions so the 15 other subscribers to this issue aren't spammed.

from searchkick.

ankane avatar ankane commented on August 15, 2024

Multi-index search would be nice. What are your thoughts on the best way to declare indexes?

from searchkick.

codker avatar codker commented on August 15, 2024

(fixed the example above, was missing 'index.name' calls)

what about something this:

Searchkick.search("thequery", [Model1, Model2, Model3], other_options)

from searchkick.

aman199002 avatar aman199002 commented on August 15, 2024

Is this method still pending to search across multiple models. I want to use boost and autocomplete across multiple models. If searching on a particular model using index names like:

Model1.search("thequery", index_name: [Model1.index.name, Model2.index.name], boost: 'name')

then, boost will be applied only for that Model1. How can i use boost across multiple models. Like in tire, we can define it while indexing in mapping.

from searchkick.

jamsi avatar jamsi commented on August 15, 2024

Quick (perhaps silly) question, how can I get the index name for a model?

from searchkick.

ankane avatar ankane commented on August 15, 2024

It's now Model.searchkick_index.name.

from searchkick.

jamsi avatar jamsi commented on August 15, 2024

Holy moly that was a fast response. Much appreciated.

from searchkick.

ankane avatar ankane commented on August 15, 2024

Haha, no problem - right place, right time.

from searchkick.

jamsi avatar jamsi commented on August 15, 2024

Ok second question; how can I get the current full index name?

Test.searchkick_index.name is returning "test_development"

But my actual index_name is "test_development_20140219162300398" etc. I need the full name in order to use it alongside index_name: when searching across multiple models.

from searchkick.

ankane avatar ankane commented on August 15, 2024

You should use the alias when searching across models - it's more robust and gives you the same results.

from searchkick.

jamsi avatar jamsi commented on August 15, 2024

Ah my bad, I had mistyped an index name. Thanks again.

from searchkick.

armstrongnate avatar armstrongnate commented on August 15, 2024

+1 to multi-index search. @liberatiluca's suggestion would be awesome.

from searchkick.

amenzhinsky avatar amenzhinsky commented on August 15, 2024

The workaroud which is on the very top of the page doesn't work because index_name option is useless now. Index name is set explicitly index: searchkick_index.name before sending to client

from searchkick.

jdurand avatar jdurand commented on August 15, 2024

I used this for an autocomplete field to match multiple models (indexes) :

results = []

[Model1, Model2, Model3, Model4].each do |model|
  query = Searchkick::Query.new model, params[:query], load: false, fields: [{name: :text_start}], limit: 10
  results = results.concat query.execute.response['hits']['hits']
end

results.sort_by! { |r| r['_score'] }.reverse!

render json: {
  all: results.map { |r| { value: r['_source']['name'] } }.first(10)
}

It shouldn't be too much of a hastle to add a method that uses this logic to the Searchkick::Query class. I'll take a deeper look into this when I have the time.

from searchkick.

ankane avatar ankane commented on August 15, 2024

@amenzhinsky Oops, just fixed. Need to add a test for that. cec8c58

@jdurand What's the advantage to this approach vs the index_name option?

from searchkick.

jdurand avatar jdurand commented on August 15, 2024

@ankane I never got the index_name approach to work and, if I understood correctly, this approach would not let you define different search criterion (ie: boost) for each model.
My approach does 4 separate queries and assemble the results, so we could do something like :

[{model: Model1, boost: 'popularity'}, {model: Model2, boost: 'popularity'}].each do |params|
  ...

I'm just tossing ideas around...

from searchkick.

ahmetabdi avatar ahmetabdi commented on August 15, 2024

Has this been implemented into searchkick yet?

from searchkick.

oliyoung avatar oliyoung commented on August 15, 2024

Another solution for the multimodel search (this will search across all searchkick indexes)
index_name: Searchkick.client.indices.get_aliases.keys,

from searchkick.

Madumo avatar Madumo commented on August 15, 2024

Is it possible to specify an includes option for each model type doing a multisearch that way?

from searchkick.

mnorhamizan avatar mnorhamizan commented on August 15, 2024

Hi,
I'm a beginner with rails

Model1.search(params[:query], index_name: [Model1.searchkick_index.name, Model2.searchkick_index.name])

I tried using it like this but all the results comes only from Model1. Is there anyway to get results from both models.

Both models have different attributes.

from searchkick.

tomav avatar tomav commented on August 15, 2024

👍

from searchkick.

RyanSnodgrass avatar RyanSnodgrass commented on August 15, 2024

The solution I have found comes from using the Searchkick class as my method

# app/models/foo.rb
class Foo
  searchkick
  def search_data
    { name: name }
  end
  def name
  end
end

# app/models/bar.rb
class Bar
  searchkick
  def search_data
    { name: name }
  end
  def name
  end
end
> Foo.reindex
> Bar.reindex
results = Searchkick.search('query')

This will search every index with the name field.


If you want to search just specific fields

# update the `search_data` method to contain your other field
# app/models/foo.rb
class Foo
  searchkick
  def search_data
    { name: name,
      description: description }
  end
  def name
  end
  def description
  end
end
> Foo.reindex
# you can then define which fields to search
results = Searchkick.search('query', fields: ['name', 'description'])

# Cool part is, if one model/index has a field the others don't, searchkick will still work as expected
results = Searchkick.search('query', fields: ['name', 'description', 'unique_field'])

# if you want to boost a certain field, put a carrot to power
results = Searchkick.search('query', fields: ['name^10', 'description'])

from searchkick.

MrHubble avatar MrHubble commented on August 15, 2024

This feature was added to the documentation in July: bd12de7

I'm unsure when the code was implemented.

Does anyone know if it's possible to search multiple indices (ie Searchkick.search "milk", index_name: [Product, Category]) and apply different order and limit to the different indices?

from searchkick.

apuntovanini avatar apuntovanini commented on August 15, 2024

@ankane Sorry to bother, but how can I sort results by _score in a multi_search example?

user_results = User.search(
  "*",
  limit: 20,
  execute: false
)
organization_results = Organization.search(
  "*",
  limit: 40,
  execute: false,
  order: { updated_at: :desc}
)
Searchkick.multi_search([user_results, organization_results])

I'd like to see organizations with higher score before than users. Thanks in advance

from searchkick.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.