samedi 24 mars 2018

Rails 4 - Elasticsearch and searchkick NOT working as expected

I have a Rails4 app using elasticsearch and searchkick for a sitewide search page. i have configured the models and its associations using searchkick search_data, but its not working as per my requirement where user can search all venues by location,name,capacity,event(marriage,engagements etc) and food_type(veg/non-veg).Sharing the code below.

############### models/venue.rb ####################################


     ##columns of venue.rb
     ##=> Venue(id: integer, name: string, description: text,active: boolean, announcements_count: integer, comments_count: integer, pictures_count: integer, videos_count: integer, created_at: datetime, updated_at: datetime, capacity_in_persons: string, workflow_state: string) 

     ### associations 
     has_many :event_categories
     has_many :event_types, through: :event_categories
     has_one :address, :as=> :addressable, :dependent => :destroy   

     ###elasticsearch config starts here
    searchkick word_middle:["name^10", :slug,  :capacity_in_persons], locations: ["location"]

  def search_data
    {
      name: name, analyzer: 'english', #: :word_start, misspellings: false},
      capacity_in_persons: capacity_in_persons,
      food_type: food_type,
      slug: slug,
      ##has many event types
      event_type_name: event_types.map(&:name),
      ratings: ratings.map(&:stars),
      ##location: [self.address.latitude, self.address.longitude],
      location: [self.address.latitude, self.address.longitude],
      picture_url: pictures.select{|p| p == pictures.last}.map do |i|{

        original:  i.original_url 

        }
      end
    }
  end

##to eager load other associations
scope :search_import, -> { includes(:address, :event_types, :pricing_details, :ratings) }
after_save :reindex if try(:address).present?



  ###### controller action ##########
          @query = []
          @query << params[:venue_name] if params[:venue_name].present? 
          @query << params[:address] if params[:address].present?
          @query << params[:venue_capacity_in_persons] if params[:venue_capacity_in_persons].present?
          @query << params[:food_type] if params[:food_type].present?
          @query << params[:event_name] if params[:event_name].present?
          @query = @query.flatten.compact
          logger.tagged("SITE WIDE SEARCH"){ logger.info "**************SEARCH QUERY***#{@query}******************" }   
          ##TODO-add constraints to handle range for capacity in persons
          ###@halls = Hall.get_completed_halls_only.paginate(:page => params[:page]).search(@query).results
          ##@halls =  Hall.get_completed_halls_only.search(@query)
          @halls =  Hall.get_completed_halls_only.search params[:address],
                    where: {
                      capacity_in_persons: {lte: params[:venue_capacity_in_persons]},
                      food_type: params[:food_type],
                      event_type_name: params[:event_name]
                    }

Moreover, i have added changes to my address.rb to find venue nearby and i think it working fine but any suggestions are welcomed.address.rb is polymorphic and has address_1 attribute to store address coming from google dropdown along with geocoding from geocoder gem.

####################### models/address.rb #######################
###address belongs_to venue
searchkick locations: [:address_1] 

  ## call Address.reindex if this method is changed
  def search_data
    attributes.except("id").merge(
      address_1: {lat: latitude, lon: longitude},
      city: city,
      state: state,
      zipcode: zipcode  ##unless addressable_type == "Hall"
      )

  end

Kindly help me out as what changes do i need to make and where i am going wrong.

Thanks in advance

Aucun commentaire:

Enregistrer un commentaire