Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I would like a review regarding the following code in which I index and search the City model. Currently both solr (with sunspot gem) and elaticsearch (with the tire gem) are show. I am migrating from solr to ES and want to make sure I am not making any major mistakes.

I am new to ES.

class City < ActiveRecord::Base
  include Tire::Model::Search
  include Tire::Model::Callbacks

  class << self
    def search(params)
      tire.search(load: false, page: params[:page], per_page: (params[:per_page] || 50)) do
        query do
          boolean do
            should { string 'has_seo_doctors:true' }
          end
          term :state_id, params[:state_id]
        end
        sort { by :name_sortable, "asc" }
      end
    end

    def search_total
      Tire.search('cities', search_type: 'count') {}.results.total
    end
  end

  # elasticsearch
  mapping do
    indexes :name, type: 'multi_field',  stored: 'yes', fields: {
      name:          { type: 'string', analyzer: 'snowball' },
      name_sortable: { type: 'string', index: :not_analyzed }
    }
    indexes :id,              type: 'integer'
    indexes :seo_name,        type: 'string',  stored: 'yes'
    indexes :state_id,        type: 'integer', stored: 'yes', as: 'state.try(:id)'
    indexes :has_seo_doctors, type: 'boolean', stored: 'yes', as: 'has_seo_doctors?'
    indexes :state_seo_name,  type: 'string',  stored: 'yes', as: 'state.try(:seo_name)'
  end


  # solr (sunspot) indexing
  searchable(auto_index: true, auto_remove: true, include: :state) do
    boolean :has_seo_doctors, stored: true do
      has_seo_doctors?
    end
    string :id
    string :name, stored: true
    string :seo_name, stored: true
    string :state_id, stored: true do
      state.try(:id)
    end
    string :state_seo_name, stored: true do
      state.try(:seo_name)
    end
  end
  handle_asynchronously :solr_index unless Rails.env.test?

end

Performing a search is as easy as:

City.search({ state_id: State.first.id, page: 1 })

We have an admin dashboard which relies on the total indexes number of cities, so I have created the following convenience method:

City.search_total

A few comments:

  • I don't exactly understand when to use to_indexed_json
  • If 'as:' is the proper way of handling custom indexed methods

Thank you

share|improve this question
    
Is this Ruby? Please add a language tag, it will give your question better visibility to those who can answer it. –  seand Dec 19 '12 at 3:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.