2015-06-22 22:08:45 +02:00
|
|
|
SearchFacet = Struct.new("SearchFacet", :term, :count) do
|
|
|
|
def term_with_count
|
|
|
|
"#{term} (#{count})"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-11-19 17:07:05 +01:00
|
|
|
class SearchController < ApplicationController
|
|
|
|
def index
|
2016-02-17 21:04:53 +01:00
|
|
|
@search_definition = PaperSearch.new(search_params)
|
2016-03-26 16:51:00 +01:00
|
|
|
@search_definition.sort_by ||= "date"
|
2015-06-22 20:38:24 +02:00
|
|
|
|
2015-10-10 09:48:18 +02:00
|
|
|
execute_search
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
@search_definition = PaperSearch.find params[:id]
|
|
|
|
execute_search
|
|
|
|
render action: "index"
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def execute_search
|
|
|
|
@response = Paper.search(@search_definition.to_definition)
|
2015-06-13 21:18:55 +02:00
|
|
|
@papers = @response.page(params[:page]).results
|
2016-12-23 00:17:18 +01:00
|
|
|
@sub = Hash.new
|
|
|
|
@papers.each do |paper|
|
2017-01-01 13:41:28 +01:00
|
|
|
unless paper.reference.nil? && paper.reference.contains("-")
|
|
|
|
segments = paper.reference.split("-")
|
|
|
|
id = (paper.reference.start_with?("VI-") && segments.count > 2 ?
|
|
|
|
segments[2] : segments[1])
|
|
|
|
escaped_chars = Regexp.escape('\\+-*:()[]{}&!?^|\/')
|
|
|
|
sanitized_id = id.gsub(/([#{escaped_chars}])/, '\\\\\1')
|
|
|
|
['AND', 'OR', 'NOT'].each do |reserved|
|
|
|
|
escaped_reserved = reserved.split('').map { |c| "\\#{c}" }.join('')
|
|
|
|
sanitized_id = sanitized_id.gsub('/\s*\b(#{reserved.upcase})\b\s*/',
|
|
|
|
" #{escaped_reserved} ")
|
2016-12-23 00:17:18 +01:00
|
|
|
end
|
2017-01-01 13:41:28 +01:00
|
|
|
@sub_search_definition = Elasticsearch::DSL::Search.search do
|
|
|
|
query do
|
|
|
|
query_string do
|
|
|
|
query "*" + sanitized_id + "*"
|
|
|
|
fields ["reference"]
|
|
|
|
end
|
|
|
|
end
|
2016-12-23 00:17:18 +01:00
|
|
|
|
2017-01-01 13:41:28 +01:00
|
|
|
sort do
|
|
|
|
by :published_at, order: 'asc'
|
|
|
|
by :reference, order: 'asc'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@sub_papers = Paper.search(@sub_search_definition)
|
|
|
|
@sub[paper.reference] = @sub_papers
|
2016-12-23 00:17:18 +01:00
|
|
|
end
|
2017-01-01 13:41:28 +01:00
|
|
|
end
|
2015-06-22 21:11:51 +02:00
|
|
|
@paper_type_facets = extract_facets('paper_types')
|
|
|
|
@originator_facets = extract_facets('originators')
|
2015-06-01 23:53:45 +02:00
|
|
|
end
|
2015-06-22 21:11:51 +02:00
|
|
|
|
2015-10-10 09:48:18 +02:00
|
|
|
def search_params
|
2016-02-17 21:04:53 +01:00
|
|
|
params.fetch(:paper_search, {}).permit(:query, :paper_type, :originator, :sort_by)
|
2015-10-10 09:48:18 +02:00
|
|
|
end
|
2015-06-22 21:11:51 +02:00
|
|
|
|
|
|
|
def extract_facets(name)
|
|
|
|
@response.
|
2015-06-22 22:08:45 +02:00
|
|
|
response['aggregations'][name.to_s][name.to_s]['buckets'].
|
|
|
|
map {|m| SearchFacet.new(m['key'], m['doc_count'])}
|
2015-06-22 21:11:51 +02:00
|
|
|
end
|
|
|
|
|
2014-11-19 17:07:05 +01:00
|
|
|
end
|