Skip to content

Instantly share code, notes, and snippets.

@mribica
Created February 20, 2013 11:18
Show Gist options
  • Save mribica/4994876 to your computer and use it in GitHub Desktop.
Save mribica/4994876 to your computer and use it in GitHub Desktop.
include Tire::Model::Search
index_name INDEX_NAME
# Update index only if user is active
after_save do
tire.update_index if self.active == true
end
# Update index only if user is active
after_destroy do
tire.update_index if self.active == true
end
settings analysis: {
filter: {
ngram_filter: {
type: "edgeNGram",
min_gram: 2,
max_gram: 6,
side: 'front'
}
},
analyzer: {
ngram_analyzer: {
tokenizer: "lowercase",
filter: ["ngram_filter"],
type: "custom"
}
}
} do
# Mapping
mapping do
indexes :id, type: 'integer'
indexes :name, type: 'string', analyzer: 'ngram_analyzer'
indexes :active, type: 'boolean'
indexes :avatar_small, type: 'string'
end
end
def to_indexed_json
{ id: self.id, name: self.name, active: self.active, avatar_small: self.avatar_url(:small)}.to_json
end
def self.search(query)
tire.search do
query {string "name:#{query}"} if query.present?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment