Can Sunspot Search Inside Array

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 this

class 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

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

Is that what you were looking for?

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



Leave a reply



Submit