Sunspot solr: boost scalar fields - ruby-on-rails-3

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.

Related

Rails: How to use facets with search results

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!

Remove duplicates from custom search query

I have a search method that combines the results from doing a fuzzy search using the fuzzily gem and doing a search in the tags used in acts_on_taggable gem.
if params[:search]
results = Post.find_by_fuzzy_title(params[:search])
tagged_results = Post.tagged_with("#{params[:search]}")
#posts = (Kaminari.paginate_array(results+tagged_results)).page(params[:page]).per(1)
else
The problem is that the returned query contains a concatenation of the two individual queries with duplicate results. What I would like to do is make sure a result is displayed only once even if it contains both a matching title and a matching tag to the search keywords.
Use this out of the ruby standard library
(Array1 + Array2).uniq

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.

Help optimizing get all children query (ActiveRecord/Ruby/Rails)

This is just a quick question on performance in an sql query using ruby and rails.
Basically I have a parent model and a bunch of children which have the variable of parent_ID.
I first gather all the parents with a specific condition and then I cycle through each parent finding any children that match.
Unfortunately this is incredibly slow and I was wondering if theres any help going in optimizing it.
#parents = Parent.where(:parent_id => 3) #This is passed in from params
#childrenArray =[]
#parents.each_with_index do |parent, index|
#TOOSLOW
#childrenArray[index] = Child.find(:all,:order=>"id",:conditions =>{:parent_ID=> parent.id})
end
One thing I thought is perhaps I should make an array of all the parent Ids to be searched and then do something like
child.find_by_parent_ID(myarrayofnumbershere)
However I don't know if this would be any better.
Any help or advice appreciated.
I'm very new to SQL and ruby. I'm aware a table joins would have been ideal here but I think I'm a bit late in my development to try it now. Also I need to serve up 2 seperate arrays. one of the parents and one of the children.
Try using the include method, like so:
#parents = Parent.where(:parent_id => 3).include(:children)
Now rails will have fetched the associated children and you should be able to loop over #parents and access their children without additional queries, like so:
#parents.each do |p|
puts "#{p}'s children: #{p.children}"
end