paper_type facets

This commit is contained in:
Lars Henrik Mai 2015-06-13 21:18:55 +02:00 committed by Andreas Haller
parent 5b677ab3ca
commit f709c399be
3 changed files with 32 additions and 1 deletions

View file

@ -1,5 +1,7 @@
class SearchController < ApplicationController
def index
@papers = Paper.search(params[:q]).page(params[:page]).results
@response = Paper.search(params[:q])
@papers = @response.page(params[:page]).results
@paper_type_facets = @response.response['aggregations']['paper_types']['buckets'].map {|h| [ h['key'], h['doc_count']] }
end
end

View file

@ -9,6 +9,15 @@ class Paper < ActiveRecord::Base
validates_presence_of :published_at, allow_nil: true
validates :url, uniqueness: true
settings index: { number_of_shards: 1 } do
mappings dynamic: false do
indexes :name, type: :string
indexes :content, type: :string
indexes :resolution, type: :string
indexes :paper_type, type: :string, index: :not_analyzed
end
end
class << self
def import_from_json(json_string)
old_count = count
@ -35,6 +44,7 @@ class Paper < ActiveRecord::Base
# and https://github.com/elastic/elasticsearch-rails/tree/master/elasticsearch-rails/lib/rails/templates
def search(q)
@search_definition = Elasticsearch::DSL::Search.search do
query do
unless q.blank?
multi_match do
@ -45,10 +55,22 @@ class Paper < ActiveRecord::Base
match_all
end
end
aggregation :paper_types do
terms do
field 'paper_type'
end
end
end
puts @search_definition.to_hash
__elasticsearch__.search(@search_definition)
end
def reset_index!
__elasticsearch__.create_index! force: true
all.each {|p| p.__elasticsearch__.index_document }
end
end
end

View file

@ -1,6 +1,13 @@
div
= render 'search/form'
.row
.small-4.columns
ul
- @paper_type_facets.each do |paper_type, count|
li.facet
= "#{paper_type} (#{count})"
.clearfix
- if params[:q].present?
.left