Sha256: 43ce729108114d9bba274530bb29ae8c926a82d226bcec313b0bb49917106866

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

require 'peoplefinder'

module Peoplefinder::Concerns::Searchable
  extend ActiveSupport::Concern

  included do
    include Elasticsearch::Model
    include Elasticsearch::Model::Callbacks

    # Force a full re-index after update, the ActiveModel::Dirty tracking
    # doesn't detect changes to community_name
    after_commit -> { __elasticsearch__.index_document }, on: :update

    index_name [Rails.env, model_name.collection.gsub(/\//, '-')].join('_')

    def self.delete_indexes
      __elasticsearch__.delete_index! index: Peoplefinder::Person.index_name
    end

    def self.fuzzy_search(query)
      search(
        size: 100,
        query: {
          fuzzy_like_this: {
            fields: [
              :name, :tags, :description, :location,
              :role_and_group, :community_name],
            like_text: query, prefix_length: 3, ignore_tf: true
          }
        }
      )
    end

    def as_indexed_json(_options = {})
      as_json(
        only: [:tags, :description, :location],
        methods: [:name, :role_and_group, :community_name]
      )
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
peoplefinder-0.1.1 app/models/peoplefinder/concerns/searchable.rb
peoplefinder-0.1.0 app/models/peoplefinder/concerns/searchable.rb