I have a search implemented with the gem sunspot_solr. I would like to add links in the search results to be able to filter the search by alphabetical order and other parameters.
my controller is
class SearchController < SuperSiteController
def index
#sunspot_search = Sunspot.search User, Post do |query|
query.keywords #search_query
query.paginate(:page => params[:page], :per_page => 30)
end
#posts = #sunspot_search.results
end
I wish I could filter for older, recent, and alphabetical order within your search has already been completed. I did not find anything in the documentation about this.
Has anyone worked with this type of search before, and if so, do you know the best practice for doing this?
I think you aim to order results based on a property (data field) of documents.
For example, to sort the results of Post model by created_at, you have to add this field to the index definition in app/models/post.rb as below:
class Post < ActiveRecord::Base
searchable do
...
time :created_at
...
end
end
And then in you SearchController sort the result as below:
class SearchController < SuperSiteController
def index
Post.search do
fulltext #search_query
paginate(:page => params[:page], :per_page => 30)
order_by :created_at, :desc #:desc for descending, :asc for ascending
end
#posts = #sunspot_search.results
end
In order to order results alphabetically you just have to add the field to you index definition as what mentioned above with text function instead of time and use order_by in SearchController.
Related
Rails 6
will_paginate
In my model, I have the following:
def index
#books = Book.all.paginate page: params[:page], per_page: params[:per_page]
end
which is going to give me an array of objects, each with the following attributes:
name
author_id
So, in my view, I can do the following:
th= sortable "name", "Name"
th= sortable "author_id", "Author ID"
But what I really want to do, is show the Author Name, which I can get from the book object, as follows:
book.author.name
As book belongs to author
How do Have a column in the table, with the author's name, and make that column sortable?
Did you tried something like this?
controllers/books_controller.rb
def index
books_with_author = Book.all.map { |book|
book.attributes.merge(author_name: book.author.name)
}
#books = books_with_author.paginate page: params[:page], per_page: params[:per_page]
end
Also you can define to your model for future use
models/book.rb
def author_name
self.author.name
end
A similar question has already been asked
Sunspot rails: include associated models when calling .results
search = Sunspot.search(ArticlePost, Post, User, Group) do
fulltext query
with(:api_search_shared, true)
paginate :page => page, :per_page => 100
end
what i want to do is include a few other tables with the query something like that:
include [{:user => [:user_job_title, :user_departments], :group => []}]
How would you go about go about putting the include in for when you are searching multiple models?
This is an example of a Single one:
Event.search(:include => [:user]) do...
this solution works for me :
search_in = [Post, Tweet]
search = Sunspot.search search_in do
search_in.each{|m|data_accessor_for(m).include = [:user]}
[...]
end
Hope this solution help.
Have a nice day :)
I have a Rails app in which I use Thinking-Sphinx for search and ActsAsTaggableOn for tagging. I want to be able to include the currently selected tag in my search query. I have tried the following but not got it to work.
In my controller:
def show
#team_tags = #team.ideas.tag_counts_on(:tags)
if params[:tag]
#ideas = #team.ideas.search(params[:search], :conditions => { tags: "tag" })
else
#ideas = #team.ideas.search(params[:search])
end
end
My index for my Idea model:
ThinkingSphinx::Index.define :idea, :with => :real_time do
[...]
indexes tags.name, :as => :tags
has user_id, type: :integer
has team_id, type: :integer
[...]
end
This gives me he following error:
ActionView::Template::Error (index idea_core: query error: no field 'tags' found in schema
When I have a tag selected my URLs looks like this:
/teams/1/tags/tag
So, what should I do to get Thinking-Sphinx and ActsAsTaggableOn to work together?
What you've got for your field will only work with SQL-backed indices, not real-time indices.
In your case, what you want to do is create a method in your model that returns all the tag names as a single string:
def tag_names
tags.collect(&:name).join(' ')
end
And then you can refer to that in your index definition:
indexes tag_names, :as => :tags
Once you've done that, you'll need to regenerate your Sphinx indices, as you've changed the structure: rake ts:regenerate.
I have a column called cached_votes_up in Communities table.
Now I'd like to fetch the records ordered by its number.
#search = Community.search do
fulltext params[:search]
with(:genre_id, params[:genre])
order_by :cached_votes_up, :desc
paginate :page => params[:page], :per_page => 5
end
#communities = #search.results
But this returns the following error:
No field configured for Community with name 'cached_votes_up'
You need to index the cached_votes_up attribute of your object. Add these lines in your Community model:
searchable do
integer :cached_votes_up
end
Solved
I want to have my :price and :purchase_date fields to be ordered together in my search index. So it will give the most present date and then the lowest price of the following date. How is this done?
I think there is a bug when doing:
order_by :purchase_date, :desc
order_by :price,:asc
compared to:
order_by(:purchase_date, :desc)
order_by(:price,:asc)
My finished code is like this:
class SearchController < ApplicationController
def index
#search = UserPrice.search do
fulltext params[:search]
paginate(:per_page => 5, :page => params[:page])
facet :business_retail_store_id, :business_online_store_id
order_by(:purchase_date, :desc)
order_by(:price,:asc)
end
#user_prices = #search.results
end
end
UserPrice.order(:by => [:price,:purchase_date])
UserPrice.order("price DESC, purchase_date DESC")
See:
http://guides.rubyonrails.org/active_record_querying.html
http://m.onkey.org/active-record-query-interface
Here's an example from the Sunspot site:
Post.search do
fulltext 'best pizza'
with :blog_id, 1
with(:published_at).less_than Time.now
order_by :price, :desc # descending order , check Documentation link below
order_by :published_at, :desc # descending order , check Documentation link below
paginate :page => 2, :per_page => 15
facet :category_ids, :author_id
end
The Sunspot documentation says:
order_by(field_name, direction = nil) Specify the order that results should be returned in. This method can be called multiple times; precedence will be in the order given.
See:
http://outoftime.github.com/sunspot/
http://outoftime.github.com/sunspot/docs/index.html