Sunspot Solr search parameter "with" not working with strings - ruby-on-rails

I'm developing a Ruby on Rails application using Sunspot Solr as an indexer.
The thing is i try to use the parameter of the search with :, using strings and it doesn't seem to work. If i pass an int it works fine. Anyone knows how to do it with strings (if it's possible)?
An example of the search i want to do is:
#search = Sunspot.search(Record) do
fulltext params[:query]
with :checked, "Checked"
end
:checked is an attribute of the table Record and it validates if a record is checked so it can't be edited.
PD: I'm doing it this way because MySQL doesn't accept booleans.

Figured it out!
It turns out strings are a little more delicate, so you have to use:
with(:checked).equal_to("any_value")
For more information see:
https://github.com/sunspot/sunspot/wiki/Scoping-by-attribute-fields

Related

How to search for wild card in Rails

Im trying to search my User model for all Users that start with any integer, I have code for individual letters and it works, but Im having trouble getting it working with a wild card. Right now I have this code:
in my view:
<%= link_to '#', users_charlist_path(:char => '[0123456789]' %>
and in my controller I have:
def charlist
#a = User.where('goal like ?', "#{params[:char]}%").to_a
end
how ever, '[0123456789]', doesnt seem to work as it does not return anythign to me even though I have users whose names begin with an integer. how do i do this?
The where method is a part of ActiveRecord which maps the objects to the database. So how you can query the database with a regex depends on which db you are using not on ruby. You need to look up the regex functions of the database your using. For mysql you can find them here:
http://dev.mysql.com/doc/refman/5.1/de/regexp.html
An alternative is to select all objects an use the select method to filter the results that match your needs. That method is documented here:
http://www.ruby-doc.org/core-2.1.0/Array.html#method-i-select
On big amounts of data I whould suggest to use the database even if that means your application isnt 100% portable between different database systems.

Sunspot and Solr: Google-like "site:" in search

I'm fairly new to Sunspot and Solr.
I'd like to implement something similar to the Google site: phrase in searches, so that when I search for
something
I get all records related to "something". However, if they search
something site:example.com
It only displays results where the site attribute is example.com.
At the moment I've got:
searchable do
text :full_text
text :title, :boost => 5
text :excerpt
end
Do I need to index the site attribute? How would I implement the above idea?
No. Just make sure you gsub your params[:query] in your controller. Extract the value next to site: and add a where close to match the site. You'll be left with something.

Mongoid datatype retrieval

Is it possible to quickly retrieve the datatype of a given Mongoid field?
Something like: FieldName.type?
Thanks
I guess, this is not just mongo specific. You can use the following code to retrieve field type of a field irrespective of the database underneath.
User.first.name.class
=> String
User.first.up_votes.class
=> Fixnum
I am still on mongoid 2.4 so I am not sure if this will work on 3.0 too:
User.fields["field_name"].options[:type]

Difficult search in ruby on rails

How can I search first in model (i know how to do this). And then search in this array for more concretence? As you see:
#articles = Article.find(:all, :conditions => { :ART_ID => #search.map(&:ARL_ART_ID)})
#a = #articles.find_all{|item| item.ART_ARTICLE_NR == search.upcase }
First i search in model, but thanks to my db) it have many wrong results, so i must to clarify my array. But there how to search like sql:
like % %
Now it search very strong: if i search AC451, it's good, but if AC45 or C451 it's nothing fetches. How to say him so that before and after
search
could be everything?
Like this, maybe?
item.ART_ARTICLE_NR.include?(search.upcase)
You are asking for trouble by not following rails naming conventions an using upper case column names. That said, the rails3 way to do it is probably:
#articles = Article.where(:ART_ID => #search.map(&:ARL_ART_ID)).where('ART_ARTICLE_NR LIKE', "%#{search.upcase}%")
Without knowing what #search is, it's hard to be sure. But you should read up on the active record guide on the rails 3 query format.
It's maybe not straigtforward answer, but have you considered using ransack gem?

Sunspot Solr matching a single object

I use the Sunspot gem in my RoR app to do searches on the Post model, and it works great for that. However, I need to use it's matching algorithm against a single post object.
For example, I can search all Posts like this:
Sunspot.search Post do
...
end
But, I need to do the search against a single post object, like so:
Sunspot.search #post do
...
end
Is this possible?
I want to use the same matching algorithm on a single post object to check whether it matches or it doesn't.
I don't think that's possible. It's Solr and Lucene, not Sunspot, who have all the algorithms that determine if something is a match or not for any given query. Solr clients merely construct the query parameters and feed them to Solr, then parse back Solr results.
I'm not exactly sure how to do this with sunspot, but one thing you could try is to query RSolr directly, pass in the ID of the model you want to check in the :fq, and see if it returns a result or not. Should return pretty fast because of the filter query:
solr = RSolr.connect(:url => Sunspot.session.config.solr.url)
solr.select :q => solr_query, :fq => ['type:Post', "id:#{#post.id}"]

Resources