Using sunspot_solr search array of values
if your question and answer has association like
class Question < ActiveRecord::Base
has_many :answers
end
class Answer < ActiveRecord::Base
belongs_to :question
end
then you can add searchable to your questions model like thisclass Question < ActiveRecord::Base
searchable do
text :title, :body
text :answers do
answers.map { |answer| answer.body }
end
integer :questions_ids, :multiple => true
end
// your another methods
end
And in your index action @search = Answer.search do
with(:questions_ids, @question_ids)
paginate :page => 1, :per_page => Answer.count
end
return question_id
I think it will help you. How do you perform a sunspot search on a specific field in a has-many :through relationship
The easiest way to do what you want would be to start by separating out the different types of dynamic fields that you want (string, integer, boolean), maybe by adding an attribute_type: 'string', attribute_type: 'numerical', etc. Then you can use Sunspot's dynamic fields to search different types of dynamic data:
searchable do
dynamic_string :string_attributes do
user_custom_string_attributes.inject({}) do |hash, uca|
hash.merge(uca.custom_attribute.name.to_sym => uca.value)
end
end
dynamic_integer :numerical_attributes do
user_custom_numerical_attributes.inject({}) do |hash, nuca|
hash.merge(nuca.custom_attribute.name.to_sym => nuca.value.to_i) # just making sure it's an integer!
end
end
end
# Separate out the different types of attributes for different dynamic blocks
# via methods like the one below.
def user_custom_numerical_attributes
user_custom_attributes.where(attribute_type: 'numerical')
end
def user_custom_string_attributes
user_custom_attributes.where(attribute_type: 'string')
end
This will give you searchable 'string_attributes' objects that look like {:has_children => 'no'} or 'numerical_attributes' like {:number_of_children => 2}.Then you can search with a block like so:
User.search do
dynamic :string_attributes do
with(:has_children, 'no')
end
dynamic :numerical_attributes do
with(:number_of_children).greater_than(0)
end
end
You can create dynamic fields for pretty much any data type except text; for that you'll have to use string instead. And once you've set up your dynamic fields, inside the dynamic :field_name do
block you can use any of the non-text search methods demonstrated in the examples in the Sunspot readme. sunspot-solr: How to exclude search results that include any given word?
From https://github.com/sunspot/sunspot/wiki/Fulltext-search
Is that what you were looking for?A small subset of the normal boolean query syntax is parsed: in particular, well-matched quotation marks can be used to demarcate phrases, and the + and - operators can be used to require or exclude words respectively
How we can search any word is searchable on sunspot solr on rails project
I got output just search key is also accepts array of words, so simply we can get this by below code
def custom_search
test = Model.search do
fulltext key do
fields(:title)
query_phrase_slop 1
end
without(:field_disabled, true)
facet(:obj_type)
end
if test.hits.size==0 #check the condition for no result matched for full text and passing every word
test = Model.search do
fulltext key.split(' ') do # key.split(' ') code make total sentence into array of words
fields(:title)
query_phrase_slop 1
end
without(:field_disabled, true)
facet(:obj_type)
end
end
return test
end
Sunspot indexing and searching tags returns everything
Ok we "solved" this ourselves and i'll report it back here in case anyone comes looking with the same question.
Somehow Sunspot doesn't like @tags_array in our search declaration, after some testing any @variable will not work. As soon as we changed it to:
tags_array= params[:tags].split(/ /)
Person.search() do
with(:project_tags).any_of(tags_array)
end
it worked.Cheers,
Erwin
Related Topics
Accessing Microsoft Exchange Server from Ruby
Using Ruby with Mechanize to Log into a Website
Undefined Method 'Merge' for '####':String <%= Form_For %> Helper
Ruby Rspec. Get List of All Test
How to Remove Backslashes from a JSON String
Rails3 Scope for Count of Children in Has_Many Relationship
What's an Example of Ruby Code That's "Too Clever"
Sql Like Operator in Ruby on Rails
Rails 3, Http Extensions (Webdav) and Rack App Mounting
How to Send an Image on The Web in an Xmpp (Jabber) Message
Sinatra Not Persisting Session with Redirect on Chrome
Rails Redirecting Invalid Route to Root
How to Declare a Two-Dimensional Array in Ruby
How Do Erlang Actors Differ from Oop Objects
Puma and Nginx 502 Bad Gateway Error (Ubuntu Server 14.04)
Resizing an Image with Mini_Magick
Running Capybara Without Rack Produces Errors When Using Url Parameters