Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redis search for mention complete #634

Merged
merged 2 commits into from May 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -23,6 +23,7 @@ gem 'rails-i18n', '5.0.0.beta3'
gem 'http_accept_language'
gem 'rails_autolink'
gem 'md_emoji'
gem 'jquery-atwho-rails'

# OAuth Provider
gem 'doorkeeper', '4.0.0.rc2'
Expand Down Expand Up @@ -53,6 +54,7 @@ gem 'will_paginate'
# 搜索
gem 'elasticsearch-model'
gem 'elasticsearch-rails'
gem 'redis-search', '1.0.0.beta2'

# 三方平台 OAuth 验证登陆
gem 'omniauth'
Expand Down
10 changes: 10 additions & 0 deletions Gemfile.lock
Expand Up @@ -190,6 +190,7 @@ GEM
jbuilder (2.4.1)
activesupport (>= 3.0.0, < 5.1)
multi_json (~> 1.2)
jquery-atwho-rails (1.3.2)
jquery-rails (4.1.1)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
Expand Down Expand Up @@ -331,8 +332,13 @@ GEM
redis (~> 3.0, >= 3.0.4)
redis-objects (1.2.1)
redis (>= 3.0.2)
redis-search (1.0.0.beta2)
redis (>= 3.0.0)
redis-namespace (>= 1.3.0)
ruby-pinyin (~> 0.3, >= 0.3.0)
responders (2.2.0)
railties (>= 4.2.0, < 5.1)
rmmseg-cpp-new (0.3.1)
rouge (1.8.0)
rspec-core (3.5.0.beta1)
rspec-support (= 3.5.0.beta1)
Expand All @@ -357,6 +363,8 @@ GEM
rainbow (>= 1.99.1, < 3.0)
ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.0, >= 1.0.1)
ruby-pinyin (0.5.0)
rmmseg-cpp-new (~> 0.3.1)
ruby-progressbar (1.8.0)
ruby-push-notifications (0.4.0)
builder (~> 3.0)
Expand Down Expand Up @@ -455,6 +463,7 @@ DEPENDENCIES
http_accept_language
jasmine-rails (~> 0.10.2)
jbuilder
jquery-atwho-rails
jquery-rails
jquery-turbolinks
kgio
Expand Down Expand Up @@ -483,6 +492,7 @@ DEPENDENCIES
redis
redis-namespace
redis-objects
redis-search (= 1.0.0.beta2)
rouge (~> 1.8.0)
rspec-rails (= 3.5.0.beta1)
rubocop (~> 0.39.0)
Expand Down
17 changes: 9 additions & 8 deletions app/assets/javascripts/app.coffee
Expand Up @@ -77,9 +77,7 @@ AppView = Backbone.View.extend
$("a[rel=twipsy]").tooltip()

# CommentAble @ 回复功能
commenters = App.scanLogins($(".cell_comments .comment .info .name a"))
commenters = ({login: k, name: v, search: "#{k} #{v}"} for k, v of commenters)
App.atReplyable(".cell_comments_new textarea", commenters)
App.atReplyable(".cell_comments_new textarea")

likeable : (e) ->
if !App.isLogined()
Expand Down Expand Up @@ -289,16 +287,19 @@ window.App =
result

atReplyable : (el, logins) ->
return if logins.length == 0
$(el).atwho
at : "@"
data : logins
search_key : "search"
tpl : "<li data-value='${login}'>${login} <small>${name}</small></li>"
searchKey: 'login'
callbacks:
remoteFilter: (query, callback) ->
$.getJSON '/search/users.json', { q: query }, (data) ->
callback(data)
displayTpl : "<li data-value='${login}'><img src='${avatar_url}' height='20' width='20'/> ${login} <small>${name}</small></li>"
insertTpl : "@${login}"
.atwho
at : ":"
data : window.EMOJI_LIST
tpl : "<li data-value='${name}:'><img src='#{App.asset_url}/assets/emojis/${name}.png' height='20' width='20'/> ${name} </li>"
displayTpl : "<li data-value='${name}:'><img src='#{App.asset_url}/assets/emojis/${name}.png' height='20' width='20'/> ${name} </li>"
true

$(document).on 'page:change', ->
Expand Down
7 changes: 2 additions & 5 deletions app/assets/javascripts/topics.coffee
Expand Up @@ -384,11 +384,8 @@ window.TopicView = Backbone.View.extend
backdrop : true
show : true

# @ Reply
logins = App.scanLogins($("#topic-show .leader a[data-author]"))
$.extend logins, App.scanLogins($('#replies span.name a'))
logins = ({login: k, name: v, search: "#{k} #{v}"} for k, v of logins)
App.atReplyable("textarea", logins)
# @ Mention complete
App.atReplyable("textarea")

# Focus title field in new-topic page
$("body[data-controller-name='topics'] #topic_title").focus()
Expand Down
8 changes: 8 additions & 0 deletions app/controllers/search_controller.rb
Expand Up @@ -17,4 +17,12 @@ def index
}
@result = Elasticsearch::Model.search(search_params, [Topic, User, Page]).paginate(page: params[:page], per_page: 30)
end

def users
@users = []
if params[:q].present?
@users = User.prefix_match(params[:q])
end
render json: @users.collect { |u| { login: u['title'], name: u['name'], avatar_url: u['large_avatar_url'] } }
end
end
5 changes: 5 additions & 0 deletions app/models/user.rb
Expand Up @@ -7,6 +7,7 @@ class User < ApplicationRecord
include BaseModel
extend OmniauthCallbacks
include Searchable
include Redis::Search

acts_as_cached version: 1, expires_in: 1.week

Expand All @@ -15,6 +16,10 @@ class User < ApplicationRecord
devise :database_authenticatable, :registerable, :recoverable,
:rememberable, :trackable, :validatable, :omniauthable

redis_search title_field: :login,
alias_field: :name,
ext_fields: [:large_avatar_url, :name]

mount_uploader :avatar, AvatarUploader

has_many :topics, dependent: :destroy
Expand Down
11 changes: 11 additions & 0 deletions config/initializers/redis.rb
@@ -1,6 +1,7 @@
require 'redis'
require 'redis-namespace'
require 'redis/objects'
require 'ruby-pinyin'

redis_config = YAML.load_file("#{Rails.root}/config/redis.yml")[Rails.env]

Expand All @@ -14,3 +15,13 @@
Sidekiq.configure_client do |config|
config.redis = { namespace: 'sidekiq', url: sidekiq_url }
end

# Redis Search
PinYin.backend = PinYin::Backend::Simple.new
redis_for_search = Redis::Namespace.new("rc-rs", redis: $redis)
redis_for_search.select(2)
Redis::Search.configure do |config|
config.redis = redis_for_search
config.complete_max_length = 100
config.pinyin_match = true
end
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -79,6 +79,7 @@
resources :jobs

get '/search' => 'search#index', as: 'search'
get '/search/users' => 'search#users', as: 'search_users'

namespace :admin do
root to: 'home#index', as: 'root'
Expand Down