Sphinx search tricks - ruby-on-rails

I have some questions regarding sphinx search and thinking sphinx gem
Is it possible to find similar results for record? For example - similar posts for current post in blog application?
If my app is indexing different models (for example in blog app - posts, comments and pages) is it possible to perform search on every model with one request, but not performing search in every model (Post.search 'string', Comment.search 'string' etc)?

Dont know about part 1 of your question, but i'm sure you can do the second i.e. Search across multiple models.
Here's the reference
http://freelancing-god.github.com/ts/en/searching.html#global

Related

Indexing criteria for elasticsearch

I am working with twitter streaming api. and am a little confused about deciding the criteria for indexing the data. Right now I have a single index that contains all the tweets in one doc_type and users in another doc type.
Is it the best way to go about storing them or should i create a new doc type for every category (category can be decided on basis of hashtag and tweet content)
What should be the best approach to storing such data?
Thanks in advance.
At first, the answer to your question is that this very much depends on your use case. What is your application doing? What do you do with the tweets? How many categories do you plan to have?
I'd in general, however, go for a solution where you use the same index and the same doc_type for all tweets. This allows you to build queries and aggregations over all your tweets without thinking about the different types of categories. It also allows you to add new categories easily without having to change your queries.
If you want to do some classification of the tweets you could add a category field to the tweet document stored in elasticsearch. You can then use this category field to implement your specific application logic.
If your category names have spaces or punctuation marks don't forget to define the category field as not_analyzed. Otherwise it will be broken up in parts.

sunspot/solr multiple indices

I have sunspot/solr set up to search products on my site. We need the ability to search users and another model (too much to explain what this is) in out app. Basically there is form for searching product via solr and this works well. There would be another form for searching users and the other form to search the other model.
I assume it is recommended to have a separate index for products, users, and the other model? It's seems best to keep the index from getting too bloated? Am I on the right track here?
All the models are indexed in the same index. And sunspot will also index the classnames into the index.

Saving search results rails

I'm doing a rather complex search on few of my models. I combine the results into one array which contains all of my models. I'm wondering what is the best way to get the permlink to my search.
I was thinking of creating another model named SearchResult which would have many to many relation to all my models in search described above.
So that way I can reference from single SearchResult all the models that are part of that search result? Is this a good approach to this? Should I do something else instead?
Theres a tutorial for this exact scenario on RailsCasts.
http://railscasts.com/episodes/111-advanced-search-form-revised

Returning Search Results in Rails

I am having a problem implementing a special kind of search for my Rails application. I am working on an achievement system where you can search for a set of users in a search form (e.g., the query being "Ross, Adam, Jake") and it returns all of the common achievements that the users have unlocked (e.g., if users Ross, Adam, and Jake all had an achievement named "You are winner!"). I have three tables, one for achievements, one for users, and a join table. We have tested the associations and such, so we know that works.
My first idea was to put the search terms in an array and get the search results for each item in the array and place them into respective "search result arrays". Then, I was thinking to go through each item in search result array 1 to see if it appears in both of the other result arrays. The objects that appear in all three of the search result arrays would be returned and displayed on a page.
Is there an easy way to implement this without writing a bunch of my own code? Are there some functions I should know about? Any help will be appreciated!
Well, both Ransack and it's predecessor (MetaSearch) are useful gems for creating complex search forms.
In general I think you want to do something like select distinct achievement ids for user ids in an array. Off the top of my head I'm not quite sure how you should write it... others may know.
Look at the documentation on MetaSearch (more established) and see if you see a pattern that fits, if not check Ransack (more advanced).
You can use some autocomplete plugin for user names and convert the names to ids on the fly, that way you won't have to deal with converting user names to ids in backend later.
For common achievements, if a user can have a achievement only once, aggregating the results in join table and counting the results with achievement ids would be the way to go.
You can provide more details for a more detailed answer. :)
You can use Sunspot which is allows easy solr integration with Ruby and Rails

Where to put model search logic in a Rails application?

I'm trying to figure out the "best" place to put multi-attribute search form logic in a Rails application. The search form in question has several attributes which may or may not have values, and the data types differ between attributes. (For example, there are search options to search for items with a price attribute between two numbers, date ranges, string values, etc.) Also, the model in question has several nested attributes through has_a/has_many relationships and some of those attributes also need to be searchable.
The Rails mantra of thick model, thin controller makes me hesitant to try to aggregate the search logic into the controller. However, it also doesn't seem appropriate to put logic relating to constructing the search conditions in the model(s). Finally, in the spirit of DRY, I'm hesitant to hard-code a bunch of specific attribute names into some module since I will need to apply similar search logic to several unrelated models. Perhaps a naming convention of the form fields in the search view could be used to construct the right conditions? (Something like using prefixes like "min_", "max_", "startdate_" indicating the data type and search condition operator and the suffix being the name of the model and/or attribute?)
I've searched for advice on this, but most of the advice seems inflexible (hardcoded attribute names, no support for nested attributes) or to use route-based searches which I don't think will work for my need (where 5-10 parameters may be used in a search at once).
Any suggestions on the "Rails way" of doing this?
If you are using 2.3.x, I've always found Searchlogic to be a solid and flexible starting point for searching ActiveRecord models where you don't need fulltext search. It supports associations, your own named scopes, etc.
If you are using Rails 3, the meta_search gem appears to offer similar functionality, but my experience with it is extremely limited.

Resources