Exclude Draft Articles from Solr Index with Sunspot

Exclude draft articles from Solr index with Sunspot

Be sure to index the published status.

class Article < ActiveRecord::Base
searchable do
text :title
text :body
boolean :is_published, :using => :published?
end
end

Then add a filter to your query

Sunspot.search(Article) do |search|
search.with(:is_published, true)
# ...
end

Rails - Sunspot conditional model indexing

Answered here

Exclude draft articles from Solr index with Sunspot

Search using Sunspot:solr

it will be easier to answer if you put your models in question but in general way if you have has_many relationship than you should index nested model

#variant model

searchable do

integer :product_id
time :check_in
time :check_out

end

if you need to index something from parent has_many model you can use :multiple=>true option in this way

#product model

def variant_ids
variants.collect(&:id)
end

searchable do

integer :variant_ids, :multiple=>true
...

end

How to reindex only some objects in Sunspot Solr

I have found the answer on https://github.com/sunspot/sunspot#reindexing-objects

Whenever an object is saved, it is automatically reindexed as part of the save callbacks. So all that was needed was to add all the objects that needed reindexing to an array and then loop through the array, calling save on each object. This successfully updated the required objects in the index.

Filter a result from Sunspot Solr

It ended up being:

facet :field_PropertySubType, exclude: [with(:field_PropertySubType, "Single Family")]

The "with" just needed brackets around it.

Exclude certain documents from all search results in Solr?

An another answer to your question will be to add a new field and storing the "status" (to be displayed or should not) there and use that in Solr filter query.
E.g. Have a string field name as "remove" and for the documents which should not be shown in result, add "remove" field value as "true".
So you can query like
"q=abc&fq=-remove:true"

Note:

  1. This have a multiple usage in long run. If the document needs to be shown in another queries, you can just remove the fq filter.
  2. You can add any number of documents to have the field "remove=true" and there wont be any change in search query. The document will not be displayed in result.
  3. You can add this filter in "solrconfig" also since there wont be a need to change it everytime.

Sunspot. Solr not searching any changes (create/update)

From the readme:

If you make a change to the object's "schema" (code in the searchable block), you must reindex all objects so the changes are reflected in Solr:

bundle exec rake sunspot:solr:reindex

Also, according to this and this, Sunspot.commit which updates the Solr index is not automatically called if you're updating a model outside of a Rails request (for example, from the Rails console). You can manually call Sunspot.commit from the console to commit the changes to Solr.

Exectuting bundle exec rake sunspot:solr:reindex will also commit your changes.

solr, sunspot, bad request, illegal character

Put the following in an initializer to automatically clean sunspot calls of any UTF8 control characters:

# config/initializers/sunspot.rb
module Sunspot
#
# DataExtractors present an internal API for the indexer to use to extract
# field values from models for indexing. They must implement the #value_for
# method, which takes an object and returns the value extracted from it.
#
module DataExtractor #:nodoc: all
#
# AttributeExtractors extract data by simply calling a method on the block.
#
class AttributeExtractor
def initialize(attribute_name)
@attribute_name = attribute_name
end

def value_for(object)
Filter.new( object.send(@attribute_name) ).value
end
end

#
# BlockExtractors extract data by evaluating a block in the context of the
# object instance, or if the block takes an argument, by passing the object
# as the argument to the block. Either way, the return value of the block is
# the value returned by the extractor.
#
class BlockExtractor
def initialize(&block)
@block = block
end

def value_for(object)
Filter.new( Util.instance_eval_or_call(object, &@block) ).value
end
end

#
# Constant data extractors simply return the same value for every object.
#
class Constant
def initialize(value)
@value = value
end

def value_for(object)
Filter.new(@value).value
end
end

#
# A Filter to allow easy value cleaning
#
class Filter
def initialize(value)
@value = value
end
def value
strip_control_characters @value
end
def strip_control_characters(value)
return value unless value.is_a? String

value.chars.inject("") do |str, char|
unless char.ascii_only? and (char.ord < 32 or char.ord == 127)
str << char
end
str
end

end
end

end
end

Source (Sunspot Github Issues): Sunspot Solr Reindexing failing due to illegal characters



Related Topics



Leave a reply



Submit