1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
<%#
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
%>
<div class="col-md-12">
<h1 class="text-right"><%= link_to 'Search New York Times articles', root_path %></h1>
<%= form_tag search_path, method: 'get', role: 'search' do %>
<div class="input-group">
<%= text_field_tag :q, params[:q], class: 'form-control', placeholder: 'Search...' %>
<span class="input-group-btn">
<button type="submit" class="btn btn-default">
<span class="glyphicon glyphicon-search"></span>
</button>
</span>
</div>
<div id="form-options" class="clearfix">
<div class="btn-group pull-left">
<label class="checkbox-inline">
<%= check_box_tag 'comments', 'y', params[:comments] == 'y', onclick: "$(this).closest('form').submit()" %>
Search in comments?
</label>
<% params.slice(:a, :c, :s).each do |name, value| %>
<%= hidden_field_tag name, value %>
<% end %>
</div>
<div class="btn-group pull-right">
<p style="float: left; margin: 0.1em 0 0 0"><small>Displaying <%= (params[:page] || 1).to_i.ordinalize %> page with <%= @articles.size %> articles
of <strong>total <%= @articles.total %></strong></small></p>
<button class="btn btn-default btn-xs dropdown-toggle" type="button" data-toggle="dropdown" style="margin-left: 0.5em">
<% sort = case
when params[:s] then params[:s]
when params[:q].blank? then 'published_on'
else 'relevancy'
end
%>
sorted by <%= sort.humanize.downcase %> <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
<li><%= link_to "Sort by published on", search_path(params.permit(:q, :a, :c, :s, :w, :comments).merge(s: 'published_on')), class: 'btn-xs' %></li>
<li><%= link_to "Sort by relevancy", search_path(params.permit(:q, :a, :c, :s, :w, :comments).merge(s: nil)), class: 'btn-xs' unless params[:q].blank? %></li>
</ul>
</div>
</div>
<% end %>
<hr>
</div>
<% if @articles.size < 1 && @articles.response.suggestions.present? %>
<div class="col-md-12">
<p class="alert alert-warning">
No documents have been found.
<% if @articles.response.suggestions['suggest_title'].present? || @articles.response.suggestions['suggest_body'].present? %>
Maybe you mean
<%= @articles.response.suggestions.map { |k,v| v.first['options'] }.flatten.map {|v| v['text']}.uniq.map do |term|
link_to term, search_path(params.permit(:q, :a, :c, :s, :w, :comments).merge q: term)
end.to_sentence(last_word_connector: ' or ').html_safe %>?
<% end %>
</p>
</div>
<% end %>
<div id="facets" class="col-md-3">
<% unless @articles.size < 1 %>
<div class="categories panel panel-default">
<p class="panel-heading"><%= link_to 'All Sections →'.html_safe, search_path(params.permit(:q, :a, :c, :s, :w, :comments).merge(c: nil))%></p>
<div class="list-group">
<% @articles.response.response['aggregations']['categories']['categories']['buckets'].each do |c| %>
<%=
link_to search_path(params.permit(:q, :a, :c, :s, :w, :comments).merge(c: c['key'])),
class: "list-group-item#{' active' if params[:c] == c['key']}" do
c['key'].titleize.html_safe + content_tag(:small, c['doc_count'], class: 'badge').html_safe
end
%>
<% end %>
</div>
</div>
<div class="authors panel panel-default">
<p class="panel-heading"><%= link_to 'All Authors →'.html_safe, search_path(params.permit(:q, :a, :c, :s, :w, :comments).merge(a: nil))%></p>
<div class="list-group">
<% @articles.response.response['aggregations']['authors']['authors']['buckets'].each do |a| %>
<%=
link_to search_path(params.permit(:q, :a, :c, :s, :w, :comments).merge(a: a['key'])),
class: "list-group-item#{' active' if params[:a] == a['key']}" do
a['key'].titleize.html_safe + content_tag(:small, a['doc_count'], class: 'badge').html_safe
end
%>
<% end %>
</div>
</div>
<div class="authors panel panel-default">
<p class="panel-heading"><%= link_to 'Any Date →'.html_safe, search_path(params.permit(:q, :a, :c, :s, :w, :comments).merge(w: nil))%></p>
<div class="list-group">
<% @articles.response.response['aggregations']['published']['published']['buckets'].each do |w| %>
<%=
__start = Time.at(w['key']/1000)
__end = __start.end_of_week
__date = __start.to_date.to_s(:iso)
link_to search_path(params.permit(:q, :a, :c, :s, :w, :comments).merge(w: __date)),
class: "list-group-item#{' active' if params[:w] == __date}" do
"#{__start.to_date.to_s(:short)} — #{__end.to_date.to_s(:short)}".html_safe + \
content_tag(:small, w['doc_count'], class: 'badge').html_safe
end
%>
<% end %>
</div>
</div>
<% end %>
</div>
<div class="col-md-9">
<div id="results">
<% @articles.each do |article| %>
<div class="result">
<h3 class="title">
<%= (article.try(:highlight).try(:title) ? article.highlight.title.join.html_safe : article.title) %>
<small class="category"> | <%= article.categories.to_sentence %></small>
</h3>
<p class="body">
<% if article.try(:highlight).try(:abstract) %>
<%= article.highlight.abstract.join.html_safe %>
<% else %>
<%= article.try(:highlight).try(:content) ? article.highlight.content.join('…').html_safe : article.abstract %>
<% end %>
</p>
<% if comments = article.try(:highlight) && article.highlight['comments.body'] %>
<p class="comments">
Comments: <%= comments.join('…').html_safe %>
</p>
<% end %>
<p class="text-muted">
<small>Authors: <%= article.authors.map(&:full_name).to_sentence %></small> |
<small>Published: <%= article.published_on %></small> |
<small>Score: <%= article._score %></small>
</p>
</div>
<% end %>
</div>
<ul class="pager">
<li class="previous"><%= link_to_previous_page @articles, 'Previous Page', params: params.slice(:q, :c, :a, :comments) %></li>
<li class="next"><%= link_to_next_page @articles, 'Next Page', params: params.slice(:q, :c, :a, :comments) %></li>
</ul>
</div>
<div class="footer <%= @articles.size < 1 ? 'col-md-12' : 'col-md-9 col-md-offset-3' %>">
<p><small>Content provided by <a href="http://nytimes.com"><em>The New York Times</em></a>.</small></p>
</div>
|