Thinking Sphinx text search - ruby-on-rails

Hi people I'm having a problem with one search that I'm making in one of my projects. I am new using Sphinx and the problem is when I make a search like = "ipad" it work perfect but when I put "ipa" the full text search seems no to work the way I want cause I'm not receiving anything back.
my_variable = self.search searchTxt, :match_mode => :any, :sort_mode => :extended, :order => sortType, :page => page, :per_page => perPage
Am I doing something wrong with the search?

I think there is some additional configuration necessary to search with wildcards, as explained in the documentation.

Related

Tire + Will_Paginate not taking Tire.options

I'm trying to integrate Tire into my site and I'm having difficulty with pagination. I've tried paginating the results outside of the context of Tire and will_paginate is working on that Array. However, when I try will_paginate within the context of Tire I'm having one large problem.
Will_Paginate will display the correct number of pages with consideration of :per_page but when I click on that page the results are not loaded, rather they are the same as on the first page. The page number is highlighted in the will_paginate navigation.
#results.inspect yields this:
#<Tire::Search::Search:0x007f88ab9153d0 #indices=["deja-set-development"], #types=[], #options={:load=>true, :page=>1, :per_page=>2}, #path="/deja-set-development/_search", #query=#<Tire::Search::Query:0x007f88ab915088 #value={:query_string=>{:query=>"oh"}}>, #facets={"type"=>{:terms=>{:field=>:_type, :size=>10, :all_terms=>false}}}>
Here is where I call will_paginate:
= will_paginate #search_results.results, params
Here is where I iterate through the results
#search_results.results.each
Does anyone have any thoughts?
Edit ---
I'm not sure what is going on, but I did this and it is working.
#search_results = #search_results.paginate(:page => params[:page], :per_page => 5)
Please see the integration test in Tire, and make sure you're passing all options properly.
So to clarify, I've attached my github correspondence with #karmi here.
https://github.com/karmi/tire/issues/627#issuecomment-13449368
I was using Tire.search as opposed to searching by model. As #karmi notes, at the moment :per_page and :page are not supported with Tire.
Here is how I solved this:
#search_results = Tire.search [:index1, :index2, :index3], :load => true, :from => from, :size => size do
query do
string q, :default_operator => 'AND', :fields => [:name1, :name2]
end
end
I ended up having to spin my own small pagination system to increment 'size' and 'from'. Here's the elasticsearch link on the topic.
http://www.elasticsearch.org/guide/reference/api/search/from-size.html
You're able to still access
= #search_results.results.total_entries/next_page/previous_page
which helps with pagination.
Thank you again #karmi.

Should I escape the condition for my rails find?

OK so i have this condition
conditions = {}
conditions[:state] = params[:state] if params[:state]
#apps = current.apps.paginate(:include => :user, :conditions => conditions, :order => "users.first_name, users.last_name")
and my routes file has
map.apps_wishlist '/apps/wishlist', :controller => 'apps', :action => 'index', :state => 'wishlist'
Since i am sending in the string should i escape the sql somehow for security reasons and if so how is the best way to do that
Someone suggested they think the params[:state] should be escaped when put into the SQL query for security reasons
They don't know what they're talking about. As long as you do either
SomeModel.where(:conditions => {:state => params[:state]})
#or
SomeModel.find(:conditions => {:state => params[:state]})
#or
SomeModel.where("state = ?", params[:state])
ActiveRecord will handle all the escaping itself, so the code you've given above is fine/ The only case it doesn't cover is if you were building up the sql entirely on your own, i.e. don't do
SomeModel.where("state = #{params[:state]}")
If you're using find_by_sql, execute and so on then you are also responsible for escaping. Don't just take my word for it though - try it! You might also want to look at the rails security guide

What's a better way to use helper methods with will_paginate?

I'm using the latest version of will_paginate with rails 3. I'd like to use out_of_bounds? to set the current page to the last page if the page parameter is higher than the last page. I found a way to do it like this:
people = People.all.paginate :page => params[:page], :per_page => 20
#people = people.paginate(:page => (people.out_of_bounds? ? people.total_pages : params[:page))
The problem with this is that I have to call paginate twice. The first time to create a WillPaginate::Collection in order to use the out_of_bounds? and total_pages methods, and the second time to actually set the current page. I also need to do this with more actions so it's getting kinda out of hand. I can't use a before_filter or an after_filter, either (I don't think?). Is there a better way to achieve this?
I don't know the best solution, but at least you can do the check before the second call to the method (I suppose that in most of the cases, the page parameter will be ok, so you wont have to call paginate twice):
people = People.all.paginate :page => params[:page], :per_page => 20
#people = people.out_of_bounds? ? people.paginate(:page => people.total_pages) : people

mini fb posting to friend's wall using graph api

Hi I have read all the other post relating to this but I think I am missing something fundamental. I am using mini_fb in my ruby on rails application for handling the facebook api. I have the following code:
current_user.session.post('me', :type => :feed, :params => {:name => "name",
:to => "{\"data\":[{\"name\":\"#{friend.facebook_name}\",\"id\":\"#{friend.facebook_id}\"}]}",
:link => url_of_app,
:description => "desc",
:actions => "{\"name\": \"action name\", \"link\": \"url\"}"})
The above posts to the current user's wall with or without the "to" parameter set. Everything works, except for the "to" parameter. I have read the graph post to wall over and over again and I can't figure out what is wrong with this code of mine. I would really appreciate it if someone could point out my mistake.
I've never used ruby's version, but probably the problem is in the first parameter. You are targeting 'me' feed, while should be targeting your friends feed. Try fetching your friend id and doing something like
current_user.session.post(friend.facebook_id, :type => :feed, :params => ...)
Wow, mini_fb looks so verbose :)
Telémako is right, you need to use your friends id. I give you another alternative for more nice code. Use Koala.
https://github.com/arsduo/koala/wiki/Graph-API
#graph.put_wall_post("explodingdog!", {:name => "i love loving you", :link => "http://www.explodingdog.com/title/ilovelovingyou.html"}, "tmiley")
=> {"id"=>"83901496_520908898070"}
I use it in my projects and works very well.

How do you use will_paginate?

Say if I have a set of objects contained in #set. Each of these objects has a description method which will return some text that I want to display on individual pages. How do I use will_paginate to paginate this?
The examples I've seen so far such as:
#articles = Article.paginate :page => params[:page]
look like they are referring to all Article objects.
Assuming your model is called SomeObject, the will_paginate syntax is similar to that of ActiveRecord's .find():
#set = SomeObject.paginate(:page => params[:page],
:per_page => 20,
:order => 'created_at DESC',
:conditions => { :foo => 'bar' })
Check the documentation for more.
To do do the call in active record you just use .paginate instead of .find and pass the necessary arguments, the gem is pretty well documented on how to use it. Installation docs, and usage examples are in the Readme. The examples might not be using your #set but you use it the same way.
Edit: Apparently those links were wrong, The github repo for will_paginate has that wiki and readme file. Do a search on github for it as I seem to be having epic link fail at the moment.

Resources