Comments (26)
@MrHubble You'll need use multi search instead (basically, two separate queries) if you want different order
s and limit
s.
https://github.com/ankane/searchkick#multi-search
from searchkick.
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.
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.
Multi-index search would be nice. What are your thoughts on the best way to declare indexes?
from searchkick.
(fixed the example above, was missing 'index.name' calls)
what about something this:
Searchkick.search("thequery", [Model1, Model2, Model3], other_options)
from searchkick.
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.
Quick (perhaps silly) question, how can I get the index name for a model?
from searchkick.
It's now Model.searchkick_index.name
.
from searchkick.
Holy moly that was a fast response. Much appreciated.
from searchkick.
Haha, no problem - right place, right time.
from searchkick.
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.
You should use the alias when searching across models - it's more robust and gives you the same results.
from searchkick.
Ah my bad, I had mistyped an index name. Thanks again.
from searchkick.
+1 to multi-index search. @liberatiluca's suggestion would be awesome.
from searchkick.
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.
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.
@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.
@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.
Has this been implemented into searchkick yet?
from searchkick.
Another solution for the multimodel search (this will search across all searchkick indexes)
index_name: Searchkick.client.indices.get_aliases.keys,
from searchkick.
Is it possible to specify an includes option for each model type doing a multisearch that way?
from searchkick.
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.
👍
from searchkick.
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.
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.
@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)
- While using opensearch-ruby got error: end of file reached (Faraday::ConnectionFailed) HOT 1
- Default index name doesn't use UTC time HOT 1
- minor issue when when doing a full reindex in queue mode HOT 1
- Make "Searchkick.search" for multi-model searches work with Rails low-level caching HOT 4
- Why is `_script` filters commented out in the source code? Possible to add support for scripts? HOT 2
- Index different objects to different indices HOT 2
- Integrate partial reindexing with mode_async bulk reindexing
- `scope_results` outputs incorrect warnings HOT 1
- rails production env indexmissing issue HOT 1
- Ability to set body_options globally in the same way you can model_options
- Clarify use of ActiveRecord `where` in `search_import` HOT 1
- Unable to load dynamic synonyms with Elastic HOT 1
- Support for Dynamoid (DynamoDB) HOT 2
- Add Semantic Search support HOT 2
- Make it also possible to set track_total_hits to an integer HOT 1
- Share indexes across environments? HOT 1
- Add support search_pipeline for OpenSearch client HOT 1
- No Mapping Found Error HOT 1
- Request level options for update requests HOT 2
- Attempting a full reindex can intermittently return an 'index_not_found_exception' HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from searchkick.