Rails: How to use facets with search results - sql

I have a rails application where I am searching for repair shops. The search class method looks like this:
def self.search(params)
if params
repairshop = Repairshop.where(:approved => true)
if params[:radius].present?
repairshop = repairshop.near(params[:location], params[:radius]) if params[:location].present?
else
repairshop = repairshop.near(params[:location], 200) if params[:location].present?
end
if params[:keywords].present?
repairshop = repairshop.joins(:specializations).joins(:brands_we_services).where("LOWER(specializations.title) LIKE ? OR LOWER(brands_we_services.title) LIKE ? OR LOWER(repairshop.title) LIKE ?", "%#{params[:keywords].downcase}%", "%#{params[:keywords].downcase}%", "%#{params[:keywords].downcase}%")
end
repairshop.uniq
else
all
end
end
A repair shop can have many specializations and brands_we_services. So, searching through all of the names apart form other params is important.
The search form looks like this:
Everything works fine. And the search controller takes me to the search page. The controller action is as follows:
def search
#repairshops = Repairshop.search(params)
end
The search result looks like this:
My question is:
On the left side of the search page, below the search box, I want to provide facets for filtering the results. I want to provide facets which would be dynamic. Like depending on Search results, it provides facets for cities (number of repair shops) etc.
I don't want to use elastic search or solr. I want to understand how I build dynamic faceted search from scratch, the basics of building facets from your search results.
Note: I looked into Forty facets but couldn't understand how can I use my complex class method search which is in Repairshop model and integrate it into the class MovieSearch < FortyFacets::FacetSearch provided by the controller as per the docs.
Will really appreciate your help. Thanks!

Related

Rails Order by frequency of a column in another table

I have a table KmRelationship which associates Keywords and Movies
In keyword index I would like to list all keywords that appear most frequently in the KmRelationships table and only take(20)
.order doesn't seem to work no matter how I use it and where I put it and same for sort_by
It sounds relatively straight forward but i just can't seem to get it to work
Any ideas?
Assuming your KmRelationship table has keyword_id:
top_keywords = KmRelationship.select('keyword_id, count(keyword_id) as frequency').
order('frequency desc').
group('keyword_id').
take(20)
This may not look right in your console output, but that's because rails doesn't build out an object attribute for the calculated frequency column.
You can see the results like this:
top_keywords.each {|k| puts "#{k.keyword_id} : #{k.freqency}" }
To put this to good use, you can then map out your actual Keyword objects:
class Keyword < ActiveRecord::Base
# other stuff
def self.most_popular
KmRelationship.
select('keyword_id, count(keyword_id) as frequency').
order('frequency desc').
group('keyword_id').
take(20).
map(&:keyword)
end
end
And call with:
Keyword.most_popular
#posts = Post.select([:id, :title]).order("created_at desc").limit(6)
I have this listed in my controller index method which allows the the order to show the last post with a limit of 6. It might be something similar to what you are trying to do. This code actually reflects a most recent post on my home page.

read all document by using particular category name using alfresco search.luceneSearch or search.lib.js

Category Name
|
Geograpy (8)
Study Db (18)
i am implement my own advance search in alfresco. i need to read all files which related with particular category.
example:
if there is 20 file under geograpy, lucene query should read particular document under search key word "banana".
Further explanation -
I am using search.lib.js to search. I would like to analyze the result to find out to which category the documents belong to. For example I would like to know how many documents belong to the category under Languages and the subcategories. I experimented with the Classification API but I don't get the result I want. Any Idea how to go through the result to get the category name of each document?
is there any simple method like node.properties["cm:creator"]?
thanks
janaka
I think you should specify more your question:
Are you using cm:content or a customized content?
Are you going to search the keyword inside the content of the file? or are you going to search the keyword in a specific metadata(s)?
Do you want to create a webscript (java or javascript)?
One thing to take in consideration:
if you use +PATH:"cm:generalclassifiable/...." for the categorization in your lucene queries, the performance will be slow (following my experince)
You can use for example the next query to find all nodes at any depth below /cm:Languages:
var results = search.luceneSearch("+PATH:\"cm:generalclassifiable/cm:Languages//*\");
Take a look to this url: https://wiki.alfresco.com/wiki/Search#Path_Queries
Once you have all the elements, you can loop all, and get to which category below. Of course you need to create some counter per each category/subcategory:
for(i = 0; i < results.length; i++){
var node = results[i];
var categoryNodeRef = node.properties["cm:categories"];
var categoryDesc = categoryNodeRef.properties["cm:description"];
var categoryName = categoryNodeRef.properties["cm:name"];
}
This is not exactly the solution, but can be a useful idea to start.
Sorry if it's not what you're asking for, I have just arrived from my holidays.

Sunspot solr: boost scalar fields

I am scoping by multiple scalar fields and I am hoping to push the scoped results to the top of the search results without excluding results that do not meet the criteria for favorite authors.
Articles have and belong to many authors
This doesn't work but is what Im going for:
favorite_author_ids = #current_user.favorite_author_ids
#search = JournalArticle.solr_search do
fulltext params[:article_title]
any_of do
boost(2.0) {with(:author_ids), favorite_author_ids}
with(:author_ids), []
end
end
I suppose I could do two searches and concatenate the results, but Im wondering if there is a cleaner way.
It doesn't make sense to have the boost inside of an any_of block. You probably want to do something like this:
favorite_author_ids = #current_user.favorite_author_ids
#search = JournalArticle.solr_search do
fulltext params[:article_title] do
boost(2.0) {with(:author_ids, favorite_author_ids)}
end
end
You may need to increase the boost as well depending if you just want to increase the placement of favourite authors, or if you want them to always be at the top.

ActiveRecord search model using LIKE only returning exact matches

In my rails app I am trying to search the Users model based on certain conditions.
In particular, I have a location field which is a string and I want to search this field based on whether it contains the search string. For example, if I search for users with location 'oxford' I want it to also return users with a variation on that, like 'oxford, england'.
Having searched the web for the answer to this it seems that I should be using the LIKE keyword in the activerecord search, but for me this is only returning exact matches.
Here is a snippet of my code from the search method
conditions_array = []
conditions_array << [ 'lower(location) LIKE ?', options[:location].downcase ] if !options[:location].empty?
conditions = build_search_conditions(conditions_array)
results = User.where(conditions)
Am I doing something wrong? Or is using LIKE not the right approach to achieving my objective?
You need to do like '%oxford%'
% Matches any number of characters, even zero characters
conditions_array << [ 'lower(location) LIKE ?', "%#{options[:location].downcase}%" ] if !options[:location].empty?

Rails3 - Is there a way to do NOTLIKE?

I previously asked a question regarding pulling specific items out of a database if they contained a specific word in their string, someone kindly offered the following which did just the job:
def SomeModel < ActiveRecord::Base
scope :contains_city,
lambda { |city| where("some_models.address LIKE ?","%"+city+"%" ) }
end
However, I have some instances where I would like to do the opposite, i.e. pull out all the items which do not have the specified word in their string. Is there a way to do a NOT LIKE function? I have prevously seen people use '!=' for a NOT EQUALS, but have had no success along these lines for the LIKE function. Is there an equivalent or is it best to iterate through the database putting items in 2 separate databases based on whether they satisfy the LIKE condition?
You could try NOT LIKE in your query; MySQL supports this.
http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html