Nested Document error on ES Tire Gem Ruby - ruby

I'm using the Tire gem still to work with a 0.9 ES database.
I've got a model that looks like this:
class Influencer
include Tire::Model::Persistence
include Tire::Model::DynamicPersistence
document_type "influencer"
# profiles
property :profiles, :type => 'nested', :class => [Profile], :default => []
end
I've also got a Profile class that looks like this.
class Profile
include Tire::Model::Persistence
include Tire::Model::DynamicPersistence
document_type 'profile'
property :id, :index => :not_analyzed
property :username, :index => :not_analyzed
property :service, :index => 'keyword'
property :url, :index => :not_analyzed
property :score, :type => 'float', :index => :not_analyzed
end
Note I removed many fields.
So, when I blow away the index and recreate it for influencers, I get an empty index. On the first write, it creates the mapping below:
{"influencer"=>
{"properties"=>
{"data"=>{"type"=>"object"},
"demographics"=>{"type"=>"object"},
"domain_names"=>{"type"=>"string"},
"geographics"=>{"type"=>"object"},
"host_names"=>{"type"=>"string"},
"id"=>{"type"=>"string"},
"income_message_numbers"=>{"type"=>"object"},
"influencer_id"=>{"type"=>"string"},
"keyword_scores"=>{"type"=>"object"},
"keywords"=>{"type"=>"string"},
"manual"=>{"type"=>"boolean"},
"name"=>{"type"=>"string"},
"notes"=>{"type"=>"object"},
"profiles"=>
{"properties"=>
{"profile"=>
{"properties"=>
{"contributors_enabled"=>{"type"=>"boolean"},
"default_profile"=>{"type"=>"boolean"},
"description"=>{"type"=>"string"},
"favourites_count"=>{"type"=>"long"},
"followers_count"=>{"type"=>"long"},
"friends_count"=>{"type"=>"long"},
"id"=>{"type"=>"long"},
"lang"=>{"type"=>"string"},
"listed_count"=>{"type"=>"long"},
"location"=>{"type"=>"string"},
"name"=>{"type"=>"string"},
"profile_image_url"=>{"type"=>"string"},
"protected"=>{"type"=>"boolean"},
"service"=>{"type"=>"string"},
"statuses_count"=>{"type"=>"long"},
"url"=>{"type"=>"string"},
"username"=>{"type"=>"string"}}}}},
"project_profiles"=>{"type"=>"object"},
"project_statuses"=>{"type"=>"object"},
"project_tags"=>{"type"=>"object"},
"project_time_additions"=>{"type"=>"object"},
"reachable_via"=>{"type"=>"string"},
"share_counts"=>{"type"=>"object"},
"source_ids"=>{"type"=>"string"},
"sources"=>{"type"=>"string"}}}}
This is what the first hash that writes looks like. I THINK it looks correct, but I could very well be missing something.
{:name=>"DogeWire", :profiles=>[{"service"=>"twitter", "id"=>2465530645, "name"=>"DogeWire", "username"=>"DogeWire", "description"=>"Fast, safe, anonymous, wow", "location"=>"www.DogeWire.com", "protected"=>false, "followers_count"=>466, "friends_count"=>1990, "listed_count"=>2, "favourites_count"=>19, "statuses_count"=>8421, "lang"=>"en", "contributors_enabled"=>false, "profile_image_url"=>"http://pbs.twimg.com/profile_images/460517818555846656/-0Ibxod4_normal.jpeg", "url"=>"http://twitter.com/DogeWire", "default_profile"=>true, "time_zone"=>nil}], :source_ids=>["twitter:2465530645"], :sources=>["twitter"], :keywords=>["altcoin"], :reachable_via=>["twitter"], :tags=>[], :host_names=>["www.lill.com"], :domain_names=>["lill.com"], :id=>"74uF9cNfW6dYEa27kSg5ww", :influencer_id=>"74uF9cNfW6dYEa27kSg5ww"}
And, lastly, the query looks like:
site_influencers = Influencer.search(:page => 1, :per_page => 10) do
query do
nested :path => 'profiles' do
query do
boolean do
must { match 'profiles.service', 'twitter' }
end
end
end
end
end
I'm not sure what i'm missing on this. This is a ruby app.. no Rails (but has ActiveRecord, etc).
Any thoughts?

Related

Ruby DataMapper::ImmutableError

get '/watch/:id' do |id|
#results = Twitchtvst.all( :fields => [:Twitchtv ],
:conditions => { :user_id => "#{id}" }
)
#p #results.inspect
#results.each do |result|
puts result.id
end
erb :mystream
end
I get this error message immutable resource cannot be lazy loaded. How do I fix this?
The Error message is:
DataMapper::ImmutableError at /watch/1
Immutable resource cannot be lazy loaded
According to the official documentation:
Note that if you don't include the primary key in the selected columns, you will not be able to modify the returned resources because DataMapper cannot know how to persist them. DataMapper will raise DataMapper::ImmutableError if you're trying to do so nevertheless.
I know that you are not modifying anything here but I think that the same rule applies for lazy loading. So I will suggest to try it like that:
#results = Twitchtvst.all( :fields => [:Twitchtv, :id],
:conditions => { :user_id => "#{id}" }
) ode here
Note the id as an additional field.

checking if a html element is true with ruby and watir?

at the moment I have this:
def burnChart()
#browser.div(:id => "container").div(:id => "header").img(:class => "cogMenuHover").click
#browser.div(:id => "container").div(:id => "header").div(:class => "sbTopMenu").li(:class => "taskMenuOp", :index =>1).click
if
assert(#browser.send(type.to_sym, :class, "highcharts-grid").exists?)
puts 'Chart has been found!'
else
puts 'No chart was generated'
end
end
originally I thought I had to use to_css? but from what I've seen of others using it, that's incorrect I'm unsure.
Can anyone help me out, I just want to check if a class exists and return a true or fasle to log an error or a confirmation
I believe you want:
def burnChart()
#browser.div(:id => "container").div(:id => "header").img(:class => "cogMenuHover").click
#browser.div(:id => "container").div(:id => "header").div(:class => "sbTopMenu").li(:class => "taskMenuOp", :index =>1).click
#Due to using IE, it looks like .exists? has to be used.
if #browser.element(:class, "highcharts-grid").exists?
puts 'Chart has been found!'
else
puts 'No chart was generated'
end
end
Notice that:
The check for the element should be the condition for the if statement.
Changed the check to be .present?. .exists? returns true as long as the element is on the page, but usually you also want to check that it is visible.
The switch to #browser.element instead of #browser.send. You can do what you were doing, but you were not defining a type anywhere, so seemed unnecessary.

How can I use field selectors in the LinkedIn gem?

I have been trying to use the LinkedIn gem (0.3.6) to search. I can successfully authenticate and search with just keywords but I want to be able to use the field selectors so that my results contain more than just id, first name, last name.
I have been unsuccessful when doing what is in the spec
fields = [{:people => [:id, :first_name, :last_name, :public_profile_url, :picture_url]}, :num_results]
client.search(:first_name => 'Giliardi', :last_name => 'Pires', :fields => fields)
Has anyone been able to get this to work?
It seems that the gem client.search does not match the github client.search... and the spec is based on the github client.search. And to be honest I cannot figure out how to get it to work with the Gem search. They do not include a spec for search in the Gem example provided in the api_spec.rb (no search_spec) gives a 404: client.search(:first_name => "Javan", :fields => ["num_results", "total"])
My suggestion would to be build the gem from the github source and use the selectors.
Gem:
def search(options={})
path = "/people-search"
options = { :keywords => options } if options.is_a?(String)
if fields = options.delete(:fields)
path +=":(#{fields.map{ |f| f.to_s.gsub("_","-") }.join(',')})"
end
options = format_options_for_query(options)
result_json = get(to_uri(path, options))
Mash.from_json(result_json)
end
GitHub:
def search(options={})
path = "/people-search"
if options.is_a?(Hash)
fields = options.delete(:fields)
path += field_selector(fields) if fields
puts path
end
options = { :keywords => options } if options.is_a?(String)
options = format_options_for_query(options)
result_json = get(to_uri(path, options))
Mash.from_json(result_json)
end

Using middleman, how do you include one HAML file in another HAML file?

I'm using middleman to do some rapid prototyping and can't for the life of me figure out how to include one HAML file into another HAML file.
I can include stuff in a layout file, but can't get one non-layout file to include another non-layout file. There are blocks of HTML that I want to reuse on some pages and I think I could do this. I've tried:
- render: partial=>"shared/nav.haml"
=shared/nav.html
="shared/nav.html
and none of these work.
Am I missing a config option or plugin? This is a fresh middleman install.
ANSWER
Partials may need file names that start with an underscore. My partial is placed in a folder called shared. The full name of the file is _nav.html.haml
This worked for me.
!= haml :"shared/_nav"
Example in context:
#email.main.subscriber.resize
#bg-wrap
%div
%img{:src=>"images/backgrounds/image.png",:alt=>""}
%section#zone10
!= haml :"shared/_nav"
You may also use the format specified in the approved answer below.
I've been using HAML with MiddleMan and couldn't be happier. Here is what is working for me:
I have a file: source/_donate_buttons.h
#DonationButtons
%p= t('searching.donate_cover_costs')
%br
= partial(:paypal_donate_button, :locals => {:amount => 1,
:amount_text => t('searching.donate_1')})
This uses the partial statement shown to include a file called source/_paypal_donate_button.html.haml.
And I include the _donate_buttons.html.haml file itself in a couple of places with:
= partial "donate_buttons"
though I think this could also be:
= partial :donate_buttons
I.e. I think partial is the magic you're looking for.
And, just for completeness, here is a slightly stripped down _paypal_donate_button.haml which shows how the paramaterization works there:
-btnclass = (locals.key?(:highlight) && locals[:highlight] ? "HighlightedDonationButton" : "DonationButton")
-btnstyle = locals.key?(:button_style) && locals[:button_style]
.DonationButtonContainer
%form{:action => "https://www.paypal.com/cgi-bin/webscr", :method => "post"}
%input{:name => "business", :type => "hidden", :value => "payments#example.com"}
%input{:name => "cmd", :type => "hidden", :value => "_donations"}
%input{:name => "amount", :type => "hidden", :value => "#{amount}.00"}
%input{:name => "currency_code", :type => "hidden", :value => "USD"}
%input{:class => btnclass, :alt => t('paypal.alt_text'),
:style => "cursor: pointer; font-size: 18px; #{btnstyle}", :type => "submit", :value => amount_text}
Fwiw, I don't think the file needs to be _filename.html.haml and can instead be _filename.haml. Also, I'm localizing these, so ignore the t('tagname') and just put strings there. (I didn't want to introduce an error copy-pasting the examples so I left them in there.)
Hope this helps!

Is it possible to specify what index a query should use in Mongoid?

MongoDB seems like it is using an inefficient query pattern when one index is a subset of another index.
class Model
field :status, :type => Integer
field :title, :type => String
field :subtitle, :type => String
field :rating, :type => Float
index([
[:status, Mongo::ASCENDING],
[:title, Mongo::ASCENDING],
[:subtitle, Mongo::ASCENDING],
[:rating, Mongo::DESCENDING]
])
index([
[:status, Mongo::ASCENDING],
[:title, Mongo::ASCENDING],
[:rating, Mongo::DESCENDING]
])
end
The first index is being used both when querying on status, title and subtitle and sorting on rating and when querying on just status and title and sorting on rating even though using explain() along with hint() in the javascript console states that using the second index is 4 times faster.
How can I tell Mongoid to tell MongoDB to use the second index?
You can pass options such as hint to Mongo::Collection using Mongoid::Criterion::Optional.extras
An example:
criteria = Model.where(:status => true, :title => 'hello world').desc(:rating)
criteria.extras(:hint => {:status => 1, :title => 1, :rating => -1})
extras accepts anything that Mongo::Collection can handle
http://www.mongodb.org/display/DOCS/Optimization#Optimization-Hint
While the mongo query optimizer often
performs very well, explicit "hints"
can be used to force mongo to use a
specified index, potentially improving
performance in some situations.
db.collection.find({user:u, foo:d}).hint({user:1});
You need to work from http://www.rdoc.info/github/mongoid/mongoid/master/Mongoid/Cursor here as I do not know Ruby enough. It mentions hint.

Resources