Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from sad16/16-full-text-searching
full text searching
- Loading branch information
Showing
35 changed files
with
1,022 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,9 @@ | |
|
||
/config/database.yml | ||
|
||
/config/development.sphinx.conf | ||
/db/sphinx | ||
|
||
.idea | ||
|
||
homework | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
function clearForm(form) { | ||
$(form).find('input').val(''); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
function errorSearch(response) { | ||
var errors = response.detail[0].errors; | ||
var errorsBlock = $(response.currentTarget).find('.errors'); | ||
errorsBlock.html(errors); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
$(document).on('turbolinks:load', function() { | ||
onAjaxSuccessGlobalSearch($('#global-search-form')); | ||
}); | ||
|
||
function onAjaxSuccessGlobalSearch(form) { | ||
$(form) | ||
.on('ajax:success', function(response) { | ||
successGlobalSearch(response); | ||
}) | ||
.on('ajax:error', function(response) { | ||
errorSearch(response); | ||
}) | ||
} | ||
|
||
function successGlobalSearch(response) { | ||
var resultBlock = $('.result'); | ||
resultBlock.html(''); | ||
|
||
var data = response.detail[0]; | ||
|
||
if (data.length !== 0) { | ||
$.each(data, function(index, wrapper) { | ||
var item = wrapper.wrapper; | ||
var type = Object.keys(item)[0]; | ||
var data = Object.values(item)[0]; | ||
|
||
resultBlock.append(getTemplate(type, data)); | ||
}); | ||
} else { | ||
resultBlock.html('No result') | ||
} | ||
|
||
clearForm(response.currentTarget); | ||
} | ||
|
||
function getTemplate(type, data) { | ||
switch (type) { | ||
case 'question': | ||
var template = questionTemplate(data) | ||
break; | ||
case 'answer': | ||
var template = answerTemplate(data) | ||
break; | ||
case 'comment': | ||
var template = commentTemplate(data) | ||
break; | ||
case 'user': | ||
var template = userTemplate(data) | ||
break; | ||
} | ||
|
||
return template; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
$(document).on('turbolinks:load', function() { | ||
onAjaxSuccessSearch($('#questions-search-form'), questionTemplate); | ||
onAjaxSuccessSearch($('#answers-search-form'), answerTemplate); | ||
onAjaxSuccessSearch($('#comments-search-form'), commentTemplate); | ||
onAjaxSuccessSearch($('#users-search-form'), userTemplate); | ||
}); | ||
|
||
function onAjaxSuccessSearch(form, template) { | ||
$(form) | ||
.on('ajax:success', function(response) { | ||
successSearch(response, template); | ||
}) | ||
.on('ajax:error', function(response) { | ||
errorSearch(response); | ||
}) | ||
} | ||
|
||
function successSearch(response, template) { | ||
var resultBlock = $('.result'); | ||
resultBlock.html(''); | ||
|
||
var data = response.detail[0].data; | ||
|
||
if (data.length !== 0) { | ||
$.each(data, function(index, item) { | ||
resultBlock.append(template(item)); | ||
}); | ||
} else { | ||
resultBlock.html('No result') | ||
} | ||
|
||
clearForm(response.currentTarget); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
function questionTemplate(question) { | ||
return `<div className="item"><p className="link"><a href="/questions/${question.id}">Link</a></p><p className="title">${question.title}</p><p className="body">${question.body}</p><br></div>` | ||
} | ||
|
||
function answerTemplate(answer) { | ||
return `<div className="item"><p className="link"><a href="/questions/${answer.question_id}">Question Link</a></p><p className="body">${answer.body}</p><br></div>` | ||
} | ||
|
||
function commentTemplate(comment) { | ||
return `<div className="item"><p className="text">${comment.text}</p><br></div>` | ||
} | ||
|
||
function userTemplate(user) { | ||
return `<div className="item"><p className="email">${user.email}</p><br></div>` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
class SearchController < ApplicationController | ||
before_action :authenticate_user! | ||
|
||
rescue_from Services::Search::Base::EmptySearchError, with: :empty_search_error | ||
|
||
def global | ||
respond_to do |format| | ||
format.html | ||
format.json { render json: Services::Search::Global.new.call(search_params).to_a, each_serializer: Search::GlobalSerializer, adapter: :attributes } | ||
end | ||
end | ||
|
||
def questions | ||
respond_to do |format| | ||
format.html | ||
format.json { render_json(Services::Search::Questions.new.call(search_params)) } | ||
end | ||
end | ||
|
||
def answers | ||
respond_to do |format| | ||
format.html | ||
format.json { render_json(Services::Search::Answers.new.call(search_params)) } | ||
end | ||
end | ||
|
||
def comments | ||
respond_to do |format| | ||
format.html | ||
format.json { render_json(Services::Search::Comments.new.call(search_params)) } | ||
end | ||
end | ||
|
||
def users | ||
respond_to do |format| | ||
format.html | ||
format.json { render_json(Services::Search::Users.new.call(search_params)) } | ||
end | ||
end | ||
|
||
private | ||
|
||
def search_params | ||
params.require(:search).permit! | ||
end | ||
|
||
def empty_search_error | ||
render json: { errors: ['Error: search without params'] }, status: :unprocessable_entity | ||
end | ||
|
||
def render_json(data) | ||
render json: { data: data } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
ThinkingSphinx::Index.define :answer, with: :active_record do | ||
# fileds | ||
indexes body | ||
indexes user.email, as: :author, sortable: true | ||
|
||
# attributes | ||
has question_id, user_id, created_at, updated_at | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
ThinkingSphinx::Index.define :comment, with: :active_record do | ||
# fileds | ||
indexes text | ||
indexes user.email, as: :author, sortable: true | ||
|
||
# attributes | ||
has commentable_id, commentable_type, user_id, created_at, updated_at | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
ThinkingSphinx::Index.define :question, with: :active_record do | ||
# fileds | ||
indexes title, sortable: true | ||
indexes body | ||
indexes user.email, as: :author, sortable: true | ||
|
||
# attributes | ||
has user_id, created_at, updated_at | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
ThinkingSphinx::Index.define :user, with: :active_record do | ||
# fileds | ||
indexes email | ||
|
||
# attributes | ||
has created_at, updated_at | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Search | ||
class GlobalSerializer < ApplicationSerializer | ||
attributes :wrapper | ||
|
||
def wrapper | ||
{ | ||
object.class.name.downcase => object | ||
} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module Services | ||
module Search | ||
class Answers < Base | ||
|
||
private | ||
|
||
def search_klass | ||
Answer | ||
end | ||
|
||
def search_conditions(params) | ||
{ | ||
body: params[:body].presence, | ||
author: params[:author].presence, | ||
} | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module Services | ||
module Search | ||
class Base < ApplicationService | ||
class Error < StandardError; end | ||
class EmptySearchError < Error; end | ||
|
||
def call(params) | ||
search(params.to_h.deep_symbolize_keys) | ||
end | ||
|
||
private | ||
|
||
def search(params) | ||
search_options = search_options(params) | ||
validate_search_options(search_options) | ||
search_klass.search(*search_options) | ||
end | ||
|
||
def search_options(params) | ||
[ | ||
params[:global].presence, | ||
{ conditions: search_conditions(params).compact } | ||
] | ||
end | ||
|
||
def validate_search_options(search_options) | ||
raise EmptySearchError if search_options[0].nil? && search_options[1][:conditions].blank? | ||
end | ||
|
||
def search_klass | ||
raise NotImplementedError | ||
end | ||
|
||
def search_conditions(params) | ||
{} | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.