Sort by date using acts_as_solr - ruby-on-rails

I have gotten sorting working from my rails application using acts_as_solr for text fields, as seen below with Title.
I am having problems getting it to work for date.
My model has the following
class Article < ActiveRecord::Base
acts_as_solr :fields[:title, {:title_s=> :string}, {:created_at_d => :date}]
def title_s
self.title
end
def created_at_d
self.created_at
end
The following is being sent to the solr server:
path=/select params={wt=ruby&rows=10start=0&sort=created_at_d_d+asc&fl=pk_i,score&q=(+searchtext)..........
The solr code
Article.paginate_all_by_solr(searchString, :order=> "created_at_d asc", :page = page, :per_page => results_per_page, :total_entrieds => count)
Is there something obvious I am doing wrong? I am not sure that the {:created_at_d => :date} in the model is the correct way to set up the index for dates.
When I just coin it off of :created_at, I get the error around tokenized fields similar to that when I tried to sort against :title.

Something was wrong with the index.
When I went into script/console and ran
Article.rebuild_solr_index
It did not index the date correctly.
I created a rake task from with the following site.
http://henrik.nyh.se/2007/06/rake-task-to-reindex-models-for-acts_as_solr
When I ran
>rake solr:reindex
The index was created correctly and the date sorting started working.

Related

Rails 5 Friendly Way to Optimize Query

I like to simplify my approach to the following query. (Using MySQL and Rails 5.0.1)
I have a Prospect with many Results. I want to generate a list of last updated Results on all Prospects with results to use in my controller.
Prospect
has_many :results
Result
has_one :Prospect
This works in my Propects model
def self.last_results
customer_ids=Result.where("results.disposition" => "appointment").where.not("results.event" => nil).distinct.pluck(:prospect_id)
all=[]
customer_ids.each { |c|
last_result= self.includes(:prospect)
.where("results.prospect_id" => c)
.order("results.created_at ASC")
.last
all << last_result
}
return all
end
I think there must be an easier rails way to do this without creating multiple loops with queries.

Sunspot rails: include associated models when calling .results and how to correct syntax for it

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 :)

RoR Method to search for data in a table and then update another column on another table based on that data

I have a program i am making changes to for my work. I know what i must do but i am having problems getting the code to work. I come from a Java and C background.
i have two tables one table called customprojecschedule_lines has a project_id,workorder_base_id, and other various columns column.
The other table called customschedule has an id, workorder column and various other columns.
I have a method and variable called work order.
I am trying to get an SQL statement that will do that like this:
class Customschedule < ActiveRecord::Base
set_table_name "customschedules"
after_create :build_customprojectschedule_lines
has_many :customprojectschedule_lines, :dependent => :destroy
has_one :projectschedule_cost
delegate :est_cost, :act_cost, :to => :projectschedule_cost, :allow_nil => true
attr_accessor :workorder_base, :lots
def workorder
customschedule.where(:id => customprojectschedule_lines.product_id)
end
def workorder=(wo)
#workorder_base = wo
customprojectschedule_lines.each do |pl|
pl.update_attributes({:workorder_base_id => wo})
end
end
def build_customprojectschedule_lines
lines = #lots.split(',').inject([]) do |lines, lot_id|
line = customprojectschedule_lines.find_or_initialize_by_workorder_lot_id(lot_id)
if line.new_record?
p workorder_base
line.workorder_base_id = #workorder_base
line.line_no = lot_id
line.workorder_split_id = 0
end
lines << line
end
customprojectschedule_lines.replace(lines)
end
Basically what i would like is that whenever a user enters a workorder on the form number goes into the database gets the stored values with that record and then retrieves the ID(from that record) and puts that same id in my other table.
However, i keep getting this error:
undefined local variable or method `customschedule' for #
<Customschedule:0x00000005542040>
Before when i was trying things i kept getting a weird looking select statement saying that Customschedule ID was null.
We use oracle here.
This is my first time posting on here so please let me know if i need anything else.
Any help is greatly appreciated.
I think all you need is to upcase the first letter in this line
customschedule.where(:id => customprojectschedule_lines.product_id)
change it to
Customschedule.where(:id => customprojectschedule_lines.product_id)

Search integers with Sunspot

I managed to get Sunspot working in my rails setup. My rails setup renders graphs (Chartwell) with input from my database (integers), example: "design: 80, art: 20, code: 40".
Is there a way that I can search for "design" and get all elements with design > 70 (integer, for instance) as output?
You're not giving code, so I'll improvise.
Since you have everything working, I assume you already have defined your design field indexed in searchable definition in your model.
After you've done that, you will have a .search block for Sunspot (or Model) in your active code (most probably in a controller).
So, let's suppose your model name is Graph.
design = params[:design] # guessing, again.
Graph.search do
with(:design).greater_than(design)
# .... other conditions
end
This should work for you.
UPDATE:
Assuming that you have a design:integer column in your db
In your event.rb:
searchable do
text :name, :location, :date_search
integer :design
end
in your events_controller.rb:
#search = Event.search do
fulltext params[:search]
with(:design).greater_than(params[:design].to_i)
end
Note: You should pass :design as a parameter with your search form

Fetch Google Analytics for Model Instances in Ruby on Rails

I am trying to fetch the page views for each instance of our model Campaigns. After much Googling, I used this post as the basis of my work: http://gistflow.com/posts/398-pageviews-for-posts I am instead using the updated Legato gem.
I am having trouble fetching and saving to the database the pageviews for each Campaign.
Here is the modified code working from the gistflow post.
Rake task:
namespace :campaigns do
task :update_page_views => :environment do
google_analytics = GoogleAnalytics.new
#campaigns = Campaign.all
#campaign = Campaign.find(:all)
#campaigns.each do |campaign|
page_views = google_analytics.page_views(campaign)
campaign.update_attribute(:page_views, page_views) if page_views
end
end
end
Legato model:
class GoogleAnalytics
extend Legato::Model
metrics :pageviews
dimensions :page_path
attr_reader :profile
def page_views(campaign)
access_token = GoogleOauth2Installed.access_token
user = Legato::User.new(access_token)
profile = user.profiles.first
GoogleAnalytics.results(
profile,
:filters => {:page_path.eql => campaign.path},
:start_date => 5.years.ago.to_date,
:end_date => Date.today
).to_a.first.try(:pageviews)
end
end
However, I receive an error
undefined method `path' for #<Campaign:0xbf334a4>
If I change the filter line to:
:filters => {:page_path.eql => campaign},
then the task runs, but all of the campaigns have the same number of views which matches the total of all of the campaigns.
I played around with the GA Query tool mentioned in Getting query results with Legato gem
and can get a result I want with:
filters = ga:pagePath==/campaigns/6
However, even if I plug that into the code, the task still results with all of the campaigns having the same number which is the total of campaigns.
I have also looked at this answer How to pull Google Analytics stats? but do not have enough reputation yet to ask Severin to explain how to modify the query.
I have looked into the Impressionist gem but would like to make it work with GA as we would like to pull more GA data in the future.
Thanks so much for any help!!

Resources