我在我的一个rails应用程序中使用elasticsearch,它似乎有效,但大多数情况下,当用户搜索那里的标签或帖子或用户时,它不返回结果而是返回搜索页面我尝试在我的设置中找到问题,但一切看起来都不错。
服务器报告
2017-09-03T06:35:47.787828+00:00 heroku[router]: at=info method=GET path="/api/autocomplete.json/?term=kol" host=www.wewrite.in request_id=8b55bddf-04e7-4512-9063-2f34e7363aa5 fwd="47.15.7.112" dyno=web.1 connect=0ms service=83ms status=304 bytes=647 protocol=http
2017-09-03T06:35:47.996239+00:00 app[web.1]: Rendered api/search_autocomplete/index.json.jbuilder (0.2ms)
2017-09-03T06:35:47.996592+00:00 app[web.1]: Completed 200 OK in 80ms (Views: 0.9ms | ActiveRecord: 0.0ms)
2017-09-03T06:35:47.998254+00:00 heroku[router]: at=info method=GET path="/api/autocomplete.json/?term=kolk" host=www.wewrite.in request_id=0179c693-8175-4347-b11a-299a9871a90e fwd="47.15.7.112" dyno=web.1 connect=0ms service=88ms status=304 bytes=647 protocol=http
2017-09-03T06:35:49.438727+00:00 heroku[router]: at=info method=GET path="/api/autocomplete.json/?term=kolka" host=www.wewrite.in request_id=e9f90b80-5804-4606-9495-1d87e4002935 fwd="47.15.7.112" dyno=web.1 connect=0ms service=49ms status=304 bytes=647 protocol=http
2017-09-03T06:35:49.391318+00:00 app[web.1]: Started GET "/api/autocomplete.json/?term=kolka" for 47.15.7.112 at 2017-09-03 06:35:49 +0000
2017-09-03T06:35:49.396815+00:00 app[web.1]: Processing by API::SearchAutocompleteController#index as JSON
2017-09-03T06:35:49.396866+00:00 app[web.1]: Parameters: {"term"=>"kolka"}
2017-09-03T06:35:49.436864+00:00 app[web.1]: Rendered api/search_autocomplete/index.json.jbuilder (0.3ms)
2017-09-03T06:35:49.436940+00:00 app[web.1]: Completed 200 OK in 40ms (Views: 1.0ms | ActiveRecord: 0.0ms)
2017-09-03T06:35:49.599323+00:00 app[web.1]: Started GET "/api/autocomplete.json/?term=kolkat" for 47.15.7.112 at 2017-09-03 06:35:49 +0000
2017-09-03T06:35:49.604309+00:00 app[web.1]: Processing by API::SearchAutocompleteController#index as JSON
2017-09-03T06:35:49.604366+00:00 app[web.1]: Parameters: {"term"=>"kolkat"}
2017-09-03T06:35:49.636210+00:00 app[web.1]: Rendered api/search_autocomplete/index.json.jbuilder (0.3ms)
2017-09-03T06:35:49.636426+00:00 app[web.1]: Completed 200 OK in 32ms (Views: 1.7ms | ActiveRecord: 0.0ms)
2017-09-03T06:35:49.654968+00:00 heroku[router]: at=info method=GET path="/api/autocomplete.json/?term=kolkat" host=www.wewrite.in request_id=e9b53eb5-6dec-4c28-b4c7-69ae0489f10e fwd="47.15.7.112" dyno=web.1 connect=0ms service=58ms status=304 bytes=647 protocol=http
2017-09-03T06:35:49.884801+00:00 heroku[router]: at=info method=GET path="/api/autocomplete.json/?term=kolkata" host=www.wewrite.in request_id=ca90c57e-0113-4e8c-8e71-c11d4d94fae3 fwd="47.15.7.112" dyno=web.1 connect=0ms service=51ms status=304 bytes=647 protocol=http
2017-09-03T06:35:49.835979+00:00 app[web.1]: Started GET "/api/autocomplete.json/?term=kolkata" for 47.15.7.112 at 2017-09-03 06:35:49 +0000
2017-09-03T06:35:49.844152+00:00 app[web.1]: Parameters: {"term"=>"kolkata"}
2017-09-03T06:35:49.883442+00:00 app[web.1]: Completed 200 OK in 39ms (Views: 1.3ms | ActiveRecord: 0.0ms)
2017-09-03T06:35:49.844076+00:00 app[web.1]: Processing by API::SearchAutocompleteController#index as JSON
2017-09-03T06:35:49.883151+00:00 app[web.1]: Rendered api/search_autocomplete/index.json.jbuilder (0.6ms)
2017-09-03T06:35:50.541060+00:00 app[web.1]: Processing by SearchController#show as HTML
2017-09-03T06:35:50.541986+00:00 app[web.1]: Parameters: {"utf8"=>"√", "button"=>"", "search"=>{"q"=>"kolkata"}}
2017-09-03T06:35:50.535308+00:00 app[web.1]: Started GET "/search?utf8=%E2%88%9A&button=&search%5Bq%5D=kolkata" for 47.15.7.112 at 2017-09-03 06:35:50 +0000
2017-09-03T06:35:50.543150+00:00 app[web.1]: Redirected to http://www.wewrite.in/search?q=kolkata
2017-09-03T06:35:50.543623+00:00 app[web.1]: Completed 302 Found in 2ms (ActiveRecord: 0.0ms)
2017-09-03T06:35:50.543462+00:00 app[web.1]: Filter chain halted as :beautify_url rendered or redirected
2017-09-03T06:35:50.545362+00:00 heroku[router]: at=info method=GET path="/search?utf8=%E2%88%9A&button=&search%5Bq%5D=kolkata" host=www.wewrite.in request_id=20b472c5-be7b-40c4-8af3-df71e654ad21 fwd="47.15.7.112" dyno=web.1 connect=0ms service=10ms status=302 bytes=802 protocol=http
2017-09-03T06:35:51.051440+00:00 app[web.1]: Started GET "/search?q=kolkata" for 47.15.7.112 at 2017-09-03 06:35:51 +0000
2017-09-03T06:35:51.059982+00:00 app[web.1]: Processing by SearchController#show as HTML
2017-09-03T06:35:51.060242+00:00 app[web.1]: Parameters: {"q"=>"kolkata"}
2017-09-03T06:35:51.081779+00:00 app[web.1]: Post Load (1.2ms) SELECT "posts".* FROM "posts" WHERE 1=0
2017-09-03T06:35:51.096921+00:00 app[web.1]: User Load (1.3ms) SELECT "users".* FROM "users" WHERE 1=0
2017-09-03T06:35:51.121114+00:00 app[web.1]: Tag Load (1.3ms) SELECT "tags".* FROM "tags" WHERE 1=0
2017-09-03T06:35:51.121604+00:00 app[web.1]: Rendered search/show.html.erb within layouts/simple (16.5ms)
2017-09-03T06:35:51.125759+00:00 app[web.1]: Rendered layouts/_overlay.html.erb (1.3ms)
2017-09-03T06:35:51.128360+00:00 app[web.1]: Rendered layouts/_simple_header.html.erb (1.8ms)
2017-09-03T06:35:51.128666+00:00 app[web.1]: Rendered layouts/_alert_messages.html.erb (0.1ms)
2017-09-03T06:35:51.129082+00:00 app[web.1]: Completed 200 OK in 69ms (Views: 29.8ms | ActiveRecord: 3.8ms)
2017-09-03T06:35:51.131165+00:00 heroku[router]: at=info method=GET path="/search?q=kolkata" host=www.wewrite.in request_id=e64f7d97-210f-4b5e-abef-4d4be2e36550 fwd="47.15.7.112" dyno=web.1 connect=6ms service=81ms status=200 bytes=4905 protocol=http
2017-09-03T06:36:38.802403+00:00 heroku[router]: at=info method=GET path="/search?utf8=%E2%9C%93&search%5Bq%5D=kolkata" host=www.wewrite.in request_id=d153226f-1daf-4e64-818a-edc1f0c1be28 fwd="47.15.7.112" dyno=web.1 connect=0ms service=8ms status=302 bytes=802 protocol=http
2017-09-03T06:36:38.796142+00:00 app[web.1]: Started GET "/search?utf8=%E2%9C%93&search%5Bq%5D=kolkata" for 47.15.7.112 at 2017-09-03 06:36:38 +0000
2017-09-03T06:36:38.799614+00:00 app[web.1]: Processing by SearchController#show as HTML
2017-09-03T06:36:38.799682+00:00 app[web.1]: Parameters: {"utf8"=>"✓", "search"=>{"q"=>"kolkata"}}
2017-09-03T06:36:38.800756+00:00 app[web.1]: Redirected to http://www.wewrite.in/search?q=kolkata
2017-09-03T06:36:38.800920+00:00 app[web.1]: Filter chain halted as :beautify_url rendered or redirected
2017-09-03T06:36:38.801078+00:00 app[web.1]: Completed 302 Found in 1ms (ActiveRecord: 0.0ms)
2017-09-03T06:36:39.363498+00:00 heroku[router]: at=info method=GET path="/search?q=kolkata" host=www.wewrite.in request_id=6f4fae0f-539c-4798-9cd5-7455828dc9bd fwd="47.15.7.112" dyno=web.1 connect=0ms service=54ms status=200 bytes=4905 protocol=http
2017-09-03T06:36:39.310111+00:00 app[web.1]: Started GET "/search?q=kolkata" for 47.15.7.112 at 2017-09-03 06:36:39 +0000
2017-09-03T06:36:39.313029+00:00 app[web.1]: Processing by SearchController#show as HTML
2017-09-03T06:36:39.313056+00:00 app[web.1]: Parameters: {"q"=>"kolkata"}
2017-09-03T06:36:39.329892+00:00 app[web.1]: Post Load (1.8ms) SELECT "posts".* FROM "posts" WHERE 1=0
2017-09-03T06:36:39.342708+00:00 app[web.1]: User Load (1.3ms) SELECT "users".* FROM "users" WHERE 1=0
2017-09-03T06:36:39.358607+00:00 app[web.1]: Tag Load (1.3ms) SELECT "tags".* FROM "tags" WHERE 1=0
2017-09-03T06:36:39.358879+00:00 app[web.1]: Rendered search/show.html.erb within layouts/simple (15.4ms)
2017-09-03T06:36:39.361251+00:00 app[web.1]: Rendered layouts/_overlay.html.erb (0.7ms)
2017-09-03T06:36:39.361253+00:00 app[web.1]: Rendered layouts/_simple_header.html.erb (0.3ms)
2017-09-03T06:36:39.361461+00:00 app[web.1]: Rendered layouts/_alert_messages.html.erb (0.1ms)
2017-09-03T06:36:39.361709+00:00 app[web.1]: Completed 200 OK in 49ms (Views: 17.3ms | ActiveRecord: 4.4ms)
搜索控制器
class SearchController < ApplicationController
before_action :beautify_url
layout "simple"
def show
@post_records = Post.search(query_term).paginate(page: params[:page]).records
@posts = @post_records.to_a.select { |post| post.published? }
@users = User.search(query_term).records.to_a
@tags = Tag.search(query_term).records
end
def users
@users = User.search(query_term).records.to_a
end
private
def beautify_url
if params[:search].present?
case params[:action]
when "show"
redirect_to search_url(q: params[:search][:q])
when "users"
redirect_to search_users_url(q: params[:search][:q])
end
end
end
def query_term
params[:q] || ''
end
end
elasticsearch_index_job.rb
class ElasticsearchIndexJob < ActiveJob::Base
queue_as :elasticsearch
def perform(operation, searchable_class, searchable_id)
if operation =~ /index|delete/
self.send(operation, searchable_class, searchable_id)
else
logger.warn "PostIndexJob cannot process #{operation}"
end
end
private
def index(searchable_class, searchable_id)
searchable = searchable_class.constantize.find_by(id: searchable_id)
searchable.__elasticsearch__.index_document if searchable.present?
end
def delete(searchable_class, searchable_id)
client = searchable_class.constantize.__elasticsearch__.client
client.delete index: searchable_class.underscore.downcase.pluralize,
type: searchable_class.underscore.downcase,
id: searchable_id
end
end
搜索节目页
<div class="col-xs-10 col-xs-offset-1">
<div class="row">
<div class="col-md-12 large-searchbar">
<%= form_for :search, url: search_path, method: :get do |f| %>
<%= f.search_field :q, value: params[:q], placeholder: 'Search The App', autofocus: true, autocomplete: 'off', onfocus: "this.value = this.value;" %>
<% end %>
</div>
</div>
<div class="row">
<div class="col-md-2">
<div class="search-filter-links">
<div class="active filter-link">
<%= link_to search_path(q: params[:q]) do %>
All <span class="glyphicon glyphicon-chevron-right pull-right"></span>
<% end %>
</div>
<div class="filter-link"><%= link_to 'People', search_users_path(q: params[:q]) %></div>
</div>
</div>
<div class="col-md-6" data-animation="fadeInUp-fadeOutDown">
<% if @posts.any? %>
<div data-behavior="endless-scroll">
<h4 class="small-heading border-top">Stories</h4>
<%= render @posts %>
</div>
<div class="center">
<%= will_paginate @post_records %>
</div>
<% end %>
</div>
<aside class="col-md-4" data-animation="fadeInUp-fadeOutDown">
<% if @tags.any? %>
<h4 class="small-heading border-top">Topics</h4>
<%= react_component('TagList', { tags: @tags }, { prerender: true }) %>
<% end %>
<% if @users.any? %>
<h4 class="small-heading border-top">People</h4>
<% @users.each do |user| %>
<%= render partial: 'search/user_card', locals: { user: user } %>
<% end %>
<% end %>
</aside>
</div>
</div>