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