Skip to content

Instantly share code, notes, and snippets.

@matteodepalo
Created January 5, 2013 15:14
Show Gist options
  • Select an option

  • Save matteodepalo/4462045 to your computer and use it in GitHub Desktop.

Select an option

Save matteodepalo/4462045 to your computer and use it in GitHub Desktop.
question elasticsearch mapping
include Tire::Model::Search
include Tire::Model::Callbacks
settings :analysis => {
:filter => {
:ngram_filter => {
:type => 'edgeNGram',
:min_gram => 1,
:max_gram => 10,
:side => 'front'
},
:multi_stop => {
:type => 'stop',
:stopwords => ['_english_', '_italian_']
}
},
:analyzer => {
:full_analyzer => {
:tokenizer => 'standard',
:filter => ['lowercase', 'standard', 'multi_stop'],
:type => 'custom'
},
:partial_analyzer => {
:tokenizer => 'standard',
:filter => ['standard', 'lowercase', 'ngram_filter', 'multi_stop'],
:type => 'custom',
}
}
} do
mapping do
indexes :text, :type => 'multi_field', :fields => {
'text' => { :type => 'string', :analyzer => 'full_analyzer' },
'partial' => { :type => 'string', :search_analyzer => 'full_analyzer', :index_analyzer => 'partial_analyzer' }
}
indexes :detail, :type => 'multi_field', :fields => {
'detail' => { :type => 'string', :analyzer => 'full_analyzer' },
'partial' => { :type => 'string', :search_analyzer => 'full_analyzer', :index_analyzer => 'partial_analyzer' }
}
indexes :answer, :type => 'multi_field', :fields => {
'answer' => { :type => 'string', :analyzer => 'full_analyzer' },
'partial' => { :type => 'string', :search_analyzer => 'full_analyzer', :index_analyzer => 'partial_analyzer' }
}
indexes :topic, :index => :not_analyzed
indexes :username
indexes :widget_id, :index => :not_analyzed
indexes :rating, :type => 'integer', :index => :not_analyzed
indexes :official, :type => 'boolean', :index => :not_analyzed
indexes :created_at, :type => 'date', :index => :not_analyzed
indexes :hidden, :type => 'boolean', :index => :not_analyzed
indexes :rating, :type => 'integer', :index => :not_analyzed
end
end
def self.search(params = {})
tire.search(:load => true, :page => params[:page]) do
query do
boolean do
if params[:query].present?
should { string "text.partial:#{params[:query]}" }
should { string "detail.partial:#{params[:query]}" }
should { string "answer.partial:#{params[:query]}" }
should { string "text:#{params[:query]}", :boost => 10 }
should { string "detail:#{params[:query]}", :boost => 10 }
should { string "answer:#{params[:query]}", :boost => 10 }
should { string "username:#{params[:query]}"}
end
must { term :hidden, false }
must { term :topic, params[:topic] } if params[:topic].present?
must { term :widget_id, params[:widget_id] } if params[:widget_id].present?
end
end
end
end
def to_indexed_json
self.as_json.merge(
:topic => topic.try(:name),
:answer => answers.last.try(:text),
:username => user.try(:name),
:official => certified?
).to_json
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment