Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-index Searching in ElasticSearch (Tire)

Large web applications offer a "global" search which combines data from various fulltext indices (this would be a table in SQL) to provide a combined search result ordered by its score. So lets say you have videos, blog articles and users, then when you type in "home" into the search it could yield results like this (ordered by score):

  • My Home (Blog Article)
  • Home Town (User)
  • I want to go home (video)

Does anyone know how to perform such a combined search using ElasticSearch? Preferably using the Tire gem for Rails, but raw ElasticSearch JSON data would also work.

Thanks.

like image 896
matsko Avatar asked Mar 04 '12 18:03

matsko


People also ask

How do I search multiple indices in Elasticsearch?

To search multiple data streams and indices, add them as comma-separated values in the search API's request path. The following request searches the my-index-000001 and my-index-000002 indices. You can also search multiple data streams and indices using an index pattern.

Can Elasticsearch have multiple indexes?

Elasticsearch features a powerful scale-out architecture based on a feature called Sharding. As document volumes grow for a given index, users can add more shards without changing their applications for the most part. Another option available to users is the use of multiple indexes.

How many indexes can Elasticsearch create?

Why do you need one per user? What sort of data is it? Elasticsearch does not impose any strict limit to the number of indices or shards, but that does not mean that there are not practical limits. Having an index per user adds a lot of flexibility and isolation, but unfortunately does not scale well at all.


1 Answers

regarding Tire, there's an important distinction: are you using it standalone or in the ActiveRecord/ActiveModel integration.

In the former case, just search against multiple indices, Tire.search ['indexA', 'indexB'] do ... end. Or search against the whole cluster, Tire.search do ... end (equivalent to curl 'http://localhost:9200/_search?q=*').

In the latter case, Tire does not handle multi-model searches well, at the moment. Notice this pull request: https://github.com/karmi/tire/pull/218 -- build your gem with this patch applied and help test out the solution.

UPDATE

Current Tire (> 0.4) can load multiple model instances just fine. See the integration test for example.

like image 71
karmi Avatar answered Sep 21 '22 23:09

karmi